logger.cpp 2.3 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
    //TODO Modif ici
65
66
    // if print is allowed by current verbosity level
    if(isStreamMsg(type))
67
      {
68
        int l = line;
69
        // check whether counter already exists
70
        string id = file+/*toString(line)*/+ l;
71
72
        map<string,double>::iterator it = m_stream_msg_counters.find(id);
        if(it == m_stream_msg_counters.end())
73
74
75
76
77
	  {
	    // 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);
	  }
78
79
80

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

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

99
100
101
102
103
104
105
  bool Logger::setStreamPrintPeriod(double s)
  {
    if(s<=0.0)
      return false;
    m_streamPrintPeriod = s;
    return true;
  }
106
} // namespace dynamicgraph