@@ -27,23 +27,28 @@ namespace dynamicgraph {
27
27
/* * Enum representing the different kind of messages.
28
28
*/
29
29
enum MsgType {
30
- MSG_TYPE_DEBUG = 0 ,
31
- MSG_TYPE_INFO = 1 ,
32
- MSG_TYPE_WARNING = 2 ,
33
- MSG_TYPE_ERROR = 3 ,
34
- MSG_TYPE_DEBUG_STREAM = 4 ,
35
- MSG_TYPE_INFO_STREAM = 5 ,
36
- MSG_TYPE_WARNING_STREAM = 6 ,
37
- MSG_TYPE_ERROR_STREAM = 7
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 <<0 , // 1
34
+ MSG_TYPE_INFO = 1 <<1 , // 2
35
+ MSG_TYPE_WARNING = 1 <<2 , // 4
36
+ MSG_TYPE_ERROR = 1 <<3 , // 8
37
+ MSG_TYPE_DEBUG_STREAM = MSG_TYPE_DEBUG | 1 <<4 , // 17
38
+ MSG_TYPE_INFO_STREAM = MSG_TYPE_INFO | 1 <<4 , // 18
39
+ MSG_TYPE_WARNING_STREAM = MSG_TYPE_WARNING | 1 <<4 , // 20
40
+ MSG_TYPE_ERROR_STREAM = MSG_TYPE_ERROR | 1 <<4 // 24
38
41
};
39
42
} // namespace dynamicgraph
40
43
41
44
/* --------------------------------------------------------------------- */
42
45
/* --- INCLUDE --------------------------------------------------------- */
43
46
/* --------------------------------------------------------------------- */
44
47
45
- #include " boost/assign.hpp"
48
+ #include < boost/assign.hpp>
49
+ #include < boost/preprocessor/stringize.hpp>
46
50
#include < dynamic-graph/linear-algebra.h>
51
+ #include < dynamic-graph/real-time-logger-def.h>
47
52
#include < fstream>
48
53
#include < iomanip> // std::setprecision
49
54
#include < map>
@@ -54,13 +59,36 @@ namespace dynamicgraph {
54
59
// #define LOGGER_VERBOSITY_INFO_WARNING_ERROR
55
60
#define LOGGER_VERBOSITY_ALL
56
61
57
- #define SEND_MSG (msg, type ) sendMsg(msg, type, __FILE__, __LINE__)
62
+ #define SEND_MSG (msg, type ) \
63
+ sendMsg (msg, type, __FILE__ " :" BOOST_PP_STRINGIZE(__LINE__))
58
64
59
65
#define SEND_DEBUG_STREAM_MSG (msg ) SEND_MSG(msg, MSG_TYPE_DEBUG_STREAM)
60
66
#define SEND_INFO_STREAM_MSG (msg ) SEND_MSG(msg, MSG_TYPE_INFO_STREAM)
61
67
#define SEND_WARNING_STREAM_MSG (msg ) SEND_MSG(msg, MSG_TYPE_WARNING_STREAM)
62
68
#define SEND_ERROR_STREAM_MSG (msg ) SEND_MSG(msg, MSG_TYPE_ERROR_STREAM)
63
69
70
+ #define _DYNAMIC_GRAPH_ENTITY_MSG (entity, type ) \
71
+ (entity).logger().stream(type, __FILE__ BOOST_PP_STRINGIZE (__LINE__))
72
+
73
+ #define DYNAMIC_GRAPH_ENTITY_DEBUG (entity ) \
74
+ _DYNAMIC_GRAPH_ENTITY_MSG (entity, MSG_TYPE_DEBUG)
75
+ #define DYNAMIC_GRAPH_ENTITY_INFO (entity ) \
76
+ _DYNAMIC_GRAPH_ENTITY_MSG (entity, MSG_TYPE_INFO)
77
+ #define DYNAMIC_GRAPH_ENTITY_WARNING (entity ) \
78
+ _DYNAMIC_GRAPH_ENTITY_MSG (entity, MSG_TYPE_WARNING)
79
+ #define DYNAMIC_GRAPH_ENTITY_ERROR (entity ) \
80
+ _DYNAMIC_GRAPH_ENTITY_MSG (entity, MSG_TYPE_ERROR)
81
+
82
+ #define DYNAMIC_GRAPH_ENTITY_DEBUG_STREAM (entity ) \
83
+ _DYNAMIC_GRAPH_ENTITY_MSG (entity, MSG_TYPE_DEBUG_STREAM)
84
+ #define DYNAMIC_GRAPH_ENTITY_INFO_STREAM (entity ) \
85
+ _DYNAMIC_GRAPH_ENTITY_MSG (entity, MSG_TYPE_INFO_STREAM)
86
+ #define DYNAMIC_GRAPH_ENTITY_WARNING_STREAM (entity ) \
87
+ _DYNAMIC_GRAPH_ENTITY_MSG (entity, MSG_TYPE_WARNING_STREAM)
88
+ #define DYNAMIC_GRAPH_ENTITY_ERROR_STREAM (entity ) \
89
+ _DYNAMIC_GRAPH_ENTITY_MSG (entity, MSG_TYPE_ERROR_STREAM)
90
+
91
+
64
92
template <typename T>
65
93
std::string toString (const T &v, const int precision = 3 ,
66
94
const int width = -1 ) {
@@ -111,11 +139,11 @@ std::string toString(const Eigen::MatrixBase<T> &v, const int precision = 3,
111
139
}
112
140
113
141
enum LoggerVerbosity {
114
- VERBOSITY_ALL,
115
- VERBOSITY_INFO_WARNING_ERROR,
116
- VERBOSITY_WARNING_ERROR,
117
- VERBOSITY_ERROR,
118
- VERBOSITY_NONE
142
+ VERBOSITY_ALL = MSG_TYPE_DEBUG ,
143
+ VERBOSITY_INFO_WARNING_ERROR = MSG_TYPE_INFO ,
144
+ VERBOSITY_WARNING_ERROR = MSG_TYPE_WARNING ,
145
+ VERBOSITY_ERROR = MSG_TYPE_ERROR ,
146
+ VERBOSITY_NONE = 0
119
147
};
120
148
121
149
// / \ingroup debug
@@ -139,8 +167,14 @@ enum LoggerVerbosity {
139
167
// / VERBOSITY_WARNING_ERROR;
140
168
// / entity.setLoggerVerbosityLevel(aLoggerVerbosityLevel);
141
169
// / ...
142
- // / std::string aMsg=aBaseMsg+" WARNING";
143
- // / entity.sendMsg(aMsg,dynamicgraph::MSG_TYPE_WARNING, __FILE__,__LINE__);
170
+ // / // using macros
171
+ // / DYNAMIC_GRAPH_ENTITY_WARNING(entity) << "your message\n";
172
+ // /
173
+ // / // or the equivalent code without macros:
174
+ // / // Please use '\n' instead of std::endl and flushing will have no effect
175
+ // / entity.logger.stream(dynamicgraph::MSG_TYPE_WARNING,
176
+ // / __FILE__ BOOST_PP_STRINGIZE(__LINE__))
177
+ // / << your message << '\n';
144
178
// /
145
179
// / \endcode
146
180
// /
@@ -157,13 +191,49 @@ class Logger {
157
191
* to decrement the internal Logger's counter. */
158
192
void countdown ();
159
193
194
+ /* * Check whether next message should be accepted.
195
+ * \note See Logger::stream to see how to use it.
196
+ * This will modify the counter associated to lineId as if it was
197
+ * published. It should thus be used in conjunction with Logger::stream.
198
+ */
199
+ bool acceptMsg (MsgType m, const std::string& lineId) {
200
+ if ((m & MSG_TYPE_TYPE_BITS) < m_lv)
201
+ return false ;
202
+
203
+ // if print is allowed by current verbosity level
204
+ if (isStreamMsg (m)) return checkStreamPeriod (lineId);
205
+ return true ;
206
+ }
207
+
208
+ /* * The most efficient logging method is
209
+ * \code
210
+ * if (logger.acceptMsg(type, lineId))
211
+ * logger.stream() << "my message\n";
212
+ * \endcode
213
+ */
214
+ RTLoggerStream stream () {
215
+ return ::dynamicgraph::RealTimeLogger::instance ().emptyStream ();
216
+ }
217
+
160
218
/* * Print the specified message on standard output if the verbosity level
161
- * allows it. The file name and the line number are used to identify
162
- * the point where sendMsg is called so that streaming messages are
163
- * printed only every streamPrintPeriod iterations.
219
+ * allows it. The lineId is used to identify the point where sendMsg is
220
+ * called so that streaming messages are printed only every streamPrintPeriod
221
+ * iterations.
222
+ * \param lineId typically __FILE__ ":" BOOST_PP_STRINGIZE(__LINE__)
164
223
*/
165
- void sendMsg (std::string msg, MsgType type, const char *file = " " ,
166
- int line = 0 );
224
+ RTLoggerStream stream (MsgType type, const std::string& lineId = " " ) {
225
+ RealTimeLogger &rtlogger = ::dynamicgraph::RealTimeLogger::instance ();
226
+ if (acceptMsg (type, lineId))
227
+ return rtlogger.front ();
228
+ return rtlogger.emptyStream ();
229
+ }
230
+
231
+ /* * \deprecated instead, use
232
+ * \code
233
+ * stream(type, lineId) << msg << '\n';
234
+ * \endcode
235
+ */
236
+ void sendMsg (std::string msg, MsgType type, const std::string& lineId = " " );
167
237
168
238
/* * Set the sampling time at which the method countdown()
169
239
* is going to be called. */
@@ -193,13 +263,13 @@ class Logger {
193
263
double m_printCountdown;
194
264
// / every time this is < 0 (i.e. every _streamPrintPeriod sec) print stuff
195
265
266
+ typedef std::map<std::string, double > StreamCounterMap_t;
196
267
/* * Pointer to the dynamic structure which holds
197
268
the collection of streaming messages */
198
- std::map<std::string, double > m_stream_msg_counters;
269
+ StreamCounterMap_t m_stream_msg_counters;
199
270
200
- bool isStreamMsg (MsgType m) {
201
- return m == MSG_TYPE_ERROR_STREAM || m == MSG_TYPE_DEBUG_STREAM ||
202
- m == MSG_TYPE_INFO_STREAM || m == MSG_TYPE_WARNING_STREAM;
271
+ inline bool isStreamMsg (MsgType m) {
272
+ return (m & MSG_TYPE_STREAM_BIT);
203
273
}
204
274
205
275
bool isDebugMsg (MsgType m) {
@@ -217,6 +287,8 @@ class Logger {
217
287
bool isErrorMsg (MsgType m) {
218
288
return m == MSG_TYPE_ERROR_STREAM || m == MSG_TYPE_ERROR;
219
289
}
290
+
291
+ bool checkStreamPeriod (const std::string& lineId);
220
292
};
221
293
222
294
} // namespace dynamicgraph
0 commit comments