hpp-centroidal-dynamics  4.10.0
Utility classes for testing (robust) equilibrium of a system in contact with the environment, and other centroidal dynamics methods.
logger.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2015, LAAS-CNRS
3  * Author: Andrea Del Prete
4  */
5 
6 #ifndef HPP_CENTROIDAL_DYNAMICS_LOGGER_HH
7 #define HPP_CENTROIDAL_DYNAMICS_LOGGER_HH
8 
9 /* --------------------------------------------------------------------- */
10 /* --- INCLUDE --------------------------------------------------------- */
11 /* --------------------------------------------------------------------- */
12 
14 #include <sstream>
15 #include <Eigen/Dense>
16 #include <map>
17 #include "boost/assign.hpp"
18 
19 namespace centroidal_dynamics {
20 
21 //#define LOGGER_VERBOSITY_ERROR
22 //#define LOGGER_VERBOSITY_WARNING_ERROR
23 //#define LOGGER_VERBOSITY_INFO_WARNING_ERROR
24 //#define LOGGER_VERBOSITY_ALL
25 #define LOGGER_VERBOSITY_ALL
26 
27 #define SEND_MSG(msg, type) getLogger().sendMsg(msg, type, __FILE__, __LINE__)
28 
29 #ifdef LOGGER_VERBOSITY_ERROR
30 #define SEND_DEBUG_MSG(msg)
31 #define SEND_INFO_MSG(msg)
32 #define SEND_WARNING_MSG(msg)
33 #define SEND_ERROR_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR)
34 #define SEND_DEBUG_STREAM_MSG(msg)
35 #define SEND_INFO_STREAM_MSG(msg)
36 #define SEND_WARNING_STREAM_MSG(msg)
37 #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM)
38 #endif
39 
40 #ifdef LOGGER_VERBOSITY_WARNING_ERROR
41 #define SEND_DEBUG_MSG(msg)
42 #define SEND_INFO_MSG(msg)
43 #define SEND_WARNING_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING)
44 #define SEND_ERROR_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR)
45 #define SEND_DEBUG_STREAM_MSG(msg)
46 #define SEND_INFO_STREAM_MSG(msg) #define SEND_WARNING_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING_STREAM)
47 #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM)
48 #endif
49 
50 #ifdef LOGGER_VERBOSITY_INFO_WARNING_ERROR
51 #define SEND_DEBUG_MSG(msg)
52 #define SEND_INFO_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO)
53 #define SEND_WARNING_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING)
54 #define SEND_ERROR_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR)
55 #define SEND_DEBUG_STREAM_MSG(msg)
56 #define SEND_INFO_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO_STREAM)
57 #define SEND_WARNING_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING_STREAM)
58 #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM)
59 #endif
60 
61 #ifdef LOGGER_VERBOSITY_ALL
62 #define SEND_DEBUG_MSG(msg) SEND_MSG(msg, MSG_TYPE_DEBUG)
63 #define SEND_INFO_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO)
64 #define SEND_WARNING_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING)
65 #define SEND_ERROR_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR)
66 #define SEND_DEBUG_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_DEBUG_STREAM)
67 #define SEND_INFO_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO_STREAM)
68 #define SEND_WARNING_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING_STREAM)
69 #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM)
70 #endif
71 
74 enum CENTROIDAL_DYNAMICS_DLLAPI MsgType {
82  MSG_TYPE_ERROR_STREAM = 7
83 };
84 
85 template <typename T>
86 std::string toString(const T& v) {
87  std::stringstream ss;
88  ss << v;
89  return ss.str();
90 }
91 
92 template <typename T>
93 std::string toString(const std::vector<T>& v, const std::string separator = ", ") {
94  std::stringstream ss;
95  for (int i = 0; i < v.size() - 1; i++) ss << v[i] << separator;
96  ss << v[v.size() - 1];
97  return ss.str();
98 }
99 
100 template <typename T, int n>
101 std::string toString(const Eigen::MatrixBase<T>& v, const std::string separator = ", ") {
102  if (v.rows() > v.cols()) return toString(v.transpose(), separator);
103  std::stringstream ss;
104  ss << v;
105  return ss.str();
106 }
107 
108 enum CENTROIDAL_DYNAMICS_DLLAPI LoggerVerbosity {
113  VERBOSITY_NONE
114 };
115 
119  public:
121  Logger(double timeSample = 0.001, double streamPrintPeriod = 1.0);
122 
124  ~Logger() {}
125 
128  void countdown();
129 
135  void sendMsg(std::string msg, MsgType type, const char* file = "", int line = 0);
136 
139  bool setTimeSample(double t);
140 
142  bool setStreamPrintPeriod(double s);
143 
145  void setVerbosity(LoggerVerbosity lv);
146 
147  protected:
148  LoggerVerbosity m_lv;
149  double m_timeSample;
152 
154  std::map<std::string, double> m_stream_msg_counters;
155 
156  bool isStreamMsg(MsgType m) {
157  return m == MSG_TYPE_ERROR_STREAM || m == MSG_TYPE_DEBUG_STREAM || m == MSG_TYPE_INFO_STREAM ||
159  }
160 
161  bool isDebugMsg(MsgType m) { return m == MSG_TYPE_DEBUG_STREAM || m == MSG_TYPE_DEBUG; }
162 
163  bool isInfoMsg(MsgType m) { return m == MSG_TYPE_INFO_STREAM || m == MSG_TYPE_INFO; }
164 
165  bool isWarningMsg(MsgType m) { return m == MSG_TYPE_WARNING_STREAM || m == MSG_TYPE_WARNING; }
166 
167  bool isErrorMsg(MsgType m) { return m == MSG_TYPE_ERROR_STREAM || m == MSG_TYPE_ERROR; }
168 };
169 
171 Logger& getLogger();
172 
173 } // namespace centroidal_dynamics
174 
175 #endif // HPP_CENTROIDAL_DYNAMICS_LOGGER_HH
MSG_TYPE_INFO_STREAM
MSG_TYPE_INFO_STREAM
Definition: logger.hh:80
centroidal_dynamics::Logger::isStreamMsg
bool isStreamMsg(MsgType m)
Definition: logger.hh:156
VERBOSITY_WARNING_ERROR
VERBOSITY_WARNING_ERROR
Definition: logger.hh:111
centroidal_dynamics::Logger
Definition: logger.hh:118
VERBOSITY_INFO_WARNING_ERROR
VERBOSITY_INFO_WARNING_ERROR
Definition: logger.hh:110
centroidal_dynamics::Logger::~Logger
~Logger()
Definition: logger.hh:124
VERBOSITY_ALL
VERBOSITY_ALL
Definition: logger.hh:109
MSG_TYPE_DEBUG_STREAM
MSG_TYPE_DEBUG_STREAM
Definition: logger.hh:79
centroidal_dynamics::Logger::isWarningMsg
bool isWarningMsg(MsgType m)
Definition: logger.hh:165
MSG_TYPE_WARNING_STREAM
MSG_TYPE_WARNING_STREAM
Definition: logger.hh:81
CENTROIDAL_DYNAMICS_DLLAPI
#define CENTROIDAL_DYNAMICS_DLLAPI
Definition: local_config.hh:52
centroidal_dynamics::Logger::isErrorMsg
bool isErrorMsg(MsgType m)
Definition: logger.hh:167
local_config.hh
VERBOSITY_ERROR
VERBOSITY_ERROR
Definition: logger.hh:112
centroidal_dynamics::Logger::m_timeSample
double m_timeSample
verbosity of the logger
Definition: logger.hh:149
centroidal_dynamics::Logger::isDebugMsg
bool isDebugMsg(MsgType m)
Definition: logger.hh:161
centroidal_dynamics::Logger::m_printCountdown
double m_printCountdown
specify the time period of the stream prints
Definition: logger.hh:151
centroidal_dynamics::toString
std::string toString(const T &v)
Definition: logger.hh:86
centroidal_dynamics
Definition: centroidal_dynamics.hh:14
centroidal_dynamics::Logger::m_lv
LoggerVerbosity m_lv
Definition: logger.hh:148
MSG_TYPE_ERROR
MSG_TYPE_ERROR
Definition: logger.hh:78
MSG_TYPE_WARNING
MSG_TYPE_WARNING
Definition: logger.hh:77
MSG_TYPE_INFO
MSG_TYPE_INFO
Definition: logger.hh:76
MSG_TYPE_DEBUG
MSG_TYPE_DEBUG
Definition: logger.hh:75
centroidal_dynamics::getLogger
Logger & getLogger()
Definition: logger.cpp:21
centroidal_dynamics::Logger::m_streamPrintPeriod
double m_streamPrintPeriod
specify the period of call of the countdown method
Definition: logger.hh:150
centroidal_dynamics::Logger::m_stream_msg_counters
std::map< std::string, double > m_stream_msg_counters
every time this is < 0 (i.e. every _streamPrintPeriod sec) print stuff
Definition: logger.hh:154
centroidal_dynamics::Logger::isInfoMsg
bool isInfoMsg(MsgType m)
Definition: logger.hh:163