logger.cpp 2.11 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
namespace dynamicgraph {

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;
}

42
43
void Logger::sendMsg(std::string msg, MsgType type, const std::string &lineId) {
  stream(type, lineId) << msg << '\n';
44
45
}

46
47
48
49
50
51
52
void Logger::sendMsg(std::string msg, MsgType type, const std::string &file,
                     int line) {
  std::ostringstream oss;
  oss << file << line;
  stream(type, oss.str()) << msg << '\n';
}

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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; }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

bool Logger::checkStreamPeriod(const std::string &lineId) {
  // insert element with value 0 if it does not exist.
  // otherwise, return a counter to the existing one.
  std::pair<StreamCounterMap_t::iterator, bool> result =
      m_stream_msg_counters.insert(std::make_pair(lineId, 0.));

  // if counter is greater than 0 then decrement it and do not print
  double &counter = result.first->second;
  if (counter > 0.0) {
    counter -= m_timeSample;
    return false;
  } else // otherwise reset counter and print
    counter = m_streamPrintPeriod;
  return true;
}

87
} // namespace dynamicgraph