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 }
dynamicgraph::toString
std::string toString(const T &v, const int precision=3, const int width=-1)
Definition: logger.h:95
entity.h
dynamicgraph::Logger::setTimeSample
bool setTimeSample(double t)
Definition: logger.cpp:53
dynamicgraph::Logger::countdown
void countdown()
Definition: logger.cpp:37
dynamicgraph::CustomEntity::getClassName
virtual const std::string & getClassName() const
Definition: debug-logger.cpp:36
dynamicgraph::Logger::setStreamPrintPeriod
bool setStreamPrintPeriod(double s)
Definition: logger.cpp:59
dynamicgraph::MSG_TYPE_DEBUG_STREAM
@ MSG_TYPE_DEBUG_STREAM
Definition: logger.h:37
logger.h
dynamicgraph
dynamicgraph::CustomEntity::~CustomEntity
~CustomEntity()
Definition: debug-logger.cpp:77
dynamicgraph::Logger::setVerbosity
void setVerbosity(LoggerVerbosity lv)
Definition: logger.cpp:34
dynamicgraph::Entity
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
Definition: include/dynamic-graph/entity.h:52
dynamicgraph::Entity::setStreamPrintPeriod
bool setStreamPrintPeriod(double t)
Set the period of the stream period.
Definition: include/dynamic-graph/entity.h:156
dynamicgraph::MSG_TYPE_WARNING_STREAM
@ MSG_TYPE_WARNING_STREAM
Definition: logger.h:39
dynamicgraph::RealTimeLogger::spinOnce
bool spinOnce()
Definition: src/debug/real-time-logger.cpp:29
dynamicgraph::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity")
dynamicgraph::CustomEntity
Definition: command-test.cpp:36
dynamicgraph::VERBOSITY_INFO_WARNING_ERROR
@ VERBOSITY_INFO_WARNING_ERROR
Definition: logger.h:145
dynamicgraph::MSG_TYPE_WARNING
@ MSG_TYPE_WARNING
Definition: logger.h:35
dynamicgraph::MSG_TYPE_INFO
@ MSG_TYPE_INFO
Definition: logger.h:34
dynamicgraph::CustomEntity::CustomEntity
CustomEntity(const std::string &n)
Definition: debug-logger.cpp:37
dynamicgraph::MSG_TYPE_ERROR
@ MSG_TYPE_ERROR
Definition: logger.h:36
CustomEntity
Definition: custom-entity.cpp:22
dynamicgraph::CustomEntity::testDebugTrace
void testDebugTrace()
Definition: debug-logger.cpp:78
dynamicgraph::Logger::getVerbosity
LoggerVerbosity getVerbosity()
Definition: logger.cpp:36
dynamicgraph::Entity::setTimeSample
bool setTimeSample(double t)
Set the time sample.
Definition: include/dynamic-graph/entity.h:150
dynamicgraph::VERBOSITY_WARNING_ERROR
@ VERBOSITY_WARNING_ERROR
Definition: logger.h:146
dynamicgraph::Entity::logger_
Logger logger_
Definition: include/dynamic-graph/entity.h:177
dynamicgraph::Entity::Entity
Entity(const std::string &name)
Definition: src/dgraph/entity.cpp:33
dynamicgraph::MSG_TYPE_ERROR_STREAM
@ MSG_TYPE_ERROR_STREAM
Definition: logger.h:40
exception-factory.h
factory.h
dynamicgraph::MSG_TYPE_DEBUG
@ MSG_TYPE_DEBUG
Definition: logger.h:33
dynamicgraph::CustomEntity::CLASS_NAME
static const std::string CLASS_NAME
Definition: command-test.cpp:38
dynamicgraph::VERBOSITY_ALL
@ VERBOSITY_ALL
Definition: logger.h:144
dynamicgraph::RealTimeLogger::destroy
static void destroy()
Definition: src/debug/real-time-logger.cpp:129
dynamicgraph::Logger::stream
RTLoggerStream stream()
Definition: logger.h:200
dynamicgraph::VERBOSITY_ERROR
@ VERBOSITY_ERROR
Definition: logger.h:147
dynamicgraph::VERBOSITY_NONE
@ VERBOSITY_NONE
Definition: logger.h:148
dynamicgraph::Entity::getTimeSample
double getTimeSample()
Get the time sample.
Definition: include/dynamic-graph/entity.h:153
real-time-logger.h
dgADD_OSTREAM_TO_RTLOG
#define dgADD_OSTREAM_TO_RTLOG(ostr)
Definition: real-time-logger.h:16
dynamicgraph::RealTimeLogger::instance
static RealTimeLogger & instance()
Definition: src/debug/real-time-logger.cpp:121
dynamicgraph::Entity::getStreamPrintPeriod
double getStreamPrintPeriod()
Get the period of the stream period.
Definition: include/dynamic-graph/entity.h:161
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(debug_logger)
Definition: debug-logger.cpp:129
dynamicgraph::MSG_TYPE_INFO_STREAM
@ MSG_TYPE_INFO_STREAM
Definition: logger.h:38
dynamicgraph::FactoryStorage::newEntity
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
Definition: src/dgraph/factory.cpp:79
pool.h
dynamicgraph::FactoryStorage::getInstance
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
Definition: src/dgraph/factory.cpp:15


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Thu Jun 13 2024 02:26:21