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
An abstraction for a periodic thread.
unsigned int get_number_of_errors()
Definition: YarpLogger.h:172
unsigned int get_number_of_infos()
Definition: YarpLogger.h:170
unsigned int get_number_of_traces()
Definition: YarpLogger.h:168
void setNewError(LogLevel level)
Definition: YarpLogger.cpp:87
unsigned int get_number_of_warnings()
Definition: YarpLogger.h:171
unsigned int get_number_of_debugs()
Definition: YarpLogger.h:169
unsigned int get_number_of_fatals()
Definition: YarpLogger.h:173
yarp::yarpLogger::LogEntryInfo logInfo
Definition: YarpLogger.h:205
void setLogEntryMaxSizeEnabled(bool enable)
Definition: YarpLogger.cpp:47
bool append_logEntry(MessageEntry entry)
Definition: YarpLogger.cpp:52
LogEntry(int _entry_list_max_size=10000)
Definition: YarpLogger.h:190
void setLogEntryMaxSize(int size)
Definition: YarpLogger.cpp:40
std::vector< MessageEntry > entry_list
Definition: YarpLogger.h:184
bool operator==(const LogLevel &other) const
Definition: YarpLogger.h:98
void operator=(LogLevelEnum level)
Definition: YarpLogger.h:88
void setLevel(LogLevelEnum level)
Definition: YarpLogger.h:61
bool operator>(const LogLevel &other) const
Definition: YarpLogger.h:106
void setLevel(int level)
Definition: YarpLogger.h:66
bool operator==(const LogLevelEnum &other) const
Definition: YarpLogger.h:102
std::string toString() const
Definition: YarpLogger.h:76
void set_log_list_max_size(bool enabled, int new_size)
bool get_listen_option(LogLevel logLevel)
Definition: YarpLogger.cpp:774
void connect(const std::list< std::string > &ports)
Definition: YarpLogger.cpp:179
void get_log_lines_max_size(bool &enabled, int &current_size)
void get_messages(std::list< MessageEntry > &messages)
Definition: YarpLogger.cpp:592
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:605
LoggerEngine(std::string portName)
Definition: YarpLogger.cpp:555
void clear_messages_by_port_complete(std::string port)
Definition: YarpLogger.cpp:636
void get_messages_by_port_complete(std::string port, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:653
void set_log_lines_max_size(bool enabled, int new_size)
Definition: YarpLogger.cpp:995
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:685
void discover(std::list< std::string > &ports)
Definition: YarpLogger.cpp:98
bool load_all_logs_from_file(std::string filename)
Definition: YarpLogger.cpp:915
void get_log_list_max_size(bool &enabled, int &current_size)
bool save_all_logs_to_file(std::string filename)
Definition: YarpLogger.cpp:842
bool export_log_to_text_file(std::string filename, std::string portname)
Definition: YarpLogger.cpp:816
void get_infos(std::list< LogEntryInfo > &infos)
Definition: YarpLogger.cpp:579
void set_log_enable_by_port_complete(std::string port, bool enable)
void set_listen_option(LogLevel logLevel, bool enable)
Definition: YarpLogger.cpp:760
void get_messages_by_pid(std::string pid, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:716
The main, catch-all namespace for YARP.
Definition: environment.h:18