GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/debug/logger.cpp Lines: 29 35 82.9 %
Date: 2023-03-15 12:04:10 Branches: 8 34 23.5 %

Line Branch Exec Source
1
/*
2
 * Copyright 2015, 2019
3
 * LAAS-CNRS
4
 * Andrea Del Prete, François Bailly, Olivier Stasse
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
#include <dynamic-graph/logger.h>
16
#include <dynamic-graph/real-time-logger.h>
17
#include <stdio.h>
18
19
#include <iomanip>  // std::setprecision
20
#include <iostream>
21
#include <sstream>
22
23
namespace dynamicgraph {
24
25
17
Logger::Logger(double timeSample, double streamPrintPeriod)
26
    : m_timeSample(timeSample),
27
      m_streamPrintPeriod(streamPrintPeriod),
28
17
      m_printCountdown(0.0) {
29
17
  m_lv = VERBOSITY_ERROR;
30
17
}
31
32
9
Logger::~Logger() {}
33
34
8006
void Logger::setVerbosity(LoggerVerbosity lv) { m_lv = lv; }
35
36
8006
LoggerVerbosity Logger::getVerbosity() { return m_lv; }
37
11000
void Logger::countdown() {
38
11000
  if (m_printCountdown < 0.0) m_printCountdown = m_streamPrintPeriod;
39
11000
  m_printCountdown -= m_timeSample;
40
11000
}
41
42
void Logger::sendMsg(std::string msg, MsgType type, const std::string &lineId) {
43
  stream(type, lineId) << msg << '\n';
44
}
45
46
void Logger::sendMsg(std::string msg, MsgType type, const std::string &file,
47
                     int line) {
48
  std::ostringstream oss;
49
  oss << file << line;
50
  stream(type, oss.str()) << msg << '\n';
51
}
52
53
3
bool Logger::setTimeSample(double t) {
54
3
  if (t <= 0.0) return false;
55
3
  m_timeSample = t;
56
3
  return true;
57
}
58
59
3
bool Logger::setStreamPrintPeriod(double s) {
60
3
  if (s <= 0.0) return false;
61
3
  m_streamPrintPeriod = s;
62
3
  return true;
63
}
64
65
1
double Logger::getTimeSample() { return m_timeSample; }
66
67
1
double Logger::getStreamPrintPeriod() { return m_streamPrintPeriod; }
68
69
44000
bool Logger::checkStreamPeriod(const std::string &lineId) {
70
  // insert element with value 0 if it does not exist.
71
  // otherwise, return a counter to the existing one.
72
  std::pair<StreamCounterMap_t::iterator, bool> result =
73

44000
      m_stream_msg_counters.insert(std::make_pair(lineId, 0.));
74
75
  // if counter is greater than 0 then decrement it and do not print
76
44000
  double &counter = result.first->second;
77
44000
  counter -= m_timeSample;
78
44000
  if (counter > 0.0) {
79
3200
    return false;
80
  } else  // otherwise reset counter and print
81
40800
    counter = m_streamPrintPeriod;
82
40800
  return true;
83
}
84
85
}  // namespace dynamicgraph