debug-logger.cpp 5.55 KB
Newer Older
Olivier Stasse's avatar
Olivier Stasse committed
1
2
3
4
5
6
7
8
9
/* Copyright 2019, LAAS-CNRS
 *
 * Olivier Stasse
 *
 * See LICENSE file
 *
 */
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
10
11
12
13
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <iostream>
#include <sstream>
Olivier Stasse's avatar
Olivier Stasse committed
14
15
16

#define ENABLE_RT_LOG
#include <dynamic-graph/logger.h>
17
#include <dynamic-graph/real-time-logger.h>
Olivier Stasse's avatar
Olivier Stasse committed
18

Bergé's avatar
Bergé committed
19
#define BOOST_TEST_MODULE debug - logger
Olivier Stasse's avatar
Olivier Stasse committed
20
21

#include <boost/test/output_test_stream.hpp>
22
#include <boost/test/unit_test.hpp>
Olivier Stasse's avatar
Olivier Stasse committed
23
24
25

using boost::test_tools::output_test_stream;

Bergé's avatar
Bergé committed
26
27
namespace dynamicgraph {
class CustomEntity : public Entity {
28
public:
Bergé's avatar
Bergé committed
29
  static const std::string CLASS_NAME;
30
  virtual const std::string &getClassName() const { return CLASS_NAME; }
Olivier Stasse's avatar
Olivier Stasse committed
31
  explicit CustomEntity(const std::string &n) : Entity(n) {
Bergé's avatar
Bergé committed
32
33
    logger_.setTimeSample(0.001);
    logger_.setStreamPrintPeriod(0.005);
Joseph Mirabel's avatar
Joseph Mirabel committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

    logger_.setVerbosity(VERBOSITY_NONE);
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_NONE);
    BOOST_CHECK( logger_.stream(MSG_TYPE_DEBUG  ).isNull());
    BOOST_CHECK( logger_.stream(MSG_TYPE_INFO   ).isNull());
    BOOST_CHECK( logger_.stream(MSG_TYPE_WARNING).isNull());
    BOOST_CHECK( logger_.stream(MSG_TYPE_ERROR  ).isNull());

    logger_.setVerbosity(VERBOSITY_ERROR);
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ERROR);
    BOOST_CHECK( logger_.stream(MSG_TYPE_DEBUG  ).isNull());
    BOOST_CHECK( logger_.stream(MSG_TYPE_INFO   ).isNull());
    BOOST_CHECK( logger_.stream(MSG_TYPE_WARNING).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR  ).isNull());

    logger_.setVerbosity(VERBOSITY_WARNING_ERROR);
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_WARNING_ERROR);
    BOOST_CHECK( logger_.stream(MSG_TYPE_DEBUG  ).isNull());
    BOOST_CHECK( logger_.stream(MSG_TYPE_INFO   ).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR  ).isNull());

    logger_.setVerbosity(VERBOSITY_INFO_WARNING_ERROR);
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_INFO_WARNING_ERROR);
    BOOST_CHECK( logger_.stream(MSG_TYPE_DEBUG  ).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO   ).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR  ).isNull());

Bergé's avatar
Bergé committed
63
    logger_.setVerbosity(VERBOSITY_ALL);
Joseph Mirabel's avatar
Joseph Mirabel committed
64
65
66
67
68
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ALL);
    BOOST_CHECK(!logger_.stream(MSG_TYPE_DEBUG  ).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO   ).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR  ).isNull());
Bergé's avatar
Bergé committed
69
  }
Olivier Stasse's avatar
Olivier Stasse committed
70

Bergé's avatar
Bergé committed
71
72
  ~CustomEntity() {}
  void testDebugTrace() {
Joseph Mirabel's avatar
Joseph Mirabel committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    logger_.stream(MSG_TYPE_DEBUG)
      << "This is a message of level MSG_TYPE_DEBUG\n";
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_INFO)
      << "This is a message of level MSG_TYPE_INFO\n";
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_WARNING)
      << "This is a message of level MSG_TYPE_WARNING\n";
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_ERROR)
      << "This is a message of level MSG_TYPE_ERROR\n";
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_DEBUG_STREAM)
      << "This is a message of level MSG_TYPE_DEBUG_STREAM\n";
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_INFO_STREAM)
      << "This is a message of level MSG_TYPE_INFO_STREAM\n";
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_WARNING_STREAM)
      << "This is a message of level MSG_TYPE_WARNING_STREAM\n";
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_ERROR_STREAM)
      << "This is a message of level MSG_TYPE_ERROR_STREAM\n";
96
    /* Add test toString */
Joseph Mirabel's avatar
Joseph Mirabel committed
97
    dynamicgraph::RealTimeLogger::instance().spinOnce();
Olivier Stasse's avatar
Olivier Stasse committed
98
    double q = 1.0;
Joseph Mirabel's avatar
Joseph Mirabel committed
99
100
    logger_.stream() << "Value to display: " + toString(q) << '\n';
    dynamicgraph::RealTimeLogger::instance().spinOnce();
101
102
    std::vector<double> vq;
    vq.resize(3);
Olivier Stasse's avatar
Olivier Stasse committed
103
104
105
    vq[0] = 1.0;
    vq[1] = 2.0;
    vq[2] = 3.0;
Joseph Mirabel's avatar
Joseph Mirabel committed
106
107
108
109
    logger_.stream(MSG_TYPE_INFO) << "Value to display: " << toString(vq) << '\n';
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_INFO) << "Value to display: " << toString(vq, 3, 10) << '\n';
    dynamicgraph::RealTimeLogger::instance().spinOnce();
Olivier Stasse's avatar
Olivier Stasse committed
110
    Eigen::Matrix<double, 3, 3> an_eig_m;
Joseph Mirabel's avatar
Joseph Mirabel committed
111
112
113
    an_eig_m.setOnes();
    logger_.stream(MSG_TYPE_INFO) << "Value to display: " << toString(an_eig_m) << '\n';
    dynamicgraph::RealTimeLogger::instance().spinOnce();
Bergé's avatar
Bergé committed
114
115
116
117
    logger_.countdown();
  }
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
118
} // namespace dynamicgraph
Olivier Stasse's avatar
Olivier Stasse committed
119

Bergé's avatar
Bergé committed
120
BOOST_AUTO_TEST_CASE(debug_logger) {
Olivier Stasse's avatar
Olivier Stasse committed
121
  std::ofstream of;
122
  dynamicgraph::RealTimeLogger::instance();
Bergé's avatar
Bergé committed
123
124
  of.open("/tmp/dg-LOGS.txt", std::ofstream::out | std::ofstream::app);
  dgADD_OSTREAM_TO_RTLOG(of);
125

Bergé's avatar
Bergé committed
126
  BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
127

128
129
130
131
  dynamicgraph::CustomEntity &entity =
      *(dynamic_cast<dynamicgraph::CustomEntity *>(
          dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
                                                                 "my-entity")));
Olivier Stasse's avatar
Olivier Stasse committed
132

133
  entity.setTimeSample(0.002);
Bergé's avatar
Bergé committed
134
  BOOST_CHECK_EQUAL(entity.getTimeSample(), 0.002);
Joseph Mirabel's avatar
Joseph Mirabel committed
135
136
  entity.setStreamPrintPeriod(0.002);
  BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.002);
137

Bergé's avatar
Bergé committed
138
139
140
  for (unsigned int i = 0; i < 10000; i++) {
    entity.testDebugTrace();
  }
141
142

  dynamicgraph::RealTimeLogger::destroy();
Olivier Stasse's avatar
Olivier Stasse committed
143
}