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>
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>
18
19#include <list>
20#include <mutex>
21#include <vector>
22#include <string>
23#include <ctime>
24
26class LoggerEngine;
27class LogEntry;
28class LogEntryInfo;
29struct MessageEntry;
30
32{
40};
41
43{
44private:
46
47public:
48 LogLevel() = default;
49
50 LogLevel(int l) :
51 e_level(static_cast<LogLevelEnum>(l))
52 {
53 }
54
56 {
57 e_level = level;
58 }
59
60 void setLevel(int level)
61 {
62 e_level = static_cast<LogLevelEnum>(level);
63 }
64
65 int toInt()
66 {
67 return static_cast<int>(e_level);
68 }
69
70 std::string_view toString() const
71 {
72 switch(e_level) {
73 case LOGLEVEL_TRACE:
74 return "TRACE";
75 case LOGLEVEL_DEBUG:
76 return "DEBUG";
77 case LOGLEVEL_INFO:
78 return "INFO";
80 return "WARNING";
81 case LOGLEVEL_ERROR:
82 return "ERROR";
83 case LOGLEVEL_FATAL:
84 return "FATAL";
86 default:
87 return "UNDEFINED";
88 }
89 }
90
92 {
93 e_level = level;
94 }
95
96 explicit operator LogLevelEnum() const
97 {
98 return e_level;
99 }
100
101 bool operator==(const LogLevel& other) const
102 {
103 return this->e_level == other.e_level;
104 }
105 bool operator==(const LogLevelEnum& other) const
106 {
107 return this->e_level == other;
108 }
109 bool operator>(const LogLevel& other) const
110 {
111 return this->e_level > other.e_level;
112 }
113};
114
116{
120} // namespace yarp::yarpLogger
121
123{
125 std::string text;
126 std::string filename;
127 unsigned int line;
128 std::string function;
129 std::string hostname;
130 std::string cmd;
131 std::string args;
132 int pid;
134 std::string component;
135 std::string id;
139 std::string backtrace;
140 std::string yarprun_timestamp;
141 std::string local_timestamp;
142};
143
145{
146 private:
147 LogLevel highest_error;
148 unsigned int number_of_traces;
149 unsigned int number_of_debugs;
150 unsigned int number_of_infos;
151 unsigned int number_of_warnings;
152 unsigned int number_of_errors;
153 unsigned int number_of_fatals;
154
155 public:
156 std::string port_system = "null";
157 std::string port_prefix = "null";
158 std::string port_complete = "null";
159 std::string process_name = "null";
160 std::string process_pid = "null";
161 std::string ip_address = "null";
162 std::time_t last_update = 0;
163 unsigned int logsize = 0;
164
166 void clear ();
167
169 void clearLastError ();
170 void setNewError (LogLevel level);
171 unsigned int get_number_of_traces () { return number_of_traces; }
172 unsigned int get_number_of_debugs () { return number_of_debugs; }
173 unsigned int get_number_of_infos () { return number_of_infos; }
174 unsigned int get_number_of_warnings () { return number_of_warnings; }
175 unsigned int get_number_of_errors () { return number_of_errors; }
176 unsigned int get_number_of_fatals () { return number_of_fatals; }
177};
178
180{
181 private:
182 unsigned int entry_list_max_size;
183 bool entry_list_max_size_enabled;
184
185 public:
187 std::vector<MessageEntry> entry_list;
189 void clear_logEntries();
190 bool append_logEntry(MessageEntry entry);
191
192 public:
193 LogEntry(bool _max_size_enabled, int _entry_list_max_size) :
194 entry_list_max_size(_entry_list_max_size),
195 entry_list_max_size_enabled(_max_size_enabled),
196 logging_enabled(true),
198 {
199 entry_list.reserve(entry_list_max_size);
200 }
201
202 int getLogEntryMaxSize () {return entry_list_max_size;}
203 bool getLogEntryMaxSizeEnabled () {return entry_list_max_size_enabled;}
204 void setLogEntryMaxSize (int size);
205 void setLogEntryMaxSizeEnabled (bool enable);
206
207 public:
209};
210
212{
213 //private class
214 class logger_thread : public yarp::os::PeriodicThread
215 {
216 public:
217 logger_thread (std::string _portname, double _period=0.01, int _log_list_max_size=100, int _logs_max_lines=10000);
218 public:
219 std::mutex mutex;
220 unsigned int log_list_max_size;
221 bool log_list_max_size_enabled;
222 std::list<LogEntry> log_list;
223 unsigned int logs_max_lines;
224 bool logs_max_lines_enabled;
226 std::string logger_portName;
227 int unknown_format_received;
228
229 public:
230 std::string getPortName();
231 void run() override;
232 void threadRelease() override;
233 bool listen_to_LOGLEVEL_UNDEFINED = true;
234 bool listen_to_LOGLEVEL_TRACE = true;
235 bool listen_to_LOGLEVEL_DEBUG = true;
236 bool listen_to_LOGLEVEL_INFO = true;
237 bool listen_to_LOGLEVEL_WARNING = true;
238 bool listen_to_LOGLEVEL_ERROR = true;
239 bool listen_to_LOGLEVEL_FATAL = true;
240 bool listen_to_YARP_MESSAGES = true;
241 bool listen_to_YARPRUN_MESSAGES = true;
242 };
243
244 private:
245 bool logging;
246 bool discovering;
247 logger_thread* log_updater = nullptr;
248
249 public:
250 void discover (std::list<std::string>& ports);
251 void connect (const std::list<std::string>& ports);
252
253 public:
254 LoggerEngine (std::string portName);
255 ~LoggerEngine ();
256 bool start_logging ();
257 bool stop_logging ();
258 void start_discover ();
259 void stop_discover ();
260 bool is_logging () {return logging;}
261 bool is_discovering () {return discovering;}
262 bool clear ();
263
264 bool save_all_logs_to_file (std::string filename);
265 bool load_all_logs_from_file (std::string filename);
266 bool export_log_to_text_file (std::string filename, std::string portname);
268 void get_infos (std::list<LogEntryInfo>& infos);
269 void get_messages (std::list<MessageEntry>& messages);
270 void get_messages_by_port_prefix (std::string port, std::list<MessageEntry>& messages, bool from_beginning = false);
271 void get_messages_by_port_complete (std::string port, std::list<MessageEntry>& messages, bool from_beginning = false);
272 void get_messages_by_process (std::string process, std::list<MessageEntry>& messages, bool from_beginning = false);
273 void get_messages_by_pid (std::string pid, std::list<MessageEntry>& messages, bool from_beginning = false);
274 void clear_messages_by_port_complete (std::string port);
275 void set_log_enable_by_port_complete (std::string port, bool enable);
276 bool get_log_enable_by_port_complete (std::string port);
277
278 void set_listen_option (LogLevel logLevel, bool enable);
279 void set_listen_option (std::string option, bool enable);
280 void set_listen_option (LogSystemEnum logSystem, bool enable);
281 bool get_listen_option (LogLevel logLevel);
282 bool get_listen_option (std::string option);
283 bool get_listen_option (LogSystemEnum logSystem);
284
285 void set_log_lines_max_size (bool enabled, int new_size);
286 void set_log_list_max_size (bool enabled, int new_size);
287 void get_log_lines_max_size (bool& enabled, int& current_size);
288 void get_log_list_max_size (bool& enabled, int& current_size);
289
290 std::list<MessageEntry> filter_by_level (int level, const std::list<MessageEntry>& messages);
291};
292
293#endif
An abstraction for a periodic thread.
unsigned int get_number_of_errors()
Definition: YarpLogger.h:175
unsigned int get_number_of_infos()
Definition: YarpLogger.h:173
unsigned int get_number_of_traces()
Definition: YarpLogger.h:171
void setNewError(LogLevel level)
Definition: YarpLogger.cpp:83
unsigned int get_number_of_warnings()
Definition: YarpLogger.h:174
unsigned int get_number_of_debugs()
Definition: YarpLogger.h:172
unsigned int get_number_of_fatals()
Definition: YarpLogger.h:176
yarp::yarpLogger::LogEntryInfo logInfo
Definition: YarpLogger.h:208
void setLogEntryMaxSizeEnabled(bool enable)
Definition: YarpLogger.cpp:43
bool append_logEntry(MessageEntry entry)
Definition: YarpLogger.cpp:48
void setLogEntryMaxSize(int size)
Definition: YarpLogger.cpp:36
std::vector< MessageEntry > entry_list
Definition: YarpLogger.h:187
LogEntry(bool _max_size_enabled, int _entry_list_max_size)
Definition: YarpLogger.h:193
bool operator==(const LogLevel &other) const
Definition: YarpLogger.h:101
void operator=(LogLevelEnum level)
Definition: YarpLogger.h:91
void setLevel(LogLevelEnum level)
Definition: YarpLogger.h:55
bool operator>(const LogLevel &other) const
Definition: YarpLogger.h:109
void setLevel(int level)
Definition: YarpLogger.h:60
bool operator==(const LogLevelEnum &other) const
Definition: YarpLogger.h:105
std::string_view toString() const
Definition: YarpLogger.h:70
void set_log_list_max_size(bool enabled, int new_size)
Definition: YarpLogger.cpp:900
std::list< MessageEntry > filter_by_level(int level, const std::list< MessageEntry > &messages)
bool get_listen_option(LogLevel logLevel)
Definition: YarpLogger.cpp:829
void connect(const std::list< std::string > &ports)
Definition: YarpLogger.cpp:184
void get_log_lines_max_size(bool &enabled, int &current_size)
Definition: YarpLogger.cpp:911
void get_messages(std::list< MessageEntry > &messages)
Definition: YarpLogger.cpp:632
bool get_log_enable_by_port_complete(std::string port)
Definition: YarpLogger.cpp:964
void get_messages_by_port_prefix(std::string port, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:647
LoggerEngine(std::string portName)
Definition: YarpLogger.cpp:591
void clear_messages_by_port_complete(std::string port)
Definition: YarpLogger.cpp:680
void get_messages_by_port_complete(std::string port, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:699
void set_log_lines_max_size(bool enabled, int new_size)
Definition: YarpLogger.cpp:879
void get_messages_by_process(std::string process, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:733
void discover(std::list< std::string > &ports)
Definition: YarpLogger.cpp:103
bool load_all_logs_from_file(std::string filename)
void get_log_list_max_size(bool &enabled, int &current_size)
Definition: YarpLogger.cpp:923
bool save_all_logs_to_file(std::string filename)
bool export_log_to_text_file(std::string filename, std::string portname)
void get_infos(std::list< LogEntryInfo > &infos)
Definition: YarpLogger.cpp:617
void set_log_enable_by_port_complete(std::string port, bool enable)
Definition: YarpLogger.cpp:945
void set_listen_option(LogLevel logLevel, bool enable)
Definition: YarpLogger.cpp:813
void get_messages_by_pid(std::string pid, std::list< MessageEntry > &messages, bool from_beginning=false)
Definition: YarpLogger.cpp:766