Skip to content

Commit eb1f332

Browse files
jmirabelOlivier Stasse
authored andcommitted
Fix bugs related to loggers.
1 parent e4d3adf commit eb1f332

File tree

3 files changed

+87
-36
lines changed

3 files changed

+87
-36
lines changed

include/dynamic-graph/logger.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ namespace dynamicgraph {
2727
/** Enum representing the different kind of messages.
2828
*/
2929
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<<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
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
4141
};
4242
} // namespace dynamicgraph
4343

@@ -182,7 +182,9 @@ enum LoggerVerbosity {
182182
///
183183
/// \endcode
184184
///
185-
///
185+
/// \todo remove m_timeSample and streamPrintPeriod to rather use a simple
186+
/// integer counting the number of calls. This will achieve exactly the
187+
/// same behaviour without rouding numerical errors.
186188
class Logger {
187189
public:
188190
/** Constructor */
@@ -198,7 +200,7 @@ class Logger {
198200
/** Get an output stream independently of the debug level.
199201
*/
200202
RTLoggerStream stream() {
201-
return ::dynamicgraph::RealTimeLogger::instance().emptyStream();
203+
return ::dynamicgraph::RealTimeLogger::instance().front();
202204
}
203205

204206
/** Print the specified message on standard output if the verbosity level
@@ -271,7 +273,8 @@ class Logger {
271273
* is updated.
272274
*/
273275
bool acceptMsg (MsgType m, const std::string& lineId) {
274-
if ((m & MSG_TYPE_TYPE_BITS) < m_lv)
276+
// If more verbose than the current verbosity level
277+
if ((m & MSG_TYPE_TYPE_BITS) > m_lv)
275278
return false;
276279

277280
// if print is allowed by current verbosity level

src/debug/logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ bool Logger::checkStreamPeriod(const std::string &lineId) {
7676

7777
// if counter is greater than 0 then decrement it and do not print
7878
double &counter = result.first->second;
79+
counter -= m_timeSample;
7980
if (counter > 0.0) {
80-
counter -= m_timeSample;
8181
return false;
8282
} else // otherwise reset counter and print
8383
counter = m_streamPrintPeriod;

tests/debug-logger.cpp

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,86 @@ class CustomEntity : public Entity {
3131
explicit CustomEntity(const std::string &n) : Entity(n) {
3232
logger_.setTimeSample(0.001);
3333
logger_.setStreamPrintPeriod(0.005);
34+
35+
logger_.setVerbosity(VERBOSITY_NONE);
36+
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_NONE);
37+
BOOST_CHECK( logger_.stream(MSG_TYPE_DEBUG ).isNull());
38+
BOOST_CHECK( logger_.stream(MSG_TYPE_INFO ).isNull());
39+
BOOST_CHECK( logger_.stream(MSG_TYPE_WARNING).isNull());
40+
BOOST_CHECK( logger_.stream(MSG_TYPE_ERROR ).isNull());
41+
42+
logger_.setVerbosity(VERBOSITY_ERROR);
43+
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ERROR);
44+
BOOST_CHECK( logger_.stream(MSG_TYPE_DEBUG ).isNull());
45+
BOOST_CHECK( logger_.stream(MSG_TYPE_INFO ).isNull());
46+
BOOST_CHECK( logger_.stream(MSG_TYPE_WARNING).isNull());
47+
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR ).isNull());
48+
49+
logger_.setVerbosity(VERBOSITY_WARNING_ERROR);
50+
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_WARNING_ERROR);
51+
BOOST_CHECK( logger_.stream(MSG_TYPE_DEBUG ).isNull());
52+
BOOST_CHECK( logger_.stream(MSG_TYPE_INFO ).isNull());
53+
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
54+
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR ).isNull());
55+
56+
logger_.setVerbosity(VERBOSITY_INFO_WARNING_ERROR);
57+
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_INFO_WARNING_ERROR);
58+
BOOST_CHECK( logger_.stream(MSG_TYPE_DEBUG ).isNull());
59+
BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO ).isNull());
60+
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
61+
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR ).isNull());
62+
3463
logger_.setVerbosity(VERBOSITY_ALL);
35-
LoggerVerbosity alv = logger_.getVerbosity();
36-
BOOST_CHECK(alv == VERBOSITY_ALL);
64+
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ALL);
65+
BOOST_CHECK(!logger_.stream(MSG_TYPE_DEBUG ).isNull());
66+
BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO ).isNull());
67+
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
68+
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR ).isNull());
3769
}
3870

