debug-logger.cpp 5.54 KB
Newer Older
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>
14 15 16

#define ENABLE_RT_LOG
#include <dynamic-graph/logger.h>
17
#include <dynamic-graph/real-time-logger.h>
18

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

#include <boost/test/output_test_stream.hpp>
22
#include <boost/test/unit_test.hpp>
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);
34 35 36

    logger_.setVerbosity(VERBOSITY_NONE);
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_NONE);
Olivier Stasse's avatar
Olivier Stasse committed
37 38 39 40
    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());
41 42 43

    logger_.setVerbosity(VERBOSITY_ERROR);
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ERROR);
Olivier Stasse's avatar
Olivier Stasse committed
44 45 46 47
    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());
48 49 50

    logger_.setVerbosity(VERBOSITY_WARNING_ERROR);
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_WARNING_ERROR);
Olivier Stasse's avatar
Olivier Stasse committed
51 52
    BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
    BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
53
    BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
Olivier Stasse's avatar
Olivier Stasse committed
54
    BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
55 56 57

    logger_.setVerbosity(VERBOSITY_INFO_WARNING_ERROR);
    BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_INFO_WARNING_ERROR);
Olivier Stasse's avatar
Olivier Stasse committed
58 59
    BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
    BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO).isNull());
60
    BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
Olivier Stasse's avatar
Olivier Stasse committed
61
    BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
62

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

Bergé's avatar
Bergé committed
71 72
  ~CustomEntity() {}
  void testDebugTrace() {
73
    logger_.stream(MSG_TYPE_DEBUG)
Olivier Stasse's avatar
Olivier Stasse committed
74
        << "This is a message of level MSG_TYPE_DEBUG\n";
75 76
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_INFO)
Olivier Stasse's avatar
Olivier Stasse committed
77
        << "This is a message of level MSG_TYPE_INFO\n";
78 79
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_WARNING)
Olivier Stasse's avatar
Olivier Stasse committed
80
        << "This is a message of level MSG_TYPE_WARNING\n";
81 82
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_ERROR)
Olivier Stasse's avatar
Olivier Stasse committed
83
        << "This is a message of level MSG_TYPE_ERROR\n";
84 85
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_DEBUG_STREAM)
Olivier Stasse's avatar
Olivier Stasse committed
86
        << "This is a message of level MSG_TYPE_DEBUG_STREAM\n";
87 88
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_INFO_STREAM)
Olivier Stasse's avatar
Olivier Stasse committed
89
        << "This is a message of level MSG_TYPE_INFO_STREAM\n";
90 91
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_WARNING_STREAM)
Olivier Stasse's avatar
Olivier Stasse committed
92
        << "This is a message of level MSG_TYPE_WARNING_STREAM\n";
93 94
    dynamicgraph::RealTimeLogger::instance().spinOnce();
    logger_.stream(MSG_TYPE_ERROR_STREAM)
Olivier Stasse's avatar
Olivier Stasse committed
95
        << "This is a message of level MSG_TYPE_ERROR_STREAM\n";
96
    /* Add test toString */
97
    dynamicgraph::RealTimeLogger::instance().spinOnce();
Olivier Stasse's avatar
Olivier Stasse committed
98
    double q = 1.0;
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;
Olivier Stasse's avatar
Olivier Stasse committed
106 107
    logger_.stream(MSG_TYPE_INFO)
        << "Value to display: " << toString(vq) << '\n';
108
    dynamicgraph::RealTimeLogger::instance().spinOnce();
Olivier Stasse's avatar
Olivier Stasse committed
109 110
    logger_.stream(MSG_TYPE_INFO)
        << "Value to display: " << toString(vq, 3, 10) << '\n';
111
    dynamicgraph::RealTimeLogger::instance().spinOnce();
Olivier Stasse's avatar
Olivier Stasse committed
112
    Eigen::Matrix<double, 3, 3> an_eig_m;
113
    an_eig_m.setOnes();
Olivier Stasse's avatar
Olivier Stasse committed
114 115
    logger_.stream(MSG_TYPE_INFO)
        << "Value to display: " << toString(an_eig_m) << '\n';
116
    dynamicgraph::RealTimeLogger::instance().spinOnce();
Bergé's avatar
Bergé committed
117 118 119 120
    logger_.countdown();
  }
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
121
} // namespace dynamicgraph
122

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

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

131 132 133 134
  dynamicgraph::CustomEntity &entity =
      *(dynamic_cast<dynamicgraph::CustomEntity *>(
          dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
                                                                 "my-entity")));
135

136
  entity.setTimeSample(0.002);
Bergé's avatar
Bergé committed
137
  BOOST_CHECK_EQUAL(entity.getTimeSample(), 0.002);
138 139
  entity.setStreamPrintPeriod(0.002);
  BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.002);
140

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

  dynamicgraph::RealTimeLogger::destroy();
146
}