debug-logger.cpp
Go to the documentation of this file.
1 /* Copyright 2019, LAAS-CNRS
2  *
3  * Olivier Stasse
4  *
5  * See LICENSE file
6  *
7  */
8 #include <dynamic-graph/entity.h>
10 
11 #include <iostream>
12 #include <sstream>
13 
14 #include "dynamic-graph/factory.h"
15 #include "dynamic-graph/pool.h"
16 
17 #define ENABLE_RT_LOG
18 #include <dynamic-graph/logger.h>
20 
21 #define BOOST_TEST_MODULE debug - logger
22 
23 #if BOOST_VERSION >= 105900
24 #include <boost/test/tools/output_test_stream.hpp>
25 #else
26 #include <boost/test/output_test_stream.hpp>
27 #endif
28 #include <boost/test/unit_test.hpp>
29 
30 using boost::test_tools::output_test_stream;
31 
32 namespace dynamicgraph {
33 class CustomEntity : public Entity {
34  public:
35  static const std::string CLASS_NAME;
36  virtual const std::string &getClassName() const { return CLASS_NAME; }
37  explicit CustomEntity(const std::string &n) : Entity(n) {
38  logger_.setTimeSample(0.001);
40 
42  BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_NONE);
43  BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
44  BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
45  BOOST_CHECK(logger_.stream(MSG_TYPE_WARNING).isNull());
46  BOOST_CHECK(logger_.stream(MSG_TYPE_ERROR).isNull());
47 
49  BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ERROR);
50  BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
51  BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
52  BOOST_CHECK(logger_.stream(MSG_TYPE_WARNING).isNull());
53  BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
54 
56  BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_WARNING_ERROR);
57  BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
58  BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
59  BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
60  BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
61 
64  BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
65  BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO).isNull());
66  BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
67  BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
68 
70  BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ALL);
71  BOOST_CHECK(!logger_.stream(MSG_TYPE_DEBUG).isNull());
72  BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO).isNull());
73  BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
74  BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
75  }
76 
78  void testDebugTrace() {
80  << "This is a message of level MSG_TYPE_DEBUG\n";
83  << "This is a message of level MSG_TYPE_INFO\n";
86  << "This is a message of level MSG_TYPE_WARNING\n";
89  << "This is a message of level MSG_TYPE_ERROR\n";
92  << "This is a message of level MSG_TYPE_DEBUG_STREAM\n";
95  << "This is a message of level MSG_TYPE_INFO_STREAM\n";
98  << "This is a message of level MSG_TYPE_WARNING_STREAM\n";
101  << "This is a message of level MSG_TYPE_ERROR_STREAM\n";
102  /* Add test toString */
104  double q = 1.0;
105  logger_.stream() << "Value to display: " + toString(q) << '\n';
107  std::vector<double> vq;
108  vq.resize(3);
109  vq[0] = 1.0;
110  vq[1] = 2.0;
111  vq[2] = 3.0;
113  << "Value to display: " << toString(vq) << '\n';
116  << "Value to display: " << toString(vq, 3, 10) << '\n';
118  Eigen::Matrix<double, 3, 3> an_eig_m;
119  an_eig_m.setOnes();
121  << "Value to display: " << toString(an_eig_m) << '\n';
123  logger_.countdown();
124  }
125 };
127 } // namespace dynamicgraph
128 
129 BOOST_AUTO_TEST_CASE(debug_logger) {
130  std::ofstream of;
132  of.open("/tmp/dg-LOGS.txt", std::ofstream::out | std::ofstream::app);
134 
135  BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
136 
138  *(dynamic_cast<dynamicgraph::CustomEntity *>(
140  "my-entity")));
141 
142  entity.setTimeSample(0.002);
143  BOOST_CHECK_EQUAL(entity.getTimeSample(), 0.002);
144  entity.setStreamPrintPeriod(0.002);
145  BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.002);
146 
147  for (unsigned int i = 0; i < 10000; i++) {
148  entity.testDebugTrace();
149  }
150 
152 }
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
#define dgADD_OSTREAM_TO_RTLOG(ostr)
RTLoggerStream stream()
Definition: logger.h:200
bool setTimeSample(double t)
Set the time sample.
BOOST_AUTO_TEST_CASE(debug_logger)
static const std::string CLASS_NAME
virtual const std::string & getClassName() const
double getTimeSample()
Get the time sample.
bool setTimeSample(double t)
Definition: logger.cpp:53
void setVerbosity(LoggerVerbosity lv)
Definition: logger.cpp:34
bool setStreamPrintPeriod(double s)
Definition: logger.cpp:59
std::string toString(const T &v, const int precision=3, const int width=-1)
Definition: logger.h:95
CustomEntity(const std::string &n)
double getStreamPrintPeriod()
Get the period of the stream period.
Entity(const std::string &name)
LoggerVerbosity getVerbosity()
Definition: logger.cpp:36
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity")
bool setStreamPrintPeriod(double t)
Set the period of the stream period.


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03