3971
~CustomEntity() {}
4072
void testDebugTrace() {
41-
sendMsg("This is a message of level MSG_TYPE_DEBUG", MSG_TYPE_DEBUG);
42-
sendMsg("This is a message of level MSG_TYPE_INFO", MSG_TYPE_INFO);
43-
sendMsg("This is a message of level MSG_TYPE_WARNING", MSG_TYPE_WARNING);
44-
sendMsg("This is a message of level MSG_TYPE_ERROR", MSG_TYPE_ERROR);
45-
sendMsg("This is a message of level MSG_TYPE_DEBUG_STREAM",
46-
MSG_TYPE_DEBUG_STREAM);
47-
sendMsg("This is a message of level MSG_TYPE_INFO_STREAM",
48-
MSG_TYPE_INFO_STREAM);
49-
sendMsg("This is a message of level MSG_TYPE_WARNING_STREAM",
50-
MSG_TYPE_WARNING_STREAM);
51-
sendMsg("This is a message of level MSG_TYPE_ERROR_STREAM",
52-
MSG_TYPE_ERROR_STREAM);
73+
logger_.stream(MSG_TYPE_DEBUG)
74+
<< "This is a message of level MSG_TYPE_DEBUG\n";
75+
dynamicgraph::RealTimeLogger::instance().spinOnce();
76+
logger_.stream(MSG_TYPE_INFO)
77+
<< "This is a message of level MSG_TYPE_INFO\n";
78+
dynamicgraph::RealTimeLogger::instance().spinOnce();
79+
logger_.stream(MSG_TYPE_WARNING)
80+
<< "This is a message of level MSG_TYPE_WARNING\n";
81+
dynamicgraph::RealTimeLogger::instance().spinOnce();
82+
logger_.stream(MSG_TYPE_ERROR)
83+
<< "This is a message of level MSG_TYPE_ERROR\n";
84+
dynamicgraph::RealTimeLogger::instance().spinOnce();
85+
logger_.stream(MSG_TYPE_DEBUG_STREAM)
86+
<< "This is a message of level MSG_TYPE_DEBUG_STREAM\n";
87+
dynamicgraph::RealTimeLogger::instance().spinOnce();
88+
logger_.stream(MSG_TYPE_INFO_STREAM)
89+
<< "This is a message of level MSG_TYPE_INFO_STREAM\n";
90+
dynamicgraph::RealTimeLogger::instance().spinOnce();
91+
logger_.stream(MSG_TYPE_WARNING_STREAM)
92+
<< "This is a message of level MSG_TYPE_WARNING_STREAM\n";
93+
dynamicgraph::RealTimeLogger::instance().spinOnce();
94+
logger_.stream(MSG_TYPE_ERROR_STREAM)
95+
<< "This is a message of level MSG_TYPE_ERROR_STREAM\n";
5396
/* Add test toString */
97+
dynamicgraph::RealTimeLogger::instance().spinOnce();
5498
double q = 1.0;
55-
sendMsg("Value to display: " + toString(q));
99+
logger_.stream() << "Value to display: " + toString(q) << '\n';
100+
dynamicgraph::RealTimeLogger::instance().spinOnce();
56101
std::vector<double> vq;
57102
vq.resize(3);
58103
vq[0] = 1.0;
59104
vq[1] = 2.0;
60105
vq[2] = 3.0;
61-
sendMsg("Value to display: " + toString(vq));
62-
sendMsg("Value to display: " + toString(vq, 3, 10));
106+
logger_.stream(MSG_TYPE_INFO) << "Value to display: " << toString(vq) << '\n';
107+
dynamicgraph::RealTimeLogger::instance().spinOnce();
108+
logger_.stream(MSG_TYPE_INFO) << "Value to display: " << toString(vq, 3, 10) << '\n';
109+
dynamicgraph::RealTimeLogger::instance().spinOnce();
63110
Eigen::Matrix<double, 3, 3> an_eig_m;
64-
an_eig_m.Ones();
65-
sendMsg("Value to display: " + toString(an_eig_m));
111+
an_eig_m.setOnes();
112+
logger_.stream(MSG_TYPE_INFO) << "Value to display: " << toString(an_eig_m) << '\n';
113+
dynamicgraph::RealTimeLogger::instance().spinOnce();
66114
logger_.countdown();
67115
}
68116
};
@@ -84,8 +132,8 @@ BOOST_AUTO_TEST_CASE(debug_logger) {
84132

85133
entity.setTimeSample(0.002);
86134
BOOST_CHECK_EQUAL(entity.getTimeSample(), 0.002);
87-
entity.setStreamPrintPeriod(0.004);
88-
BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.004);
135+
entity.setStreamPrintPeriod(0.002);
136+
BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.002);
89137

90138
for (unsigned int i = 0; i < 10000; i++) {
91139
entity.testDebugTrace();

0 commit comments

Comments
 (0)