| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 13 | #include <Eigen/Dense> | ||
| 14 | #include <hpp/centroidal-dynamics/local_config.hh> | ||
| 15 | #include <map> | ||
| 16 | #include <sstream> | ||
| 17 | |||
| 18 | #include "boost/assign.hpp" | ||
| 19 | |||
| 20 | namespace centroidal_dynamics { | ||
| 21 | |||
| 22 | // #define LOGGER_VERBOSITY_ERROR | ||
| 23 | // #define LOGGER_VERBOSITY_WARNING_ERROR | ||
| 24 | // #define LOGGER_VERBOSITY_INFO_WARNING_ERROR | ||
| 25 | // #define LOGGER_VERBOSITY_ALL | ||
| 26 | #define LOGGER_VERBOSITY_ALL | ||
| 27 | |||
| 28 | #define SEND_MSG(msg, type) getLogger().sendMsg(msg, type, __FILE__, __LINE__) | ||
| 29 | |||
| 30 | #ifdef LOGGER_VERBOSITY_ERROR | ||
| 31 | #define SEND_DEBUG_MSG(msg) | ||
| 32 | #define SEND_INFO_MSG(msg) | ||
| 33 | #define SEND_WARNING_MSG(msg) | ||
| 34 | #define SEND_ERROR_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR) | ||
| 35 | #define SEND_DEBUG_STREAM_MSG(msg) | ||
| 36 | #define SEND_INFO_STREAM_MSG(msg) | ||
| 37 | #define SEND_WARNING_STREAM_MSG(msg) | ||
| 38 | #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM) | ||
| 39 | #endif | ||
| 40 | |||
| 41 | #ifdef LOGGER_VERBOSITY_WARNING_ERROR | ||
| 42 | #define SEND_DEBUG_MSG(msg) | ||
| 43 | #define SEND_INFO_MSG(msg) | ||
| 44 | #define SEND_WARNING_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING) | ||
| 45 | #define SEND_ERROR_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR) | ||
| 46 | #define SEND_DEBUG_STREAM_MSG(msg) | ||
| 47 | #define SEND_INFO_STREAM_MSG(msg) \ | ||
| 48 | #define SEND_WARNING_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING_STREAM) | ||
| 49 | #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM) | ||
| 50 | #endif | ||
| 51 | |||
| 52 | #ifdef LOGGER_VERBOSITY_INFO_WARNING_ERROR | ||
| 53 | #define SEND_DEBUG_MSG(msg) | ||
| 54 | #define SEND_INFO_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO) | ||
| 55 | #define SEND_WARNING_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING) | ||
| 56 | #define SEND_ERROR_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR) | ||
| 57 | #define SEND_DEBUG_STREAM_MSG(msg) | ||
| 58 | #define SEND_INFO_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO_STREAM) | ||
| 59 | #define SEND_WARNING_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING_STREAM) | ||
| 60 | #define SEND_ERROR_STREAM_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM) | ||
| 61 | #endif | ||
| 62 | |||
| 63 | #ifdef LOGGER_VERBOSITY_ALL | ||
| 64 | #define SEND_DEBUG_MSG(msg) SEND_MSG(msg, MSG_TYPE_DEBUG) | ||
| 65 | #define SEND_INFO_MSG(msg) SEND_MSG(msg, MSG_TYPE_INFO) | ||
| 66 | #define SEND_WARNING_MSG(msg) SEND_MSG(msg, MSG_TYPE_WARNING) | ||
| 67 | #define SEND_ERROR_MSG(msg) SEND_MSG(msg, MSG_TYPE_ERROR) | ||
| 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 | #endif | ||
| 73 | |||
| 74 | /** Enum representing the different kind of messages. | ||
| 75 | */ | ||
| 76 | enum CENTROIDAL_DYNAMICS_DLLAPI MsgType { | ||
| 77 | MSG_TYPE_DEBUG = 0, | ||
| 78 | MSG_TYPE_INFO = 1, | ||
| 79 | MSG_TYPE_WARNING = 2, | ||
| 80 | MSG_TYPE_ERROR = 3, | ||
| 81 | MSG_TYPE_DEBUG_STREAM = 4, | ||
| 82 | MSG_TYPE_INFO_STREAM = 5, | ||
| 83 | MSG_TYPE_WARNING_STREAM = 6, | ||
| 84 | MSG_TYPE_ERROR_STREAM = 7 | ||
| 85 | }; | ||
| 86 | |||
| 87 | template <typename T> | ||
| 88 | 120 | std::string toString(const T& v) { | |
| 89 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
120 | std::stringstream ss; |
| 90 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
120 | ss << v; |
| 91 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
240 | return ss.str(); |
| 92 | 120 | } | |
| 93 | |||
| 94 | template <typename T> | ||
| 95 | std::string toString(const std::vector<T>& v, | ||
| 96 | const std::string separator = ", ") { | ||
| 97 | std::stringstream ss; | ||
| 98 | for (int i = 0; i < v.size() - 1; i++) ss << v[i] << separator; | ||
| 99 | ss << v[v.size() - 1]; | ||
| 100 | return ss.str(); | ||
| 101 | } | ||
| 102 | |||
| 103 | template <typename T, int n> | ||
| 104 | std::string toString(const Eigen::MatrixBase<T>& v, | ||
| 105 | const std::string separator = ", ") { | ||
| 106 | if (v.rows() > v.cols()) return toString(v.transpose(), separator); | ||
| 107 | std::stringstream ss; | ||
| 108 | ss << v; | ||
| 109 | return ss.str(); | ||
| 110 | } | ||
| 111 | |||
| 112 | enum CENTROIDAL_DYNAMICS_DLLAPI LoggerVerbosity { | ||
| 113 | VERBOSITY_ALL, | ||
| 114 | VERBOSITY_INFO_WARNING_ERROR, | ||
| 115 | VERBOSITY_WARNING_ERROR, | ||
| 116 | VERBOSITY_ERROR, | ||
| 117 | VERBOSITY_NONE | ||
| 118 | }; | ||
| 119 | |||
| 120 | /** A simple class for logging messages | ||
| 121 | */ | ||
| 122 | class CENTROIDAL_DYNAMICS_DLLAPI Logger { | ||
| 123 | public: | ||
| 124 | /** Constructor */ | ||
| 125 | Logger(double timeSample = 0.001, double streamPrintPeriod = 1.0); | ||
| 126 | |||
| 127 | /** Destructor */ | ||
| 128 | ✗ | ~Logger() {} | |
| 129 | |||
| 130 | /** Method to be called at every control iteration | ||
| 131 | * to decrement the internal Logger's counter. */ | ||
| 132 | void countdown(); | ||
| 133 | |||
| 134 | /** Print the specified message on standard output if the verbosity level | ||
| 135 | * allows it. The file name and the line number are used to identify | ||
| 136 | * the point where sendMsg is called so that streaming messages are | ||
| 137 | * printed only every streamPrintPeriod iterations. | ||
| 138 | */ | ||
| 139 | void sendMsg(std::string msg, MsgType type, const char* file = "", | ||
| 140 | int line = 0); | ||
| 141 | |||
| 142 | /** Set the sampling time at which the method countdown() | ||
| 143 | * is going to be called. */ | ||
| 144 | bool setTimeSample(double t); | ||
| 145 | |||
| 146 | /** Set the time period for printing of streaming messages. */ | ||
| 147 | bool setStreamPrintPeriod(double s); | ||
| 148 | |||
| 149 | /** Set the verbosity level of the logger. */ | ||
| 150 | void setVerbosity(LoggerVerbosity lv); | ||
| 151 | |||
| 152 | protected: | ||
| 153 | LoggerVerbosity m_lv; /// verbosity of the logger | ||
| 154 | double m_timeSample; /// specify the period of call of the countdown method | ||
| 155 | double m_streamPrintPeriod; /// specify the time period of the stream prints | ||
| 156 | double m_printCountdown; /// every time this is < 0 (i.e. every | ||
| 157 | /// _streamPrintPeriod sec) print stuff | ||
| 158 | |||
| 159 | /** Pointer to the dynamic structure which holds the collection of streaming | ||
| 160 | * messages */ | ||
| 161 | std::map<std::string, double> m_stream_msg_counters; | ||
| 162 | |||
| 163 | ✗ | bool isStreamMsg(MsgType m) { | |
| 164 | ✗ | return m == MSG_TYPE_ERROR_STREAM || m == MSG_TYPE_DEBUG_STREAM || | |
| 165 | ✗ | m == MSG_TYPE_INFO_STREAM || m == MSG_TYPE_WARNING_STREAM; | |
| 166 | } | ||
| 167 | |||
| 168 | bool isDebugMsg(MsgType m) { | ||
| 169 | return m == MSG_TYPE_DEBUG_STREAM || m == MSG_TYPE_DEBUG; | ||
| 170 | } | ||
| 171 | |||
| 172 | ✗ | bool isInfoMsg(MsgType m) { | |
| 173 | ✗ | return m == MSG_TYPE_INFO_STREAM || m == MSG_TYPE_INFO; | |
| 174 | } | ||
| 175 | |||
| 176 | ✗ | bool isWarningMsg(MsgType m) { | |
| 177 | ✗ | return m == MSG_TYPE_WARNING_STREAM || m == MSG_TYPE_WARNING; | |
| 178 | } | ||
| 179 | |||
| 180 | ✗ | bool isErrorMsg(MsgType m) { | |
| 181 | ✗ | return m == MSG_TYPE_ERROR_STREAM || m == MSG_TYPE_ERROR; | |
| 182 | } | ||
| 183 | }; | ||
| 184 | |||
| 185 | /** Method to get the logger (singleton). */ | ||
| 186 | Logger& getLogger(); | ||
| 187 | |||
| 188 | } // namespace centroidal_dynamics | ||
| 189 | |||
| 190 | #endif // HPP_CENTROIDAL_DYNAMICS_LOGGER_HH | ||
| 191 |