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