util/Diagnostics.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 #include "gazebo/util/system.hh"
41 
42 namespace gazebo
43 {
44  namespace util
45  {
48 
49 #ifdef ENABLE_DIAGNOSTICS
50  #define DIAG_TIMER_START(_name) \
54  gazebo::util::DiagnosticManager::Instance()->StartTimer(_name);
55 
61  #define DIAG_TIMER_LAP(_name, _prefix) \
62  gazebo::util::DiagnosticManager::Instance()->Lap(_name, _prefix);
63 
66  #define DIAG_TIMER_STOP(_name) \
67  gazebo::util::DiagnosticManager::Instance()->StopTimer(_name);
68 #else
69  #define DIAG_TIMER_START(_name) ((void) 0)
70  #define DIAG_TIMER_LAP(_name, _prefix) ((void)0)
71  #define DIAG_TIMER_STOP(_name) ((void) 0)
72 #endif
73 
77  public SingletonT<DiagnosticManager>
78  {
80  private: DiagnosticManager();
81 
83  private: virtual ~DiagnosticManager();
84 
87  public: void Init(const std::string &_worldName);
88 
92  public: void StartTimer(const std::string &_name);
93 
96  public: void StopTimer(const std::string &_name);
97 
103  public: void Lap(const std::string &_name, const std::string &_prefix);
104 
107  public: int GetTimerCount() const;
108 
112  public: common::Time GetTime(int _index) const;
113 
117  public: common::Time GetTime(const std::string &_label) const;
118 
122  public: std::string GetLabel(int _index) const;
123 
126  public: boost::filesystem::path GetLogPath() const;
127 
130  private: void Update(const common::UpdateInfo &_info);
131 
137  private: void AddTime(const std::string &_name, common::Time &_wallTime,
138  common::Time &_elapsedtime);
139 
141  private: typedef boost::unordered_map<std::string, DiagnosticTimerPtr>
142  TimerMap;
143 
145  private: TimerMap timers;
146 
148  private: boost::filesystem::path logPath;
149 
151  private: transport::NodePtr node;
152 
154  private: transport::PublisherPtr pub;
155 
157  private: msgs::Diagnostics msg;
158 
160  private: event::ConnectionPtr updateConnection;
161 
162  // Singleton implementation
163  private: friend class SingletonT<DiagnosticManager>;
164 
166  private: friend class DiagnosticTimer;
167  };
168 
172  {
175  public: DiagnosticTimer(const std::string &_name);
176 
178  public: virtual ~DiagnosticTimer();
179 
182  public: void Lap(const std::string &_prefix);
183 
184  // Documentation inherited
185  public: virtual void Start();
186 
187  // Documentation inherited
188  public: virtual void Stop();
189 
192  public: inline const std::string GetName() const
193  { return this->name; }
194 
196  private: std::string name;
197 
199  private: std::ofstream log;
200 
202  private: common::Time prevLap;
203  };
205  }
206 }
207 #endif
boost::shared_ptr< Connection > ConnectionPtr
Definition: CommonTypes.hh:144
Forward declarations for transport.
Singleton template class.
Definition: SingletonT.hh:33
A diagnostic manager class.
Definition: util/Diagnostics.hh:76
const std::string GetName() const
Get the name of the timer.
Definition: util/Diagnostics.hh:192
Information for use in an update event.
Definition: UpdateInfo.hh:30
A timer class, used to time things in real world walltime.
Definition: Timer.hh:38
boost::shared_ptr< Node > NodePtr
Definition: TransportTypes.hh:57
A timer designed for diagnostics.
Definition: util/Diagnostics.hh:171
GAZEBO_VISIBLE void Init(google::protobuf::Message &_message, const std::string &_id="")
Initialize a message.
boost::shared_ptr< Publisher > PublisherPtr
Definition: TransportTypes.hh:49
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:48
A Time class, can be used to hold wall- or sim-time.
Definition: Time.hh:43