dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
logger.h
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
37  MSG_TYPE_DEBUG_STREAM = MSG_TYPE_DEBUG | MSG_TYPE_STREAM_BIT, // 17
38  MSG_TYPE_INFO_STREAM = MSG_TYPE_INFO | MSG_TYPE_STREAM_BIT, // 18
39  MSG_TYPE_WARNING_STREAM = MSG_TYPE_WARNING | MSG_TYPE_STREAM_BIT, // 20
40  MSG_TYPE_ERROR_STREAM = MSG_TYPE_ERROR | MSG_TYPE_STREAM_BIT // 24
41 };
42 } // namespace dynamicgraph
43 
44 /* --------------------------------------------------------------------- */
45 /* --- INCLUDE --------------------------------------------------------- */
46 /* --------------------------------------------------------------------- */
47 
48 #include <map>
50 #include <fstream>
51 #include <iomanip> // std::setprecision
52 #include <sstream>
53 
54 #include <boost/assign.hpp>
55 #include <boost/preprocessor/stringize.hpp>
56 
57 #include <dynamic-graph/deprecated.hh>
58 #include <dynamic-graph/linear-algebra.h>
59 #include <dynamic-graph/real-time-logger-def.h>
60 
61 namespace dynamicgraph {
62 
63 //#define LOGGER_VERBOSITY_INFO_WARNING_ERROR
64 #define LOGGER_VERBOSITY_ALL
65 
66 #define SEND_MSG(msg, type) \
67  sendMsg(msg, type, __FILE__ ":" BOOST_PP_STRINGIZE(__LINE__))
68 
69 #define SEND_DEBUG_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_DEBUG_STREAM)
70 #define SEND_INFO_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO_STREAM)
71 #define SEND_WARNING_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING_STREAM)
72 #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM)
73 
74 #define _DYNAMIC_GRAPH_ENTITY_MSG(entity, type) \
75  (entity).logger().stream(type, __FILE__ BOOST_PP_STRINGIZE(__LINE__))
76 
77 #define DYNAMIC_GRAPH_ENTITY_DEBUG(entity) \
78  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_DEBUG)
79 #define DYNAMIC_GRAPH_ENTITY_INFO(entity) \
80  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_INFO)
81 #define DYNAMIC_GRAPH_ENTITY_WARNING(entity) \
82  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_WARNING)
83 #define DYNAMIC_GRAPH_ENTITY_ERROR(entity) \
84  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_ERROR)
85 
86 #define DYNAMIC_GRAPH_ENTITY_DEBUG_STREAM(entity) \
87  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_DEBUG_STREAM)
88 #define DYNAMIC_GRAPH_ENTITY_INFO_STREAM(entity) \
89  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_INFO_STREAM)
90 #define DYNAMIC_GRAPH_ENTITY_WARNING_STREAM(entity) \
91  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_WARNING_STREAM)
92 #define DYNAMIC_GRAPH_ENTITY_ERROR_STREAM(entity) \
93  _DYNAMIC_GRAPH_ENTITY_MSG(entity, MSG_TYPE_ERROR_STREAM)
94 
95 template <typename T>
96 std::string toString(const T &v, const int precision = 3,
97  const int width = -1) {
98  std::stringstream ss;
99  if (width > precision)
100  ss << std::fixed << std::setw(width) << std::setprecision(precision) << v;
101  else
102  ss << std::fixed << std::setprecision(precision) << v;
103  return ss.str();
104 }
105 
106 template <typename T>
107 std::string toString(const std::vector<T> &v, const int precision = 3,
108  const int width = -1, const std::string separator = ", ") {
109  std::stringstream ss;
110  if (width > precision) {
111  for (unsigned int i = 0; i < v.size() - 1; i++)
112  ss << std::fixed << std::setw(width) << std::setprecision(precision)
113  << v[i] << separator;
114  ss << std::fixed << std::setw(width) << std::setprecision(precision)
115  << v[v.size() - 1];
116  } else {
117  for (unsigned int i = 0; i < v.size() - 1; i++)
118  ss << std::fixed << std::setprecision(precision) << v[i] << separator;
119  ss << std::fixed << std::setprecision(precision) << v[v.size() - 1];
120  }
121 
122  return ss.str();
123 }
124 
125 template <typename T>
126 std::string toString(const Eigen::MatrixBase<T> &v, const int precision = 3,
127  const int width = -1, const std::string separator = ", ") {
128  std::stringstream ss;
129  if (width > precision) {
130  for (unsigned int i = 0; i < v.size() - 1; i++)
131  ss << std::fixed << std::setw(width) << std::setprecision(precision)
132  << v[i] << separator;
133  ss << std::fixed << std::setw(width) << std::setprecision(precision)
134  << v[v.size() - 1];
135  } else {
136  for (unsigned int i = 0; i < v.size() - 1; i++)
137  ss << std::fixed << std::setprecision(precision) << v[i] << separator;
138  ss << std::setprecision(precision) << v[v.size() - 1];
139  }
140 
141  return ss.str();
142 }
143 
144 enum LoggerVerbosity {
145  VERBOSITY_ALL = MSG_TYPE_DEBUG,
146  VERBOSITY_INFO_WARNING_ERROR = MSG_TYPE_INFO,
147  VERBOSITY_WARNING_ERROR = MSG_TYPE_WARNING,
148  VERBOSITY_ERROR = MSG_TYPE_ERROR,
149  VERBOSITY_NONE = 0
150 };
151 
187 class Logger {
188 public:
190  Logger(double timeSample = 0.001, double streamPrintPeriod = 1.0);
191 
193  ~Logger();
194 
197  void countdown();
198 
201  RTLoggerStream stream() {
202  return ::dynamicgraph::RealTimeLogger::instance().front();
203  }
204 
212  RTLoggerStream stream(MsgType type, const std::string &lineId = "") {
213  RealTimeLogger &rtlogger = ::dynamicgraph::RealTimeLogger::instance();
214  if (acceptMsg(type, lineId))
215  return rtlogger.front();
216  return rtlogger.emptyStream();
217  }
218 
224  void sendMsg(std::string msg, MsgType type, const std::string &lineId = "");
225 
231  void sendMsg(std::string msg, MsgType type, const std::string &file,
232  int line) DYNAMIC_GRAPH_DEPRECATED;
233 
236  bool setTimeSample(double t);
237 
240  double getTimeSample();
241 
243  bool setStreamPrintPeriod(double s);
244 
246  double getStreamPrintPeriod();
247 
249  void setVerbosity(LoggerVerbosity lv);
250 
252  LoggerVerbosity getVerbosity();
253 
254 protected:
255  LoggerVerbosity m_lv;
256  double m_timeSample;
261 
262  typedef std::map<std::string, double> StreamCounterMap_t;
265  StreamCounterMap_t m_stream_msg_counters;
266 
267  inline bool isStreamMsg(MsgType m) { return (m & MSG_TYPE_STREAM_BIT); }
268 
273  bool acceptMsg(MsgType m, const std::string &lineId) {
274  // If more verbose than the current verbosity level
275  if ((m & MSG_TYPE_TYPE_BITS) > m_lv)
276  return false;
277 
278  // if print is allowed by current verbosity level
279  if (isStreamMsg(m))
280  return checkStreamPeriod(lineId);
281  return true;
282  }
283 
287  bool checkStreamPeriod(const std::string &lineId);
288 };
289 
290 } // namespace dynamicgraph
291 
292 #endif // #ifndef __sot_torque_control_logger_H__
Logger(double timeSample=0.001, double streamPrintPeriod=1.0)
bool setTimeSample(double t)
double m_timeSample
verbosity of the logger
Definition: logger.h:256
RTLoggerStream stream(MsgType type, const std::string &lineId="")
Definition: logger.h:212
StreamCounterMap_t m_stream_msg_counters
Definition: logger.h:265
Main class of the real-time logger.
double m_streamPrintPeriod
specify the period of call of the countdown method
Definition: logger.h:258
void setVerbosity(LoggerVerbosity lv)
std::map< std::string, double > StreamCounterMap_t
every time this is < 0 (i.e. every _streamPrintPeriod sec) print stuff
Definition: logger.h:262
RTLoggerStream emptyStream()
Return an empty stream object.
double m_printCountdown
specify the time period of the stream prints
Definition: logger.h:259
RTLoggerStream stream()
Definition: logger.h:201
double getStreamPrintPeriod()
LoggerVerbosity getVerbosity()
bool acceptMsg(MsgType m, const std::string &lineId)
Definition: logger.h:273
Class for logging messages.
Definition: logger.h:187
void sendMsg(std::string msg, MsgType type, const std::string &lineId="")
bool setStreamPrintPeriod(double s)
bool checkStreamPeriod(const std::string &lineId)