YARP
Yet Another Robot Platform
YarpLogger.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_LOGGER
10 #define YARP_LOGGER
11 
12 #include <yarp/os/Network.h>
13 #include <yarp/os/Port.h>
14 #include <yarp/os/BufferedPort.h>
15 #include <yarp/os/Bottle.h>
16 #include <yarp/os/Time.h>
17 #include <yarp/os/Vocab.h>
18 
19 #include <yarp/os/Thread.h>
20 #include <yarp/os/PeriodicThread.h>
21 
22 #include <list>
23 #include <mutex>
24 #include <vector>
25 #include <string>
26 #include <ctime>
27 
28 namespace yarp
29 {
30  namespace yarpLogger
31  {
32  class LoggerEngine;
33  class LogEntry;
34  class LogEntryInfo;
35  struct MessageEntry;
36 
38  {
45  LOGLEVEL_FATAL = 6
46  };
47 
48  class LogLevel
49  {
50  private:
52 
53  public:
54  LogLevel() = default;
55 
56  LogLevel(int l) :
57  e_level(static_cast<LogLevelEnum>(l))
58  {
59  }
60 
61  void setLevel(LogLevelEnum level)
62  {
63  e_level = level;
64  }
65 
66  void setLevel(int level)
67  {
68  e_level = static_cast<LogLevelEnum>(level);
69  }
70 
71  int toInt()
72  {
73  return static_cast<int>(e_level);
74  }
75 
76  std::string toString() const
77  {
78  if (e_level == LOGLEVEL_UNDEFINED) { return "<UNDEFINED>"; }
79  if (e_level == LOGLEVEL_TRACE) { return "<TRACE>"; }
80  if (e_level == LOGLEVEL_DEBUG) { return "<DEBUG>"; }
81  if (e_level == LOGLEVEL_INFO) { return "<INFO>"; }
82  if (e_level == LOGLEVEL_WARNING) { return "<WARNING>"; }
83  if (e_level == LOGLEVEL_ERROR) { return "<ERROR>"; }
84  if (e_level == LOGLEVEL_FATAL) { return "<FATAL>"; }
85  else { return "<UNDEFINED>"; }
86  }
87 
88  void operator=(LogLevelEnum level)
89  {
90  e_level = level;
91  }
92 
93  explicit operator LogLevelEnum() const
94  {
95  return e_level;
96  }
97 
98  bool operator==(const LogLevel& other) const
99  {
100  return this->e_level == other.e_level;
101  }
102  bool operator==(const LogLevelEnum& other) const
103  {
104  return this->e_level == other;
105  }
106  bool operator>(const LogLevel& other) const
107  {
108  return this->e_level > other.e_level;
109  }
110  };
111 
113  {
116  };
117  }
118 }
119 
121 {
123  std::string text;
124  std::string filename;
125  unsigned int line;
126  std::string function;
127  std::string hostname;
128  std::string cmd;
129  std::string args;
130  int pid;
131  long thread_id;
132  std::string component;
133  double systemtime;
134  double networktime;
135  double externaltime;
136  std::string backtrace;
137  std::string yarprun_timestamp;
138  std::string local_timestamp;
139 };
140 
142 {
143  private:
144  LogLevel highest_error;
145  unsigned int number_of_traces;
146  unsigned int number_of_debugs;
147  unsigned int number_of_infos;
148  unsigned int number_of_warnings;
149  unsigned int number_of_errors;
150  unsigned int number_of_fatals;
151 
152  public:
153  std::string port_system;
154  std::string port_prefix;
155  std::string port_complete;
156  std::string process_name;
157  std::string process_pid;
158  std::string ip_address;
159  std::time_t last_update;
160  unsigned int logsize;
161 
163  void clear ();
164 
166  void clearLastError ();
167  void setNewError (LogLevel level);
168  unsigned int get_number_of_traces () { return number_of_traces; }
169  unsigned int get_number_of_debugs () { return number_of_debugs; }
170  unsigned int get_number_of_infos () { return number_of_infos; }
171  unsigned int get_number_of_warnings () { return number_of_warnings; }
172  unsigned int get_number_of_errors () { return number_of_errors; }
173  unsigned int get_number_of_fatals () { return number_of_fatals; }
174 };
175 
177 {
178  private:
179  unsigned int entry_list_max_size;
180  bool entry_list_max_size_enabled;
181 
182  public:
184  std::vector<MessageEntry> entry_list;
186  void clear_logEntries();
187  bool append_logEntry(MessageEntry entry);
188 
189  public:
190  LogEntry(int _entry_list_max_size=10000) :
191  entry_list_max_size(_entry_list_max_size),
192  entry_list_max_size_enabled(true),
193  logging_enabled(true),
195  {
196  entry_list.reserve(entry_list_max_size);
197  }
198 
199  int getLogEntryMaxSize () {return entry_list_max_size;}
200  bool getLogEntryMaxSizeEnabled () {return entry_list_max_size_enabled;}
201  void setLogEntryMaxSize (int size);
202  void setLogEntryMaxSizeEnabled (bool enable);
203 
204  public:
206 };
207 
209 {
210  //private class
211  class logger_thread : public yarp::os::PeriodicThread
212  {
213  public:
214  logger_thread (std::string _portname, double _period=0.01, int _log_list_max_size=100);
215  public:
216  std::mutex mutex;
217  unsigned int log_list_max_size;
218  bool log_list_max_size_enabled;
219  std::list<LogEntry> log_list;
221  std::string logger_portName;
222  int unknown_format_received;
223 
224  public:
225  std::string getPortName();
226  void run() override;
227  void threadRelease() override;
228  bool listen_to_LOGLEVEL_UNDEFINED;
229  bool listen_to_LOGLEVEL_TRACE;
230  bool listen_to_LOGLEVEL_DEBUG;
231  bool listen_to_LOGLEVEL_INFO;
232  bool listen_to_LOGLEVEL_WARNING;
233  bool listen_to_LOGLEVEL_ERROR;
234  bool listen_to_LOGLEVEL_FATAL;
235  bool listen_to_YARP_MESSAGES;
236  bool listen_to_YARPRUN_MESSAGES;
237  };
238 
239  private:
240  bool logging;
241  bool discovering;
242  logger_thread* log_updater;
243 
244  public:
245  void discover (std::list<std::string>& ports);
246  void connect (const std::list<std::string>& ports);
247 
248  public:
249  LoggerEngine (std::string portName);
250  ~LoggerEngine ();
251  bool start_logging ();
252  bool stop_logging ();
253  void start_discover ();
254  void stop_discover ();
255  bool is_logging () {return logging;}
256  bool is_discovering () {return discovering;}
257  bool clear ();
258 
259  bool save_all_logs_to_file (std::string filename);
260  bool load_all_logs_from_file (std::string filename);
261  bool export_log_to_text_file (std::string filename, std::string portname);
262  int get_num_of_processes ();
263  void get_infos (std::list<LogEntryInfo>& infos);
264  void get_messages (std::list<MessageEntry>& messages);
265  void get_messages_by_port_prefix (std::string port, std::list<MessageEntry>& messages, bool from_beginning = false);
266  void get_messages_by_port_complete (std::string port, std::list<MessageEntry>& messages, bool from_beginning = false);
267  void get_messages_by_process (std::string process, std::list<MessageEntry>& messages, bool from_beginning = false);
268  void get_messages_by_pid (std::string pid, std::list<MessageEntry>& messages, bool from_beginning = false);
269  void clear_messages_by_port_complete (std::string port);
270  void set_log_enable_by_port_complete (std::string port, bool enable);
271  bool get_log_enable_by_port_complete (std::string port);
272 
273  void set_listen_option (LogLevel logLevel, bool enable);
274  void set_listen_option (std::string option, bool enable);
275  void set_listen_option (LogSystemEnum logSystem, bool enable);
276  bool get_listen_option (LogLevel logLevel);
277  bool get_listen_option (std::string option);
278  bool get_listen_option (LogSystemEnum logSystem);
279 
280  void set_log_lines_max_size (bool enabled, int new_size);
281  void set_log_list_max_size (bool enabled, int new_size);
282  void get_log_lines_max_size (bool& enabled, int& current_size);
283  void get_log_list_max_size (bool& enabled, int& current_size);
284 
285  std::list<MessageEntry> filter_by_level (int level, const std::list<MessageEntry>& messages);
286 };
287 
288 #endif
yarp::yarpLogger::LoggerEngine::start_logging
bool start_logging()
Definition: YarpLogger.cpp:497
yarp::yarpLogger::LogEntryInfo::process_pid
std::string process_pid
Definition: YarpLogger.h:157
yarp::yarpLogger::LOGLEVEL_FATAL
@ LOGLEVEL_FATAL
Definition: YarpLogger.h:45
yarp::yarpLogger::LogEntry::logging_enabled
bool logging_enabled
Definition: YarpLogger.h:183
Network.h
yarp::yarpLogger::LogEntryInfo::get_number_of_fatals
unsigned int get_number_of_fatals()
Definition: YarpLogger.h:173
yarp::yarpLogger::LOGLEVEL_INFO
@ LOGLEVEL_INFO
Definition: YarpLogger.h:42
yarp::yarpLogger::LoggerEngine::clear_messages_by_port_complete
void clear_messages_by_port_complete(std::string port)
Definition: YarpLogger.cpp:636
yarp::yarpLogger::LOGLEVEL_WARNING
@ LOGLEVEL_WARNING
Definition: YarpLogger.h:43
yarp::yarpLogger::LoggerEngine::is_logging
bool is_logging()
Definition: YarpLogger.h:255
yarp::yarpLogger::MessageEntry::yarprun_timestamp
std::string yarprun_timestamp
Definition: YarpLogger.h:137
yarp::yarpLogger::LogEntryInfo::process_name
std::string process_name
Definition: YarpLogger.h:156
yarp::yarpLogger::LOGLEVEL_UNDEFINED
@ LOGLEVEL_UNDEFINED
Definition: YarpLogger.h:39
yarp::yarpLogger::MessageEntry::systemtime
double systemtime
Definition: YarpLogger.h:133
yarp::yarpLogger::LogLevel::operator==
bool operator==(const LogLevel &other) const
Definition: YarpLogger.h:98
yarp::yarpLogger::LoggerEngine::export_log_to_text_file
bool export_log_to_text_file(std::string filename, std::string portname)
Definition: YarpLogger.cpp:816
yarp::yarpLogger::LogEntryInfo::port_system
std::string port_system
Definition: YarpLogger.h:153
Port.h
yarp::yarpLogger::LogSystemEnum
LogSystemEnum
Definition: YarpLogger.h:113
yarp::yarpLogger::MessageEntry::line
unsigned int line
Definition: YarpLogger.h:125
yarp::yarpLogger::LogEntryInfo::get_number_of_errors
unsigned int get_number_of_errors()
Definition: YarpLogger.h:172
yarp::yarpLogger::LoggerEngine::set_log_enable_by_port_complete
void set_log_enable_by_port_complete(std::string port, bool enable)
Definition: YarpLogger.cpp:1049
yarp::yarpLogger::LoggerEngine::clear
bool clear()
Definition: YarpLogger.cpp:1040
yarp::yarpLogger::LogEntryInfo::get_number_of_traces
unsigned int get_number_of_traces()
Definition: YarpLogger.h:168
yarp::yarpLogger::LogEntry::entry_list
std::vector< MessageEntry > entry_list
Definition: YarpLogger.h:184
yarp::yarpLogger::LoggerEngine::get_infos
void get_infos(std::list< LogEntryInfo > &infos)
Definition: YarpLogger.cpp:579
yarp::yarpLogger::LoggerEngine::get_messages_by_process
void get_messages_by_process(std::string process, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:685
yarp::yarpLogger::MessageEntry::text
std::string text
Definition: YarpLogger.h:123
yarp::yarpLogger::MessageEntry::pid
int pid
Definition: YarpLogger.h:130
yarp::yarpLogger::LoggerEngine::get_messages_by_port_prefix
void get_messages_by_port_prefix(std::string port, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:605
yarp::yarpLogger::LoggerEngine::get_num_of_processes
int get_num_of_processes()
Definition: YarpLogger.cpp:572
yarp::yarpLogger::LogLevel::LogLevel
LogLevel()=default
yarp::yarpLogger::LOGLEVEL_TRACE
@ LOGLEVEL_TRACE
Definition: YarpLogger.h:40
yarp::yarpLogger::MessageEntry::backtrace
std::string backtrace
Definition: YarpLogger.h:136
yarp::yarpLogger::LogLevelEnum
LogLevelEnum
Definition: YarpLogger.h:38
yarp::yarpLogger::LoggerEngine::LoggerEngine
LoggerEngine(std::string portName)
Definition: YarpLogger.cpp:555
yarp::yarpLogger::LogEntry::append_logEntry
bool append_logEntry(MessageEntry entry)
Definition: YarpLogger.cpp:52
yarp::os::BufferedPort< yarp::os::Bottle >
yarp::yarpLogger::LogEntryInfo::ip_address
std::string ip_address
Definition: YarpLogger.h:158
yarp::yarpLogger::LogEntry::setLogEntryMaxSize
void setLogEntryMaxSize(int size)
Definition: YarpLogger.cpp:40
yarp::yarpLogger::LoggerEngine
Definition: YarpLogger.h:209
yarp::yarpLogger::LogLevel::toString
std::string toString() const
Definition: YarpLogger.h:76
yarp::yarpLogger::MessageEntry::cmd
std::string cmd
Definition: YarpLogger.h:128
yarp::yarpLogger::LoggerEngine::is_discovering
bool is_discovering()
Definition: YarpLogger.h:256
yarp::yarpLogger::LogEntryInfo::clear
void clear()
Definition: YarpLogger.cpp:64
yarp::yarpLogger::LoggerEngine::set_log_lines_max_size
void set_log_lines_max_size(bool enabled, int new_size)
Definition: YarpLogger.cpp:995
yarp::yarpLogger::LogLevel
Definition: YarpLogger.h:49
yarp::yarpLogger::LogEntryInfo::get_number_of_debugs
unsigned int get_number_of_debugs()
Definition: YarpLogger.h:169
yarp::yarpLogger::LogEntryInfo::get_number_of_infos
unsigned int get_number_of_infos()
Definition: YarpLogger.h:170
yarp::yarpLogger::LogLevel::setLevel
void setLevel(LogLevelEnum level)
Definition: YarpLogger.h:61
yarp::yarpLogger::LogEntry::LogEntry
LogEntry(int _entry_list_max_size=10000)
Definition: YarpLogger.h:190
yarp::yarpLogger::MessageEntry::networktime
double networktime
Definition: YarpLogger.h:134
yarp::yarpLogger::MessageEntry::level
LogLevel level
Definition: YarpLogger.h:122
yarp::yarpLogger::LogEntryInfo::setNewError
void setNewError(LogLevel level)
Definition: YarpLogger.cpp:87
yarp::yarpLogger::MessageEntry::component
std::string component
Definition: YarpLogger.h:132
yarp::yarpLogger::LogEntry::last_read_message
int last_read_message
Definition: YarpLogger.h:185
yarp::yarpLogger::MessageEntry::args
std::string args
Definition: YarpLogger.h:129
yarp::yarpLogger::MessageEntry
Definition: YarpLogger.h:121
yarp::yarpLogger::LogEntry
Definition: YarpLogger.h:177
yarp::yarpLogger::LogLevel::setLevel
void setLevel(int level)
Definition: YarpLogger.h:66
Thread.h
yarp::yarpLogger::LoggerEngine::filter_by_level
std::list< MessageEntry > filter_by_level(int level, const std::list< MessageEntry > &messages)
yarp::yarpLogger::LogLevel::LogLevel
LogLevel(int l)
Definition: YarpLogger.h:56
yarp::yarpLogger::LoggerEngine::discover
void discover(std::list< std::string > &ports)
Definition: YarpLogger.cpp:98
yarp::yarpLogger::LogEntry::setLogEntryMaxSizeEnabled
void setLogEntryMaxSizeEnabled(bool enable)
Definition: YarpLogger.cpp:47
yarp::yarpLogger::LogLevel::operator>
bool operator>(const LogLevel &other) const
Definition: YarpLogger.h:106
yarp::yarpLogger::LOGSYSTEM_YARPRUN
@ LOGSYSTEM_YARPRUN
Definition: YarpLogger.h:115
yarp::yarpLogger::MessageEntry::local_timestamp
std::string local_timestamp
Definition: YarpLogger.h:138
BufferedPort.h
yarp::yarpLogger::LogEntry::getLogEntryMaxSizeEnabled
bool getLogEntryMaxSizeEnabled()
Definition: YarpLogger.h:200
yarp::yarpLogger::LogEntryInfo::get_number_of_warnings
unsigned int get_number_of_warnings()
Definition: YarpLogger.h:171
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
yarp::yarpLogger::LogEntry::logInfo
yarp::yarpLogger::LogEntryInfo logInfo
Definition: YarpLogger.h:205
yarp::yarpLogger::LogEntryInfo::port_complete
std::string port_complete
Definition: YarpLogger.h:155
yarp::yarpLogger::LOGLEVEL_DEBUG
@ LOGLEVEL_DEBUG
Definition: YarpLogger.h:41
PeriodicThread.h
yarp::yarpLogger::LoggerEngine::load_all_logs_from_file
bool load_all_logs_from_file(std::string filename)
Definition: YarpLogger.cpp:915
yarp::yarpLogger::MessageEntry::hostname
std::string hostname
Definition: YarpLogger.h:127
yarp::yarpLogger::LoggerEngine::get_log_lines_max_size
void get_log_lines_max_size(bool &enabled, int &current_size)
Definition: YarpLogger.cpp:1020
yarp::yarpLogger::LogLevel::operator==
bool operator==(const LogLevelEnum &other) const
Definition: YarpLogger.h:102
yarp::yarpLogger::LogEntryInfo::getLastError
LogLevel getLastError()
Definition: YarpLogger.cpp:77
yarp::yarpLogger::LogEntryInfo::clearLastError
void clearLastError()
Definition: YarpLogger.cpp:82
yarp::yarpLogger::LoggerEngine::start_discover
void start_discover()
Definition: YarpLogger.cpp:542
yarp::yarpLogger::LoggerEngine::save_all_logs_to_file
bool save_all_logs_to_file(std::string filename)
Definition: YarpLogger.cpp:842
yarp::yarpLogger::LOGLEVEL_ERROR
@ LOGLEVEL_ERROR
Definition: YarpLogger.h:44
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::yarpLogger::LogEntry::getLogEntryMaxSize
int getLogEntryMaxSize()
Definition: YarpLogger.h:199
yarp::yarpLogger::LoggerEngine::stop_logging
bool stop_logging()
Definition: YarpLogger.cpp:533
yarp::yarpLogger::LogLevel::operator=
void operator=(LogLevelEnum level)
Definition: YarpLogger.h:88
yarp::yarpLogger::LoggerEngine::get_log_list_max_size
void get_log_list_max_size(bool &enabled, int &current_size)
Definition: YarpLogger.cpp:1031
yarp::yarpLogger::LogEntryInfo::port_prefix
std::string port_prefix
Definition: YarpLogger.h:154
Vocab.h
yarp::yarpLogger::LoggerEngine::stop_discover
void stop_discover()
Definition: YarpLogger.cpp:550
yarp::yarpLogger::LoggerEngine::get_messages_by_port_complete
void get_messages_by_port_complete(std::string port, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:653
yarp::yarpLogger::MessageEntry::thread_id
long thread_id
Definition: YarpLogger.h:131
yarp::yarpLogger::MessageEntry::filename
std::string filename
Definition: YarpLogger.h:124
yarp::yarpLogger::MessageEntry::externaltime
double externaltime
Definition: YarpLogger.h:135
Time.h
yarp::yarpLogger::LoggerEngine::set_listen_option
void set_listen_option(LogLevel logLevel, bool enable)
Definition: YarpLogger.cpp:760
yarp::yarpLogger::LoggerEngine::~LoggerEngine
~LoggerEngine()
Definition: YarpLogger.cpp:562
yarp::yarpLogger::LogEntryInfo::LogEntryInfo
LogEntryInfo()
Definition: YarpLogger.h:162
yarp::yarpLogger::LogEntryInfo::last_update
std::time_t last_update
Definition: YarpLogger.h:159
yarp::yarpLogger::LoggerEngine::set_log_list_max_size
void set_log_list_max_size(bool enabled, int new_size)
Definition: YarpLogger.cpp:1011
yarp::yarpLogger::LogEntryInfo::logsize
unsigned int logsize
Definition: YarpLogger.h:160
yarp::yarpLogger::LOGSYSTEM_YARP
@ LOGSYSTEM_YARP
Definition: YarpLogger.h:114
yarp::yarpLogger::LoggerEngine::get_messages
void get_messages(std::list< MessageEntry > &messages)
Definition: YarpLogger.cpp:592
yarp::yarpLogger::LogEntry::clear_logEntries
void clear_logEntries()
Definition: YarpLogger.cpp:33
yarp::yarpLogger::LogLevel::toInt
int toInt()
Definition: YarpLogger.h:71
Bottle.h
yarp::yarpLogger::LoggerEngine::get_messages_by_pid
void get_messages_by_pid(std::string pid, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:716
yarp::yarpLogger::LoggerEngine::connect
void connect(const std::list< std::string > &ports)
Definition: YarpLogger.cpp:179
yarp::yarpLogger::LoggerEngine::get_listen_option
bool get_listen_option(LogLevel logLevel)
Definition: YarpLogger.cpp:774
yarp::yarpLogger::LoggerEngine::get_log_enable_by_port_complete
bool get_log_enable_by_port_complete(std::string port)
Definition: YarpLogger.cpp:1066
yarp::yarpLogger::LogEntryInfo
Definition: YarpLogger.h:142