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 2012 Open Source Robotics Foundation
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 <boost/unordered_map.hpp>
26 #include <string>
27 #include <boost/filesystem.hpp>
28 
29 #include "gazebo/gazebo_config.h"
30 
32 
33 #include "gazebo/msgs/msgs.hh"
34 
37 #include "gazebo/common/Timer.hh"
38 
39 #include "gazebo/util/UtilTypes.hh"
40 
41 namespace gazebo
42 {
43  namespace util
44  {
47 
48 #ifdef ENABLE_DIAGNOSTICS
49 
50 
51 
52  #define DIAG_TIMER_START(_name) \
53  gazebo::util::DiagnosticManager::Instance()->StartTimer(_name);
54 
60  #define DIAG_TIMER_LAP(_name, _prefix) \
61  gazebo::util::DiagnosticManager::Instance()->Lap(_name, _prefix);
62 
65  #define DIAG_TIMER_STOP(_name) \
66  gazebo::util::DiagnosticManager::Instance()->StopTimer(_name);
67 #else
68  #define DIAG_TIMER_START(_name) ((void) 0)
69  #define DIAG_TIMER_LAP(_name, _prefix) ((void)0)
70  #define DIAG_TIMER_STOP(_name) ((void) 0)
71 #endif
72 
75  class DiagnosticManager : public SingletonT<DiagnosticManager>
76  {
78  private: DiagnosticManager();
79 
81  private: virtual ~DiagnosticManager();
82 
85  public: void Init(const std::string &_worldName);
86 
90  public: void StartTimer(const std::string &_name);
91 
94  public: void StopTimer(const std::string &_name);
95 
101  public: void Lap(const std::string &_name, const std::string &_prefix);
102 
105  public: int GetTimerCount() const;
106 
110  public: common::Time GetTime(int _index) const;
111 
115  public: common::Time GetTime(const std::string &_label) const;
116 
120  public: std::string GetLabel(int _index) const;
121 
124  public: boost::filesystem::path GetLogPath() const;
125 
128  private: void Update(const common::UpdateInfo &_info);
129 
135  private: void AddTime(const std::string &_name, common::Time &_wallTime,
136  common::Time &_elapsedtime);
137 
139  private: typedef boost::unordered_map<std::string, DiagnosticTimerPtr>
140  TimerMap;
141 
143  private: TimerMap timers;
144 
146  private: boost::filesystem::path logPath;
147 
149  private: transport::NodePtr node;
150 
152  private: transport::PublisherPtr pub;
153 
155  private: msgs::Diagnostics msg;
156 
158  private: event::ConnectionPtr updateConnection;
159 
160  // Singleton implementation
161  private: friend class SingletonT<DiagnosticManager>;
162 
164  private: friend class DiagnosticTimer;
165  };
166 
170  {
173  public: DiagnosticTimer(const std::string &_name);
174 
176  public: virtual ~DiagnosticTimer();
177 
180  public: void Lap(const std::string &_prefix);
181 
182  // Documentation inherited
183  public: virtual void Start();
184 
185  // Documentation inherited
186  public: virtual void Stop();
187 
190  public: inline const std::string GetName() const
191  { return this->name; }
192 
194  private: std::string name;
195 
197  private: std::ofstream log;
198 
200  private: common::Time prevLap;
201  };
203  }
204 }
205 #endif