debug-logger.cpp 2.57 KB
Newer Older
Olivier Stasse's avatar
Olivier Stasse committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Copyright 2019, LAAS-CNRS
 *
 * Olivier Stasse
 *
 * See LICENSE file
 *
 */
#include <sstream>
#include <iostream>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"

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

#define BOOST_TEST_MODULE debug-logger

#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>

using boost::test_tools::output_test_stream;


namespace dynamicgraph
{
  class CustomEntity : public Entity
  {
  public:
    static const std::string CLASS_NAME;
    virtual const std::string& getClassName () const
    {
      return CLASS_NAME;
    }
    CustomEntity (const std::string n)
      : Entity (n)
    {
      logger_.setTimeSample(0.001);
      logger_.setStreamPrintPeriod(0.005);
      logger_.setVerbosity(VERBOSITY_ALL);
      LoggerVerbosity alv = logger_.getVerbosity();
      BOOST_CHECK(alv==VERBOSITY_ALL);
    }
46

Olivier Stasse's avatar
Olivier Stasse committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    ~CustomEntity()
    {
    }
    void testDebugTrace()
    {
      sendMsg("This is a message of level MSG_TYPE_DEBUG",MSG_TYPE_DEBUG);
      sendMsg("This is a message of level MSG_TYPE_INFO",MSG_TYPE_INFO);
      sendMsg("This is a message of level MSG_TYPE_WARNING",MSG_TYPE_WARNING);
      sendMsg("This is a message of level MSG_TYPE_ERROR",MSG_TYPE_ERROR);
      sendMsg("This is a message of level MSG_TYPE_DEBUG_STREAM",MSG_TYPE_DEBUG_STREAM);
      sendMsg("This is a message of level MSG_TYPE_INFO_STREAM",MSG_TYPE_INFO_STREAM);
      sendMsg("This is a message of level MSG_TYPE_WARNING_STREAM",MSG_TYPE_WARNING_STREAM);
      sendMsg("This is a message of level MSG_TYPE_ERROR_STREAM",MSG_TYPE_ERROR_STREAM);

      logger_.countdown();

    }
  };
  DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN (CustomEntity,"CustomEntity");
}

BOOST_AUTO_TEST_CASE(debug_logger)
{
  std::ofstream of;
71
  dynamicgraph::RealTimeLogger::instance();
72
  of.open("/tmp/dg-LOGS.txt",std::ofstream::out|std::ofstream::app);
Olivier Stasse's avatar
Olivier Stasse committed
73
  dgADD_OSTREAM_TO_RTLOG (of);
74

Olivier Stasse's avatar
Olivier Stasse committed
75
  BOOST_CHECK_EQUAL (dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
76

Olivier Stasse's avatar
Olivier Stasse committed
77
78
79
80
  dynamicgraph::CustomEntity& entity = *(dynamic_cast<dynamicgraph::CustomEntity *>(
    dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
							   "my-entity")));

81
82
83
84
85
  entity.setTimeSample(0.002);
  BOOST_CHECK_EQUAL(entity.getTimeSample(),0.002);
  entity.setStreamPrintPeriod(0.004);
  BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(),0.004);

Olivier Stasse's avatar
Olivier Stasse committed
86
87
88
89
  for(unsigned int i=0;i<10000;i++)
    {
      entity.testDebugTrace();
    }
90
91

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