logger.cpp 2.4 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
#include <dynamic-graph/logger.h>
#include <iomanip> // std::setprecision
#include <iostream>
corentinberge's avatar
corentinberge committed
18
#include <sstream>
19
20
#include <stdio.h>

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

23
24
25
26
27
28
29
30
31
32
33
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
63
64
65
66
67
68
69
70
71
72
73
namespace dynamicgraph {

using namespace std;

Logger::Logger(double timeSample, double streamPrintPeriod)
    : m_timeSample(timeSample), m_streamPrintPeriod(streamPrintPeriod),
      m_printCountdown(0.0) {
  m_lv = VERBOSITY_ERROR;
}

Logger::~Logger() {}

void Logger::setVerbosity(LoggerVerbosity lv) { m_lv = lv; }

LoggerVerbosity Logger::getVerbosity() { return m_lv; }
void Logger::countdown() {
  if (m_printCountdown < 0.0)
    m_printCountdown = m_streamPrintPeriod;
  m_printCountdown -= m_timeSample;
}

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;

  // if print is allowed by current verbosity level
  if (isStreamMsg(type)) {
    // check whether counter already exists
    std::ostringstream oss;
    oss << file << line;
    std::string id(oss.str());
    map<string, double>::iterator it = m_stream_msg_counters.find(id);
    if (it == m_stream_msg_counters.end()) {
      // 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);
    }

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

bool Logger::setTimeSample(double t) {
  if (t <= 0.0)
    return false;
  m_timeSample = t;
  return true;
}

bool Logger::setStreamPrintPeriod(double s) {
  if (s <= 0.0)
    return false;
  m_streamPrintPeriod = s;
  return true;
}

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

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