All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Diagnostics.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Nate Koenig
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 /* Desc: A diagnostic class
18  * Author: Nate Koenig
19  * Date: 2 Feb 2011
20  */
21 
22 #ifndef _DIAGNOSTICMANAGER_HH_
23 #define _DIAGNOSTICMANAGER_HH_
24 
25 #include <map>
26 #include <string>
27 
28 #include "common/SingletonT.hh"
29 #include "common/Timer.hh"
30 
31 namespace gazebo
32 {
33  namespace common
34  {
37 
39  #define DIAG_TIMER(name) DiagnosticManager::Instance()->CreateTimer(name);
40 
42  typedef boost::shared_ptr< DiagnosticTimer > DiagnosticTimerPtr;
43 
45  class DiagnosticManager : public SingletonT<DiagnosticManager>
46  {
48  private: DiagnosticManager();
49 
51  private: virtual ~DiagnosticManager();
52 
56  public: DiagnosticTimerPtr CreateTimer(const std::string &_name);
57 
60  public: void TimerStart(DiagnosticTimer *_timer);
61 
64  public: void TimerStop(DiagnosticTimer *_timer);
65 
68  public: int GetTimerCount() const;
69 
73  public: Time GetTime(int _index) const;
74 
78  public: Time GetTime(const std::string &_label) const;
79 
83  public: std::string GetLabel(int _index) const;
84 
87  public: void SetEnabled(bool _e) {this->enabled = _e;}
88 
91  public: inline bool GetEnabled() const {return this->enabled;}
92 
94  private: bool enabled;
95 
97  private: std::map<std::string, Time> timers;
98 
99  // Singleton implementation
100  private: friend class SingletonT<DiagnosticManager>;
101  };
102 
104  class DiagnosticTimer : public Timer
105  {
108  public: DiagnosticTimer(const std::string &_name) : Timer()
109  {
110  this->Start();
111  this->name = _name;
112  this->diagManager->TimerStart(this);
113  }
114 
116  public: virtual ~DiagnosticTimer()
117  {
118  this->diagManager->TimerStop(this);
119  }
120 
123  public: inline const std::string GetName() const
124  { return this->name; }
125 
127  private: std::string name;
128 
130  private: static DiagnosticManager *diagManager;
131  };
133  }
134 }
135 #endif