logger.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015, 2019
3  * LAAS-CNRS
4  * Andrea Del Prete, François Bailly, Olivier Stasse
5  *
6  */
7 
8 #ifndef __dynamic_graph_logger_H__
9 #define __dynamic_graph_logger_H__
10 
11 /* --------------------------------------------------------------------- */
12 /* --- API ------------------------------------------------------------- */
13 /* --------------------------------------------------------------------- */
14 
15 #if defined(WIN32)
16 #if defined(logger_EXPORTS)
17 #define LOGGER_EXPORT __declspec(dllexport)
18 #else
19 #define LOGGER_EXPORT __declspec(dllimport)
20 #endif
21 #else
22 #define LOGGER_EXPORT
23 #endif
24 
25 namespace dynamicgraph {
26 
29 enum MsgType {
30  MSG_TYPE_TYPE_BITS = 1 << 0 | 1 << 1 | 1 << 2 | 1 << 3, // 15
31  MSG_TYPE_STREAM_BIT = 1 << 4, // 16
32 
33  MSG_TYPE_DEBUG = 1 << 3, // 1
34  MSG_TYPE_INFO = 1 << 2, // 2
35  MSG_TYPE_WARNING = 1 << 1, // 4
36  MSG_TYPE_ERROR = 1 << 0, // 8
41 };
42 } // namespace dynamicgraph
43 
44 /* --------------------------------------------------------------------- */
45 /* --- INCLUDE --------------------------------------------------------- */
46 /* --------------------------------------------------------------------- */
47 
48 #include <map>
52 
53 #include <boost/assign.hpp>
54 #include <boost/preprocessor/stringize.hpp>
55 #include <dynamic-graph/deprecated.hh>
56 #include <fstream>
57 #include <iomanip> // std::setprecision
58 #include <sstream>
59 
60 namespace dynamicgraph {
61 
62 // #define LOGGER_VERBOSITY_INFO_WARNING_ERROR
63 #define LOGGER_VERBOSITY_ALL
64 
65 #define SEND_MSG(msg, type) \
66  sendMsg(msg, type, __FILE__ ":" BOOST_PP_STRINGIZE(__LINE__))
67 
68 #define SEND_DEBUG_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_DEBUG_STREAM)
69 #define SEND_INFO_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO_STREAM)
70 #define SEND_WARNING_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING_STREAM)
71 #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM)
72 
73 #define _DYNAMIC_GRAPH_ENTITY_MSG(entity, type) \
74  (entity).logger().stream(type, __FILE__ BOOST_PP_STRINGIZE(__LINE__))
75 
76 #define DYNAMIC_GRAPH_ENTITY_DEBUG(entity) \
77  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_DEBUG)
78 #define DYNAMIC_GRAPH_ENTITY_INFO(entity) \
79  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_INFO)
80 #define DYNAMIC_GRAPH_ENTITY_WARNING(entity) \
81  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_WARNING)
82 #define DYNAMIC_GRAPH_ENTITY_ERROR(entity) \
83  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_ERROR)
84 
85 #define DYNAMIC_GRAPH_ENTITY_DEBUG_STREAM(entity) \
86  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_DEBUG_STREAM)
87 #define DYNAMIC_GRAPH_ENTITY_INFO_STREAM(entity) \
88  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_INFO_STREAM)
89 #define DYNAMIC_GRAPH_ENTITY_WARNING_STREAM(entity) \
90  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_WARNING_STREAM)
91 #define DYNAMIC_GRAPH_ENTITY_ERROR_STREAM(entity) \
92  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_ERROR_STREAM)
93 
94 template <typename T>
95 std::string toString(const T &v, const int precision = 3,
96  const int width = -1) {
97  std::stringstream ss;
98  if (width > precision)
99  ss << std::fixed << std::setw(width) << std::setprecision(precision) << v;
100  else
101  ss << std::fixed << std::setprecision(precision) << v;
102  return ss.str();
103 }
104 
105 template <typename T>
106 std::string toString(const std::vector<T> &v, const int precision = 3,
107  const int width = -1, const std::string separator = ", ") {
108  std::stringstream ss;
109  if (width > precision) {
110  for (unsigned int i = 0; i < v.size() - 1; i++)
111  ss << std::fixed << std::setw(width) << std::setprecision(precision)
112  << v[i] << separator;
113  ss << std::fixed << std::setw(width) << std::setprecision(precision)
114  << v[v.size() - 1];
115  } else {
116  for (unsigned int i = 0; i < v.size() - 1; i++)
117  ss << std::fixed << std::setprecision(precision) << v[i] << separator;
118  ss << std::fixed << std::setprecision(precision) << v[v.size() - 1];
119  }
120 
121  return ss.str();
122 }
123 
124 template <typename T>
125 std::string toString(const Eigen::MatrixBase<T> &v, const int precision = 3,
126  const int width = -1, const std::string separator = ", ") {
127  std::stringstream ss;
128  if (width > precision) {
129  for (unsigned int i = 0; i < v.size() - 1; i++)
130  ss << std::fixed << std::setw(width) << std::setprecision(precision)
131  << v[i] << separator;
132  ss << std::fixed << std::setw(width) << std::setprecision(precision)
133  << v[v.size() - 1];
134  } else {
135  for (unsigned int i = 0; i < v.size() - 1; i++)
136  ss << std::fixed << std::setprecision(precision) << v[i] << separator;
137  ss << std::setprecision(precision) << v[v.size() - 1];
138  }
139 
140  return ss.str();
141 }
142 
149 };
150 
186 class Logger {
187  public:
189  Logger(double timeSample = 0.001, double streamPrintPeriod = 1.0);
190 
192  ~Logger();
193 
196  void countdown();
197 
200  RTLoggerStream stream() {
201  return ::dynamicgraph::RealTimeLogger::instance().front();
202  }
203 
211  RTLoggerStream stream(MsgType type, const std::string &lineId = "") {
213  if (acceptMsg(type, lineId)) return rtlogger.front();
214  return rtlogger.emptyStream();
215  }
216 
222  [[deprecated("use stream(type, lineId) << msg")]] void sendMsg(
223  std::string msg, MsgType type, const std::string &lineId = "");
224 
230  [[deprecated("use stream(type, lineId) << msg")]] void sendMsg(
231  std::string msg, MsgType type, const std::string &file, int line);
232 
235  bool setTimeSample(double t);
236 
239  double getTimeSample();
240 
242  bool setStreamPrintPeriod(double s);
243 
245  double getStreamPrintPeriod();
246 
248  void setVerbosity(LoggerVerbosity lv);
249 
252 
253  protected:
255  double m_timeSample;
260 
261  typedef std::map<std::string, double> StreamCounterMap_t;
265 
266  inline bool isStreamMsg(MsgType m) { return (m & MSG_TYPE_STREAM_BIT); }
267 
272  bool acceptMsg(MsgType m, const std::string &lineId) {
273  // If more verbose than the current verbosity level
274  if ((m & MSG_TYPE_TYPE_BITS) > m_lv) return false;
275 
276  // if print is allowed by current verbosity level
277  if (isStreamMsg(m)) return checkStreamPeriod(lineId);
278  return true;
279  }
280 
284  bool checkStreamPeriod(const std::string &lineId);
285 };
286 
287 } // namespace dynamicgraph
288 
289 #endif // #ifndef __sot_torque_control_logger_H__
dynamicgraph::toString
std::string toString(const T &v, const int precision=3, const int width=-1)
Definition: logger.h:95
dynamicgraph::Logger::m_stream_msg_counters
StreamCounterMap_t m_stream_msg_counters
Definition: logger.h:264
linear-algebra.h
dynamicgraph::Logger::setTimeSample
bool setTimeSample(double t)
Definition: logger.cpp:53
dynamicgraph::Logger::countdown
void countdown()
Definition: logger.cpp:37
dynamicgraph::RealTimeLogger::emptyStream
RTLoggerStream emptyStream()
Return an empty stream object.
Definition: real-time-logger-def.h:122
dynamicgraph::Logger::setStreamPrintPeriod
bool setStreamPrintPeriod(double s)
Definition: logger.cpp:59
dynamicgraph::Logger::getStreamPrintPeriod
double getStreamPrintPeriod()
Definition: logger.cpp:67
dynamicgraph::Logger
Class for logging messages.
Definition: logger.h:186
dynamicgraph::MSG_TYPE_DEBUG_STREAM
@ MSG_TYPE_DEBUG_STREAM
Definition: logger.h:37
dynamicgraph::LoggerVerbosity
LoggerVerbosity
Definition: logger.h:143
dynamicgraph::Logger::m_printCountdown
double m_printCountdown
specify the time period of the stream prints
Definition: logger.h:258
dynamicgraph
dynamicgraph::Logger::acceptMsg
bool acceptMsg(MsgType m, const std::string &lineId)
Definition: logger.h:272
dynamicgraph::Logger::setVerbosity
void setVerbosity(LoggerVerbosity lv)
Definition: logger.cpp:34
dynamicgraph::Logger::m_lv
LoggerVerbosity m_lv
Definition: logger.h:254
dynamicgraph::Logger::~Logger
~Logger()
Definition: logger.cpp:32
dynamicgraph::Logger::isStreamMsg
bool isStreamMsg(MsgType m)
Definition: logger.h:266
dynamicgraph::RealTimeLogger
Main class of the real-time logger.
Definition: real-time-logger-def.h:96
dynamicgraph::MSG_TYPE_WARNING_STREAM
@ MSG_TYPE_WARNING_STREAM
Definition: logger.h:39
dynamicgraph::VERBOSITY_INFO_WARNING_ERROR
@ VERBOSITY_INFO_WARNING_ERROR
Definition: logger.h:145
dynamicgraph::MSG_TYPE_WARNING
@ MSG_TYPE_WARNING
Definition: logger.h:35
dynamicgraph::MSG_TYPE_STREAM_BIT
@ MSG_TYPE_STREAM_BIT
Definition: logger.h:31
dynamicgraph::MSG_TYPE_INFO
@ MSG_TYPE_INFO
Definition: logger.h:34
dynamicgraph::MSG_TYPE_ERROR
@ MSG_TYPE_ERROR
Definition: logger.h:36
dynamicgraph::Logger::m_streamPrintPeriod
double m_streamPrintPeriod
specify the period of call of the countdown method
Definition: logger.h:257
dynamicgraph::Logger::getVerbosity
LoggerVerbosity getVerbosity()
Definition: logger.cpp:36
dynamicgraph::VERBOSITY_WARNING_ERROR
@ VERBOSITY_WARNING_ERROR
Definition: logger.h:146
dynamicgraph::MSG_TYPE_ERROR_STREAM
@ MSG_TYPE_ERROR_STREAM
Definition: logger.h:40
dynamicgraph::MsgType
MsgType
Definition: logger.h:29
dynamicgraph::Logger::stream
RTLoggerStream stream(MsgType type, const std::string &lineId="")
Definition: logger.h:211
dynamicgraph::MSG_TYPE_DEBUG
@ MSG_TYPE_DEBUG
Definition: logger.h:33
dynamicgraph::Logger::StreamCounterMap_t
std::map< std::string, double > StreamCounterMap_t
every time this is < 0 (i.e. every _streamPrintPeriod sec) print stuff
Definition: logger.h:261
real-time-logger-def.h
dynamicgraph::VERBOSITY_ALL
@ VERBOSITY_ALL
Definition: logger.h:144
dynamicgraph::Logger::stream
RTLoggerStream stream()
Definition: logger.h:200
dynamicgraph::Logger::m_timeSample
double m_timeSample
verbosity of the logger
Definition: logger.h:255
dynamicgraph::VERBOSITY_ERROR
@ VERBOSITY_ERROR
Definition: logger.h:147
dynamicgraph::VERBOSITY_NONE
@ VERBOSITY_NONE
Definition: logger.h:148
dynamicgraph::Logger::Logger
Logger(double timeSample=0.001, double streamPrintPeriod=1.0)
Definition: logger.cpp:25
dynamicgraph::RealTimeLogger::front
RTLoggerStream front()
Definition: src/debug/real-time-logger.cpp:42
dynamicgraph::MSG_TYPE_TYPE_BITS
@ MSG_TYPE_TYPE_BITS
Definition: logger.h:30
dynamicgraph::RealTimeLogger::instance
static RealTimeLogger & instance()
Definition: src/debug/real-time-logger.cpp:121
dynamicgraph::Logger::getTimeSample
double getTimeSample()
Definition: logger.cpp:65
dynamicgraph::Logger::checkStreamPeriod
bool checkStreamPeriod(const std::string &lineId)
Definition: logger.cpp:69
dynamicgraph::MSG_TYPE_INFO_STREAM
@ MSG_TYPE_INFO_STREAM
Definition: logger.h:38
dynamicgraph::Logger::sendMsg
void sendMsg(std::string msg, MsgType type, const std::string &lineId="")
Definition: logger.cpp:42


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38