Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright 2015, LAAS-CNRS |
3 |
|
|
* Author: Andrea Del Prete |
4 |
|
|
*/ |
5 |
|
|
|
6 |
|
|
#ifndef WIN32 |
7 |
|
|
#include <sys/time.h> |
8 |
|
|
#else |
9 |
|
|
#include <Windows.h> |
10 |
|
|
#endif |
11 |
|
|
|
12 |
|
|
#include <stdio.h> |
13 |
|
|
|
14 |
|
|
#include <boost/algorithm/string.hpp> |
15 |
|
|
#include <hpp/centroidal-dynamics/logger.hh> |
16 |
|
|
#include <iomanip> // std::setprecision |
17 |
|
|
#include <iostream> |
18 |
|
|
|
19 |
|
|
namespace centroidal_dynamics { |
20 |
|
|
using namespace std; |
21 |
|
|
|
22 |
|
✗ |
Logger& getLogger() { |
23 |
|
✗ |
static Logger l(0.001, 1.0); |
24 |
|
✗ |
return l; |
25 |
|
|
} |
26 |
|
|
|
27 |
|
✗ |
Logger::Logger(double timeSample, double streamPrintPeriod) |
28 |
|
✗ |
: m_timeSample(timeSample), |
29 |
|
✗ |
m_streamPrintPeriod(streamPrintPeriod), |
30 |
|
✗ |
m_printCountdown(0.0) { |
31 |
|
|
#ifdef LOGGER_VERBOSITY_ERROR |
32 |
|
|
m_lv = VERBOSITY_ERROR; |
33 |
|
|
#endif |
34 |
|
|
#ifdef LOGGER_VERBOSITY_WARNING_ERROR |
35 |
|
|
m_lv = VERBOSITY_WARNING_ERROR; |
36 |
|
|
#endif |
37 |
|
|
#ifdef LOGGER_VERBOSITY_INFO_WARNING_ERROR |
38 |
|
|
m_lv = VERBOSITY_INFO_WARNING_ERROR; |
39 |
|
|
#endif |
40 |
|
|
#ifdef LOGGER_VERBOSITY_ALL |
41 |
|
✗ |
m_lv = VERBOSITY_ALL; |
42 |
|
|
#endif |
43 |
|
|
} |
44 |
|
|
|
45 |
|
✗ |
void Logger::countdown() { |
46 |
|
✗ |
if (m_printCountdown < 0.0) m_printCountdown = m_streamPrintPeriod; |
47 |
|
✗ |
m_printCountdown -= m_timeSample; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
✗ |
void Logger::sendMsg(string msg, MsgType type, const char* file, int line) { |
51 |
|
|
// if(m_lv==VERBOSITY_NONE || |
52 |
|
|
// (m_lv==VERBOSITY_ERROR && !isErrorMsg(type)) || |
53 |
|
|
// (m_lv==VERBOSITY_WARNING_ERROR && !(isWarningMsg(type) || |
54 |
|
|
// isErrorMsg(type))) || (m_lv==VERBOSITY_INFO_WARNING_ERROR && |
55 |
|
|
// isDebugMsg(type))) |
56 |
|
|
// return; |
57 |
|
|
|
58 |
|
|
// if print is allowed by current verbosity level |
59 |
|
✗ |
if (isStreamMsg(type)) { |
60 |
|
|
// check whether counter already exists |
61 |
|
✗ |
string id = file + toString(line); |
62 |
|
✗ |
map<string, double>::iterator it = m_stream_msg_counters.find(id); |
63 |
|
✗ |
if (it == m_stream_msg_counters.end()) { |
64 |
|
|
// if counter doesn't exist then add one |
65 |
|
✗ |
m_stream_msg_counters.insert(make_pair(id, 0.0)); |
66 |
|
✗ |
it = m_stream_msg_counters.find(id); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
// if counter is greater than 0 then decrement it and do not print |
70 |
|
✗ |
if (it->second > 0.0) { |
71 |
|
✗ |
it->second -= m_timeSample; |
72 |
|
✗ |
return; |
73 |
|
|
} else // otherwise reset counter and print |
74 |
|
✗ |
it->second = m_streamPrintPeriod; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
✗ |
vector<string> fields; |
78 |
|
✗ |
boost::split(fields, file, boost::is_any_of("/")); |
79 |
|
✗ |
const char* file_name = fields[fields.size() - 1].c_str(); |
80 |
|
|
|
81 |
|
✗ |
if (isErrorMsg(type)) |
82 |
|
✗ |
printf("[ERROR %s %d] %s\n", file_name, line, msg.c_str()); |
83 |
|
✗ |
else if (isWarningMsg(type)) |
84 |
|
✗ |
printf("[WARNING %s %d] %s\n", file_name, line, msg.c_str()); |
85 |
|
✗ |
else if (isInfoMsg(type)) |
86 |
|
✗ |
printf("[INFO %s %d] %s\n", file_name, line, msg.c_str()); |
87 |
|
|
else |
88 |
|
✗ |
printf("[DEBUG %s %d] %s\n", file_name, line, msg.c_str()); |
89 |
|
|
|
90 |
|
✗ |
fflush(stdout); // Prints to screen or whatever your standard out is |
91 |
|
|
} |
92 |
|
|
|
93 |
|
✗ |
bool Logger::setTimeSample(double t) { |
94 |
|
✗ |
if (t <= 0.0) return false; |
95 |
|
✗ |
m_timeSample = t; |
96 |
|
✗ |
return true; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
✗ |
bool Logger::setStreamPrintPeriod(double s) { |
100 |
|
✗ |
if (s <= 0.0) return false; |
101 |
|
✗ |
m_streamPrintPeriod = s; |
102 |
|
✗ |
return true; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
} // namespace centroidal_dynamics |
106 |
|
|
|