logger.cpp 2.47 KB
Newer Older
1
/*
Guilhem Saurel's avatar
Guilhem Saurel committed
2
 * Copyright 2015, 2019
3
 * LAAS-CNRS
4
 * Andrea Del Prete, François Bailly, Olivier Stasse
Guilhem Saurel's avatar
Guilhem Saurel committed
5
 *
6
7
8
 */

#ifndef WIN32
9
#include <sys/time.h>
10
#else
11
#include <Windows.h>
12
#endif
13
#define ENABLE_RT_LOG
14
15
16
17
18
19

#include <stdio.h>
#include <iostream>
#include <iomanip>      // std::setprecision
#include <dynamic-graph/logger.h>

20
21
#include <dynamic-graph/real-time-logger.h>

22
23
24
namespace dynamicgraph
{

25
  using namespace std;
26

27
28
29
30
31
32
33
  Logger::Logger(double timeSample, double streamPrintPeriod)
    : m_timeSample(timeSample),
      m_streamPrintPeriod(streamPrintPeriod),
      m_printCountdown(0.0)
  {
    m_lv = VERBOSITY_ERROR;
  }
34

35
36
37
  Logger::~Logger()
  {
  }
Guilhem Saurel's avatar
Guilhem Saurel committed
38

39
40
41
42
  void Logger::setVerbosity(LoggerVerbosity lv)
  {
    m_lv=lv;
  }
43

44
45
46
  LoggerVerbosity Logger::getVerbosity()
  {
    return m_lv;
Guilhem Saurel's avatar
Guilhem Saurel committed
47

48
49
50
51
52
53
54
  }
  void Logger::countdown()
  {
    if(m_printCountdown<0.0)
      m_printCountdown = m_streamPrintPeriod;
    m_printCountdown -= m_timeSample;
  }
55

56
57
58
59
60
61
62
  void Logger::sendMsg(string msg, MsgType type, const char* file, int line)
  {
    if(m_lv==VERBOSITY_NONE ||
       (m_lv==VERBOSITY_ERROR && !isErrorMsg(type)) ||
       (m_lv==VERBOSITY_WARNING_ERROR && !(isWarningMsg(type) || isErrorMsg(type))) ||
       (m_lv==VERBOSITY_INFO_WARNING_ERROR && isDebugMsg(type)))
      return;
63

64
    // if print is allowed by current verbosity level
65
    if( isStreamMsg(type))
66
67
      {
        // check whether counter already exists
corentinberge's avatar
corentinberge committed
68
        string id = file+toString(line);
69
70
        map<string,double>::iterator it = m_stream_msg_counters.find(id);
        if(it == m_stream_msg_counters.end())
71
72
73
74
75
	  {
	    // if counter doesn't exist then add one
	    m_stream_msg_counters.insert(make_pair(id, 0.0));
	    it = m_stream_msg_counters.find(id);
	  }
76
77
78

        // if counter is greater than 0 then decrement it and do not print
        if(it->second>0.0)
79
80
	  {
	    it->second -= m_timeSample;
81
82
83
84
	    if (it->second<=0.0)
	      it->second = m_streamPrintPeriod;
	    else
	      return;
85
	  }
86
87
88
        else  // otherwise reset counter and print
          it->second = m_streamPrintPeriod;
      }
89
90
    dgRTLOG() << msg.c_str() << "\n";
  }
91

92
93
94
95
96
97
98
  bool Logger::setTimeSample(double t)
  {
    if(t<=0.0)
      return false;
    m_timeSample = t;
    return true;
  }
99

100
101
102
103
104
105
106
  bool Logger::setStreamPrintPeriod(double s)
  {
    if(s<=0.0)
      return false;
    m_streamPrintPeriod = s;
    return true;
  }
107
108
109
110
111
112
113
114
115
116

  double Logger::getTimeSample()
  {
    return m_timeSample;
  }

  double Logger::getStreamPrintPeriod()
  {
    return m_streamPrintPeriod;
  }
117
} // namespace dynamicgraph