Diagnostics.hh
00001 /*
00002  * Copyright 2011 Nate Koenig & Andrew Howard
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 /* Desc: A diagnostic class
00018  * Author: Nate Koenig
00019  * Date: 2 Feb 2011
00020  */
00021 
00022 #ifndef DIAGNOSTICMANAGER_HH
00023 #define DIAGNOSTICMANAGER_HH
00024 
00025 #include "common/SingletonT.hh"
00026 #include "common/Timer.hh"
00027 
00028 namespace gazebo
00029 {
00030     namespace common
00031   {
00034 
00035     #define DIAG_TIMER(name) DiagnosticManager::Instance()->CreateTimer(name);
00036   
00037     class DiagnosticTimer;
00038     typedef boost::shared_ptr< DiagnosticTimer > DiagnosticTimerPtr;
00039   
00041     class DiagnosticManager : public SingletonT<DiagnosticManager>
00042     {
00044       private: DiagnosticManager();
00045   
00047       private: virtual ~DiagnosticManager();
00048   
00049       public: DiagnosticTimerPtr CreateTimer(const std::string &name);
00050   
00052       public: void TimerStart(DiagnosticTimer *timer);
00053   
00055       public: void TimerStop(DiagnosticTimer *timer);
00056   
00058       public: int GetTimerCount() const;
00059   
00061       public: Time GetTime(int index) const;
00062   
00064       public: Time GetTime(const std::string &label) const;
00065   
00067       public: std::string GetLabel(int index) const;
00068   
00069       public: void SetEnabled(bool e) {this->enabled = e;}
00070   
00071       public: inline bool GetEnabled() const {return this->enabled;}
00072   
00073       private: bool enabled;
00074   
00075       private: std::map<std::string, Time> timers;
00076   
00077       //Singleton implementation
00078       private: friend class SingletonT<DiagnosticManager>;
00079     };
00080   
00082     class DiagnosticTimer : public Timer
00083     {
00085       public: DiagnosticTimer(const std::string &name) : Timer() 
00086               {
00087                 this->Start();
00088                 this->name = name; 
00089                 this->diagManager->TimerStart(this);
00090               }
00091   
00093       public: virtual ~DiagnosticTimer() 
00094               { 
00095                 this->diagManager->TimerStop(this);
00096               }
00097   
00098       public: inline const std::string GetName() const 
00099               { return this->name; }
00100   
00101       private: std::string name;
00102       private: static DiagnosticManager *diagManager;
00103     };
00104 
00106   }
00107  
00108 }
00109 #endif