YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Log.cpp
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#include <yarp/os/Log.h>
7
9#include <yarp/conf/system.h>
10
12#include <yarp/os/LogStream.h>
13#include <yarp/os/NetType.h>
14#include <yarp/os/Network.h>
15#include <yarp/os/SystemClock.h>
16#include <yarp/os/SystemInfo.h>
17#include <yarp/os/Time.h>
18
22
23#include <algorithm>
24#include <atomic>
25#include <cstdarg>
26#include <cstdio>
27#include <cstdlib>
28#include <cstring>
29#include <fstream>
30#include <iostream>
31#include <iomanip>
32#include <mutex>
33#include <sstream>
34
35#ifdef YARP_HAS_ACE
36# include <ace/Stack_Trace.h>
37// In one the ACE headers there is a definition of "main" for WIN32
38# ifdef main
39# undef main
40# endif
41#elif defined(YARP_HAS_EXECINFO_H)
42# include <execinfo.h>
43#endif
44
45#ifdef YARP_HAS_WIN_VT_SUPPORT
46#include <windows.h>
47#endif
48
49
51{
52public:
53 LogPrivate(const char* file,
54 const unsigned int line,
55 const char* func,
56 const char* id,
57 const double externaltime,
59 const LogComponent& comp);
60
62 const char *msg,
63 va_list args) const;
64
66 const char* msg,
67 const char* file,
68 const unsigned int line,
69 const char* func,
70 double systemtime,
71 double networktime,
72 double externaltime,
73 const char* comp_name,
74 const char* id);
75
77 const char* msg,
78 const char* file,
79 const unsigned int line,
80 const char* func,
81 double systemtime,
82 double networktime,
83 double externaltime,
84 const char* comp_name,
85 const char* id);
86
87 // Calls the right print and forward callbacks
88 static void do_log(yarp::os::Log::LogType type,
89 const char* msg,
90 const char* file,
91 const unsigned int line,
92 const char* func,
93 double systemtime,
94 double networktime,
95 double externaltime,
97 const std::string_view id);
98
99 // This is a LogCallback that calls the print callback that is currently
100 // set, even if this is changed later
102 const char* msg,
103 const char* file,
104 const unsigned int line,
105 const char* func,
106 double systemtime,
107 double networktime,
108 double externaltime,
109 const char* comp_name,
110 const char* id)
111 {
112 if (auto cb = current_print_callback.load()) {
114 }
115 }
116
117 // This is a LogCallback that calls the forward callback that is currently
118 // set, even if this is changed later
120 const char* msg,
121 const char* file,
122 const unsigned int line,
123 const char* func,
124 double systemtime,
125 double networktime,
126 double externaltime,
127 const char* comp_name,
128 const char* id)
129 {
130 if (auto cb = current_forward_callback.load()) {
132 }
133 }
134
136
137#ifdef YARP_HAS_WIN_VT_SUPPORT
138 bool enable_vt_colors();
139#endif
140
141 const char* file;
142 const unsigned int line;
143 const char* func;
149 const char* id;
150
151 static std::atomic<bool> yarprun_format;
152 static std::atomic<bool> colored_output;
153 static std::atomic<bool> verbose_output;
154 static std::atomic<bool> compact_output;
155 static std::atomic<bool> debug_output;
156 static std::atomic<bool> trace_output;
157 static std::atomic<bool> forward_output;
158 static std::atomic<bool> forward_codeinfo;
159 static std::atomic<bool> forward_hostname;
160 static std::atomic<bool> forward_processinfo;
161 static std::atomic<bool> forward_backtrace;
162 static std::atomic<bool> debug_log;
163#ifdef YARP_HAS_WIN_VT_SUPPORT
164 static std::atomic<bool> vt_colors_enabled;
165#endif
166
167 static std::atomic<yarp::os::Log::LogType> current_minimum_print_level;
168 static std::atomic<yarp::os::Log::LogType> current_minimum_forward_level;
169
170 static std::atomic<Log::LogCallback> current_print_callback;
171 static std::atomic<Log::LogCallback> current_forward_callback;
172
174};
175
176// BEGIN Utilities
177
178#define BOLD_RED (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;31m" : "")
179#define BOLD_GREEN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;32m" : "")
180#define BOLD_YELLOW (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;33m" : "")
181#define BOLD_BLUE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;34m" : "")
182#define BOLD_MAGENTA (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;35m" : "")
183#define BOLD_CYAN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;36m" : "")
184#define BOLD_WHITE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;37m" : "")
185#define RED (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[31m" : "")
186#define GREEN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[32m" : "")
187#define YELLOW (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[33m" : "")
188#define BLUE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[34m" : "")
189#define MAGENTA (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[35m" : "")
190#define CYAN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[36m" : "")
191#define WHITE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[37m" : "")
192#define BRI_RED (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[91m" : "")
193#define BRI_GREEN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[92m" : "")
194#define BRI_YELLOW (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[93m" : "")
195#define BRI_BLUE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[94m" : "")
196#define BRI_MAGENTA (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[95m" : "")
197#define BRI_CYAN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[96m" : "")
198#define BRI_WHITE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[97m" : "")
199#define RED_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[41m" : "")
200#define GREEN_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[42m" : "")
201#define YELLOW_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[43m" : "")
202#define BLUE_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[44m" : "")
203#define MAGENTA_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[45m" : "")
204#define CYAN_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[46m" : "")
205#define WHITE_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[47m" : "")
206#define CLEAR (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[00m" : "")
207
208namespace {
209
210inline const char* logTypeToString(yarp::os::Log::LogType t)
211{
212 switch (t) {
214 return "TRACE";
216 return "DEBUG";
218 return "INFO";
220 return "WARNING";
222 return "ERROR";
224 return "FATAL";
225 default:
226 return "";
227 }
228}
229
230inline const char* logTypeToColor(yarp::os::Log::LogType t)
231{
232 switch (t) {
234 return BOLD_WHITE;
236 return BOLD_GREEN;
238 return BOLD_BLUE;
240 return BOLD_YELLOW;
242 return BOLD_RED;
244 return BOLD_WHITE;
246 return CYAN;
247 default:
248 return "";
249 }
250}
251
252inline const char* logTypeToBgColor(yarp::os::Log::LogType t)
253{
254 switch (t) {
256 return RED_BG;
262 default:
263 return "";
264 }
265}
266
267inline const char* compNameToColor(const char* comp_name)
268{
269 if (!comp_name || comp_name[0] == '\0' || !yarp::os::impl::LogPrivate::colored_output.load()) {
270 return "";
271 }
272
273 // Hashing the component for every log line is probably not optimal, but in
274 // using a hash table will have the hash + a search, and since the
275 // LogCallback has only the name of the component, we cannot store the color
276 // in the LogComponent without breaking the API, therefore this is the less
277 // invasive option.
278 // Anyway, this is enabled only when YARP_COLORED_OUTPUT is set, therefore
279 // it can be easily disabled if this slows down the execution.
280 static std::hash<std::string_view> hsh;
281 std::size_t comp_hash = hsh(comp_name) % 6;
282 switch (comp_hash) {
283 // BOLD_RED is not used, since this is easily confused with an error
284 case 0:
285 return BOLD_GREEN;
286 case 1:
287 return BOLD_YELLOW;
288 case 2:
289 return BOLD_BLUE;
290 case 3:
291 return BOLD_MAGENTA;
292 case 4:
293 return BOLD_CYAN;
294 case 5:
295 return BOLD_WHITE;
296 default:
297 return "";
298 }
299}
300
301inline const char* idToColor(const char* id)
302{
303 if (!id || id[0] == '\0' || !yarp::os::impl::LogPrivate::colored_output.load()) {
304 return "";
305 }
306
307 // Hashing the component for every log line is probably not optimal, but in
308 // using a hash table will have the hash + a search, and since the
309 // LogCallback has only the name of the component, we cannot store the color
310 // in the LogComponent without breaking the API, therefore this is the less
311 // invasive option.
312 // Anyway, this is enabled only when YARP_COLORED_OUTPUT is set, therefore
313 // it can be easily disabled if this slows down the execution.
314 static std::hash<std::string_view> hsh;
315 std::size_t id_hash = hsh(id) % 12;
316 switch (id_hash) {
317 case 0:
318 return GREEN;
319 case 1:
320 return YELLOW;
321 case 2:
322 return BLUE;
323 case 3:
324 return MAGENTA;
325 case 4:
326 return CYAN;
327 case 5:
328 return WHITE;
329 case 6:
330 return BRI_GREEN;
331 case 7:
332 return BRI_YELLOW;
333 case 8:
334 return BRI_BLUE;
335 case 9:
336 return BRI_MAGENTA;
337 case 10:
338 return BRI_CYAN;
339 case 11:
340 return BRI_WHITE;
341 default:
342 return GREEN;
343 }
344}
345
346std::string backtrace()
347{
348#ifdef YARP_HAS_ACE
349 ACE_Stack_Trace st(-1);
350 // TODO demangle symbols using <cxxabi.h> and abi::__cxa_demangle
351 // when available.
352 return {st.c_str()};
353#elif defined(YARP_HAS_EXECINFO_H)
354 const size_t max_depth = 100;
355 size_t stack_depth;
356 void* stack_addrs[max_depth];
357 char** stack_strings;
358 stack_depth = ::backtrace(stack_addrs, max_depth);
359 stack_strings = backtrace_symbols(stack_addrs, stack_depth);
360 std::ostringstream ost;
361 for (size_t i = 1; i < stack_depth; i++) {
362 ost << stack_strings[i] << '\n';
363 }
364 free(stack_strings); // malloc()ed by backtrace_symbols
365 return ost.str();
366#else
367 // Not implemented on this platform
368 return {};
369#endif
370}
371
372inline void forwardable_output(std::ostream* ost,
374 const char* msg,
375 const char* file,
376 const unsigned int line,
377 const char* func,
378 double systemtime,
379 double networktime,
380 double externaltime,
381 const char* comp_name,
382 const char* id)
383{
384 const char *level = logTypeToString(t);
385
386 // This generates the same string a Property containing the following keys:
387 // * level
388 // * systemtime
389 // * networktime (if not using system time)
390 // * externaltime (if defined)
391 // * filename (if YARP_FORWARD_CODEINFO_ENABLE is enabled)
392 // * line (if YARP_FORWARD_CODEINFO_ENABLE is enabled)
393 // * function (if YARP_FORWARD_CODEINFO_ENABLE is enabled)
394 // * hostname (if YARP_FORWARD_HOSTNAME_ENABLE is enabled)
395 // * cmd (if YARP_FORWARD_PROCESSINFO_ENABLE is enabled)
396 // * args (if YARP_FORWARD_PROCESSINFO_ENABLE is enabled)
397 // * pid (if YARP_FORWARD_PROCESSINFO_ENABLE is enabled)
398 // * thread_id (if YARP_FORWARD_PROCESSINFO_ENABLE is enabled)
399 // * component (if defined)
400 // * id (if defined)
401 // * message (if any)
402 // * backtrace (for FATAL or if requested using YARP_FORWARD_BACKTRACE_ENABLE)
403
404 *ost << "(level " << yarp::os::impl::StoreString::quotedString(level) << ")";
405 *ost << " (systemtime " << yarp::conf::numeric::to_string(systemtime) << ")";
407 *ost << " (networktime " << yarp::conf::numeric::to_string(networktime) << ")";
408 }
409 if (externaltime != 0.0) {
410 *ost << " (externaltime " << yarp::conf::numeric::to_string(externaltime) << ")";
411 }
413 *ost << " (filename " << yarp::os::impl::StoreString::quotedString(file) << ")";
414 *ost << " (line " << line << ")";
415 *ost << " (function " << yarp::os::impl::StoreString::quotedString(func) << ")";
416 }
418 static std::string hostname(yarp::os::gethostname());
419 *ost << " (hostname " << yarp::os::impl::StoreString::quotedString(hostname) << ")";
420 }
423 static std::string cmd(processInfo.name.substr(processInfo.name.find_last_of("\\/") + 1));
424 thread_local long thread_id(yarp::os::impl::ThreadImpl::getKeyOfCaller());
425 *ost << " (pid " << processInfo.pid << ")";
426 *ost << " (cmd " << yarp::os::impl::StoreString::quotedString(cmd) << ")";
427 *ost << " (args " << yarp::os::impl::StoreString::quotedString(processInfo.arguments) << ")";
428 *ost << " (thread_id 0x" << std::setfill('0') << std::setw(8) << yarp::conf::numeric::to_hex_string(thread_id) << ")";
429 }
430 if (comp_name) {
431 *ost << " (component " << yarp::os::impl::StoreString::quotedString(comp_name) << ")";
432 }
433 if (id && id[0] != '\0') {
434 *ost << " (id " << yarp::os::impl::StoreString::quotedString(id) << ")";
435 }
436 if (msg[0]) {
437 *ost << " (message " << yarp::os::impl::StoreString::quotedString(msg) << ")";
438 }
440 *ost << " (backtrace " << yarp::os::impl::StoreString::quotedString(backtrace()) << ")";
441 }
442}
443
444inline void printable_output(std::ostream* ost,
446 const char* msg,
447 const char* file,
448 const unsigned int line,
449 const char* func,
450 double systemtime,
451 double networktime,
452 double externaltime,
453 const char* comp_name,
454 const char* id)
455{
456 YARP_UNUSED(file);
457 YARP_UNUSED(line);
458 YARP_UNUSED(systemtime);
459 YARP_UNUSED(networktime);
460 YARP_UNUSED(externaltime);
461
462#if !defined (_MSC_VER)
463 static constexpr const char level_char[] = "\u25cf";
464#else
465 static constexpr const char* level_char = "*";
466#endif
467
468 const char* level_string = logTypeToString(t);
469 const char* level_color = logTypeToColor(t);
470 const char* level_bgcolor = logTypeToBgColor(t);
471 static const char *reserved_color = logTypeToColor(yarp::os::Log::LogTypeReserved);
472 const char* comp_color = compNameToColor(comp_name);
473 const char* id_color = idToColor(id);
474
475 // Print Level
477 *ost << level_color << level_bgcolor << level_char << CLEAR << " ";
478 } else {
479 *ost << "[" << level_color << level_bgcolor << level_string << CLEAR << "] ";
480 }
481
482 // Print function information (trace only)
483 if (t == yarp::os::Log::TraceType) {
484 *ost << level_color << func << CLEAR << ((msg[0] || comp_name) ? ": " : "");
485 }
486
487 // Print component and id
488 if (comp_name) {
489 *ost << "|" << comp_color << comp_name << CLEAR << "|";
490 }
491 if (id && id[0] != '\0') {
492 if (!comp_name) {
493 *ost << "|";
494 }
495 *ost << id_color << id << CLEAR << "| ";
496 } else if (comp_name) {
497 *ost << " ";
498 }
499
500 // Finally print the message
502 *ost << reserved_color << '^' << CLEAR;
503 }
504 *ost << msg;
506 *ost << reserved_color << '$' << CLEAR;
507 }
508}
509
510inline void printable_output_verbose(std::ostream* ost,
512 const char* msg,
513 const char* file,
514 const unsigned int line,
515 const char* func,
516 double systemtime,
517 double networktime,
518 double externaltime,
519 const char* comp_name,
520 const char* id)
521{
522 YARP_UNUSED(systemtime);
523
524 const char* level_string = logTypeToString(t);
525 const char *level_color = logTypeToColor(t);
526 const char *level_bgcolor = logTypeToBgColor(t);
527 static const char *reserved_color = logTypeToColor(yarp::os::Log::LogTypeReserved);
528 const char* comp_color = compNameToColor(comp_name);
529 const char* id_color = idToColor(comp_name);
530
531 // Print external time
532 if (externaltime != 0.0) {
533 *ost << "[" << std::fixed << externaltime << "] ";
534 }
535 else {
536 // Print time
537 *ost << "[" << std::fixed << networktime << "] ";
538 }
539
540 // Print level
541 *ost << "[" << level_color << level_bgcolor << level_string << CLEAR << "] ";
542
543 // Print file, line and function
544 *ost << file << ":" << line << " " << level_color << level_bgcolor << func << CLEAR << " ";
545
546 // Print thread id
547 *ost << "(0x" << std::setfill('0') << std::setw(8) << yarp::conf::numeric::to_hex_string(yarp::os::impl::ThreadImpl::getKeyOfCaller()) << ") ";
548
549 // Print component and id
550 if (comp_name) {
551 *ost << "|" << comp_color << comp_name << CLEAR << "|";
552 }
553 if (id && id[0] != '\0') {
554 if (!comp_name) {
555 *ost << "|";
556 }
557 *ost << id_color << id << CLEAR << "| ";
558 } else if (comp_name) {
559 *ost << " ";
560 }
561
562 // Finally print the message
564 *ost << reserved_color << '^' << CLEAR;
565 }
566 *ost << msg;
568 *ost << reserved_color << '$' << CLEAR;
569 }
570}
571
572} // namespace
573
574// END Utilities
575
576// BEGIN LogPrivate static variables initialization
577
578// BEGIN Environment variables
579std::atomic<bool> yarp::os::impl::LogPrivate::yarprun_format(yarp::conf::environment::get_bool("YARP_IS_YARPRUN", false) &&
580 yarp::conf::environment::get_bool("YARPRUN_IS_FORWARDING_LOG", false));
581
582#if defined(_WIN32) && !defined(YARP_HAS_WIN_VT_SUPPORT)
583std::atomic<bool> yarp::os::impl::LogPrivate::colored_output(false);
584#else
585std::atomic<bool> yarp::os::impl::LogPrivate::colored_output(yarp::conf::environment::get_bool("YARP_COLORED_OUTPUT", false) &&
587#endif
588std::atomic<bool> yarp::os::impl::LogPrivate::verbose_output(yarp::conf::environment::get_bool("YARP_VERBOSE_OUTPUT", false) &&
590std::atomic<bool> yarp::os::impl::LogPrivate::compact_output(yarp::conf::environment::get_bool("YARP_COMPACT_OUTPUT", false) &&
593std::atomic<bool> yarp::os::impl::LogPrivate::forward_output(yarp::conf::environment::get_bool("YARP_FORWARD_LOG_ENABLE", false) &&
595
596// The following 4 environment variables are to be considered experimental
597// until we have a reason to believe that this extra traffic does not impact
598// on the performances (and that all these info are actually useful).
599std::atomic<bool> yarp::os::impl::LogPrivate::forward_codeinfo(yarp::conf::environment::get_bool("YARP_FORWARD_CODEINFO_ENABLE", false));
600std::atomic<bool> yarp::os::impl::LogPrivate::forward_hostname(yarp::conf::environment::get_bool("YARP_FORWARD_HOSTNAME_ENABLE", false));
601std::atomic<bool> yarp::os::impl::LogPrivate::forward_processinfo(yarp::conf::environment::get_bool("YARP_FORWARD_PROCESSINFO_ENABLE", false));
602std::atomic<bool> yarp::os::impl::LogPrivate::forward_backtrace(yarp::conf::environment::get_bool("YARP_FORWARD_BACKTRACE_ENABLE", false));
603
604std::atomic<bool> yarp::os::impl::LogPrivate::debug_output(yarp::conf::environment::get_bool("YARP_DEBUG_ENABLE", true));
605std::atomic<bool> yarp::os::impl::LogPrivate::trace_output(yarp::conf::environment::get_bool("YARP_TRACE_ENABLE", false) &&
607
608std::atomic<bool> yarp::os::impl::LogPrivate::debug_log(yarp::conf::environment::get_bool("YARP_DEBUG_LOG_ENABLE", false));
609// END Environment variables
610
611#ifdef YARP_HAS_WIN_VT_SUPPORT
612std::atomic<bool> yarp::os::impl::LogPrivate::vt_colors_enabled = false;
613#endif
614
615
616std::atomic<yarp::os::Log::LogType> yarp::os::impl::LogPrivate::current_minimum_print_level(
618 (!yarp::os::impl::LogPrivate::debug_output.load() ? yarp::os::Log::InfoType :
619 yarp::os::Log::defaultMinimumPrintLevel())));
620
621std::atomic<yarp::os::Log::LogType> yarp::os::impl::LogPrivate::current_minimum_forward_level(
623 : yarp::os::Log::LogTypeReserved);
624
626
627std::atomic<yarp::os::Log::LogCallback> yarp::os::impl::LogPrivate::current_forward_callback(
629 : nullptr);
630
631// Log internal component never forwards the output and, if enabled, prints
632// all the output using the default print callback
634 "yarp.os.Log",
636 : yarp::os::Log::LogTypeReserved,
637 yarp::os::Log::LogTypeReserved,
638 yarp::os::impl::LogPrivate::debug_log.load() ? yarp::os::impl::LogPrivate::call_current_print_callback
639 : nullptr,
640 nullptr);
641
642
643// END LogPrivate static variables initialization
644
645// BEGIN LogPrivate methods
646
648 unsigned int line,
649 const char* func,
650 const char* id,
651 const double externaltime,
652 const yarp::os::Log::Predicate pred,
653 const LogComponent& comp) :
654 file(file),
655 line(line),
656 func(func),
657 systemtime(yarp::os::SystemClock::nowSystem()),
658 networktime(!yarp::os::NetworkBase::isNetworkInitialized() ? 0.0 : (yarp::os::Time::isSystemClock() ? systemtime : yarp::os::Time::now())),
659 externaltime(externaltime),
660 pred(pred),
661 comp(comp),
662 id(id)
663{
664#ifdef YARP_HAS_WIN_VT_SUPPORT
665 if (colored_output.load() && !yarp::os::impl::LogPrivate::vt_colors_enabled.load()) {
667 }
668#endif
669}
670
672 const char* msg,
673 const char* file,
674 const unsigned int line,
675 const char* func,
676 double systemtime,
677 double networktime,
678 double externaltime,
679 const char* comp_name,
680 const char* id)
681{
682 std::ostream *ost;
683 if(t == yarp::os::Log::TraceType ||
686 ost = &std::cout;
687 } else {
688 ost = &std::cerr;
689 }
690
691 static std::mutex log_mutex;
692 std::lock_guard<std::mutex> lock(log_mutex);
693
694 if (yarprun_format.load()) {
695 // Same output as forward_callback
696 forwardable_output(ost, t, msg, file, line, func, systemtime, networktime, externaltime, comp_name, id);
697 } else if (verbose_output.load()) {
698 printable_output_verbose(ost, t, msg, file, line, func, systemtime, networktime, externaltime, comp_name, id);
699 } else {
700 printable_output(ost, t, msg, file, line, func, systemtime, networktime, externaltime, comp_name, id);
701 }
702 *ost << std::endl;
703}
704
706 const char* msg,
707 const char* file,
708 const unsigned int line,
709 const char* func,
710 double systemtime,
711 double networktime,
712 double externaltime,
713 const char* comp_name,
714 const char* id)
715{
717 // Network is not initialized. Don't forward any log.
718 // And avoid creating the LogForwarder!
719 return;
720 }
721 std::stringstream stringstream_buffer;
722 forwardable_output(&stringstream_buffer, t, msg, file, line, func, systemtime, networktime, externaltime, comp_name, id);
724}
725
727 const char* msg,
728 va_list args) const
729{
730 constexpr size_t YARP_MAX_STATIC_LOG_MSG_SIZE = 1024;
731
732 if (msg != nullptr) {
733 if (!pred || pred()) {
735 char* dyn_buf = nullptr;
736
737 char* out = buf;
739
740 // Save va_list status in case we need to repeat the operation with
741 // the dynamically allocated buffer (vsnprintf leaves it in an
742 // undefined status).
744 va_copy(args_bak, args);
745
746 auto log_line_size = static_cast<size_t>(std::vsnprintf(buf, YARP_MAX_STATIC_LOG_MSG_SIZE, msg, args));
747
749 // The static buffer is not big enough for the log line.
750 // Using a dynamic buffer instead.
751 // Round up size to the first power of 2.
752 size_t dyn_buf_size = [](size_t x) {
753 --x;
754 x |= x >> 1;
755 x |= x >> 2;
756 x |= x >> 4;
757 x |= x >> 8;
758 x |= x >> 16;
759 return x + 1;
760 }(log_line_size);
761
762 dyn_buf = new char[dyn_buf_size];
763 std::vsnprintf(dyn_buf, dyn_buf_size, msg, args_bak);
765
766 out = dyn_buf;
768 }
769
770 auto p = std::min<size_t>(log_line_size - 1, buf_size);
771 if (log_line_size > 0 && p < buf_size && out[p] == '\n' && msg[strlen(msg) - 1] == '\n') {
772 yarp::os::Log(file, line, func, nullptr, log_internal_component).warning("Removing extra '\\n' (c-style)");
773 out[p] = 0;
774 }
775
776 do_log(type, out, file, line, func, systemtime, networktime, externaltime, comp, id);
777
778 if (dyn_buf) {
779 yarp::os::Log(file, line, func, nullptr, log_internal_component).warning("Previous message was longer than the static buffer size, dynamic allocation was used");
780 delete[] dyn_buf;
781 }
782 }
783 } else {
784 yarp::os::Log(file, line, func, nullptr, log_internal_component).warning("Unexpected nullptr received");
785 }
786}
787
789 const char* msg,
790 const char* file,
791 const unsigned int line,
792 const char* func,
793 double systemtime,
794 double networktime,
795 double externaltime,
796 const LogComponent& comp,
797 const std::string_view id)
798{
799 auto* print_cb = (yarprun_format && comp != log_internal_component) ? yarp::os::impl::LogPrivate::print_callback : comp.printCallback(type);
800 if (print_cb) {
801 print_cb(type, msg, file, line, func, systemtime, networktime, externaltime, comp.name(), id.data());
802 } else {
803 if (comp != log_internal_component) {
804 if (comp.name()) {
805 yarp::os::Log(file, line, func, nullptr, log_internal_component).debug("Not printing [%s][%s]", comp.name(), msg);
806 } else {
807 yarp::os::Log(file, line, func, nullptr, log_internal_component).debug("Not printing [%s]", msg);
808 }
809 }
810 }
811
812 auto* forward_cb = comp.forwardCallback(type);
813 if(forward_cb) {
814 forward_cb(type, msg, file, line, func, systemtime, networktime, externaltime, comp.name(), id.data());
815 } else {
816 if (comp != log_internal_component) {
817 if (comp.name()) {
818 yarp::os::Log(file, line, func, nullptr, log_internal_component).debug("Not forwarding [%s][%s]", comp.name(), msg);
819 } else {
820 yarp::os::Log(file, line, func, nullptr, log_internal_component).debug("Not forwarding [%s]", msg);
821 }
822 }
823 }
824}
825
826#ifdef YARP_HAS_WIN_VT_SUPPORT
827bool yarp::os::impl::LogPrivate::enable_vt_colors()
828{
829 DWORD handleMode = 0;
831 bool success = false;
832
836 }
837
838 yarp::os::impl::LogPrivate::vt_colors_enabled = true;
839 return success;
840}
841#endif
842
843// END LogPrivate methods
844
845
846// BEGIN Minimum Print Level
851
856
861// END Minimum Print Level
862
863
864// BEGIN Minimum Forward Level
871
876
881
882// END Minimum Forward Level
883
884
885// BEGIN Print Callback
886
891
896
901
902// END Print Callback
903
904
905// BEGIN Forward Callback
906
913
918
923
924// END Forward Callback
925
926
927// BEGIN Log Components
928const yarp::os::LogComponent& yarp::os::Log::defaultLogComponent()
929{
930 static const yarp::os::LogComponent component(nullptr,
931 minimumPrintLevel(),
932 minimumForwardLevel(),
933 printCallback(),
934 forwardCallback());
935 return component;
936}
937
938const yarp::os::LogComponent& yarp::os::Log::logInternalComponent()
939{
941}
942// END Log Components
943
944
945yarp::os::Log::Log(const char* file,
946 unsigned int line,
947 const char* func,
948 const Predicate pred,
949 const LogComponent& comp) :
950 mPriv(std::make_unique<yarp::os::impl::LogPrivate>(file, line, func, "", 0.0, pred, comp))
951{
952}
953
954// method for Log with id
955yarp::os::Log::Log(const char* file,
956 unsigned int line,
957 const char* func,
958 std::string_view id,
959 const Predicate pred,
960 const LogComponent& comp) :
961 mPriv(std::make_unique<yarp::os::impl::LogPrivate>(file, line, func, id.data(), 0.0, pred, comp))
962{
963}
964
965// method for Log with externaltime
966yarp::os::Log::Log(const char* file,
967 unsigned int line,
968 const char* func,
969 const double externaltime,
970 const Predicate pred,
971 const LogComponent& comp) :
972 mPriv(std::make_unique<yarp::os::impl::LogPrivate>(file, line, func, "", externaltime, pred, comp))
973{
974}
975
976// method for Log with id and externaltime
977
978yarp::os::Log::Log(const char* file,
979 unsigned int line,
980 const char* func,
981 std::string_view id,
982 const double externaltime,
983 const Predicate pred,
984 const LogComponent& comp) :
985 mPriv(std::make_unique<yarp::os::impl::LogPrivate>(file, line, func, id.data(), externaltime, pred, comp))
986{
987}
988
990 mPriv(std::make_unique<yarp::os::impl::LogPrivate>(nullptr, 0, nullptr, nullptr, 0.0, nullptr, nullptr))
991{
992}
993
997
998void yarp::os::Log::do_log(yarp::os::Log::LogType type,
999 const char* msg,
1000 const char* file,
1001 const unsigned int line,
1002 const char* func,
1003 double systemtime,
1004 double networktime,
1005 double externaltime,
1006 const LogComponent& comp,
1007 const std::string_view id)
1008{
1009 yarp::os::impl::LogPrivate::do_log(type, msg, file, line, func, systemtime, networktime, externaltime, comp, id);
1010}
1011
1012
1013void yarp::os::Log::trace(const char* msg, ...) const
1014{
1015 va_list args;
1016 va_start(args, msg);
1017 mPriv->log(yarp::os::Log::TraceType, msg, args);
1018 va_end(args);
1019}
1020
1022{
1024 mPriv->file,
1025 mPriv->line,
1026 mPriv->func,
1027 mPriv->id,
1028 mPriv->externaltime,
1029 mPriv->pred,
1030 mPriv->comp);
1031}
1032
1033
1034void yarp::os::Log::debug(const char* msg, ...) const
1035{
1036 va_list args;
1037 va_start(args, msg);
1038 mPriv->log(yarp::os::Log::DebugType, msg, args);
1039 va_end(args);
1040}
1041
1043{
1045 mPriv->file,
1046 mPriv->line,
1047 mPriv->func,
1048 mPriv->id,
1049 mPriv->externaltime,
1050 mPriv->pred,
1051 mPriv->comp);
1052}
1053
1054void yarp::os::Log::info(const char* msg, ...) const
1055{
1056 va_list args;
1057 va_start(args, msg);
1058 mPriv->log(yarp::os::Log::InfoType, msg, args);
1059 va_end(args);
1060}
1061
1063{
1065 mPriv->file,
1066 mPriv->line,
1067 mPriv->func,
1068 mPriv->id,
1069 mPriv->externaltime,
1070 mPriv->pred,
1071 mPriv->comp);
1072}
1073
1074
1075void yarp::os::Log::warning(const char* msg, ...) const
1076{
1077 va_list args;
1078 va_start(args, msg);
1079 mPriv->log(yarp::os::Log::WarningType, msg, args);
1080 va_end(args);
1081}
1082
1084{
1086 mPriv->file,
1087 mPriv->line,
1088 mPriv->func,
1089 mPriv->id,
1090 mPriv->externaltime,
1091 mPriv->pred,
1092 mPriv->comp);
1093}
1094
1095
1096void yarp::os::Log::error(const char* msg, ...) const
1097{
1098 va_list args;
1099 va_start(args, msg);
1100 mPriv->log(yarp::os::Log::ErrorType, msg, args);
1101 va_end(args);
1102}
1103
1105{
1107 mPriv->file,
1108 mPriv->line,
1109 mPriv->func,
1110 mPriv->id,
1111 mPriv->externaltime,
1112 mPriv->pred,
1113 mPriv->comp);
1114}
1115
1116void yarp::os::Log::fatal(const char* msg, ...) const
1117{
1118 va_list args;
1119 va_start(args, msg);
1120 mPriv->log(yarp::os::Log::FatalType, msg, args);
1121 va_end(args);
1122 yarp_print_trace(stderr, mPriv->file, mPriv->line);
1123 std::exit(-1);
1124}
1125
1127{
1129 mPriv->file,
1130 mPriv->line,
1131 mPriv->func,
1132 mPriv->id,
1133 mPriv->externaltime,
1134 mPriv->pred,
1135 mPriv->comp);
1136}
1137
1138void yarp_print_trace(FILE* out, const char* file, unsigned int line)
1139{
1140 fprintf(out, "Trace requested at %s:%u by code called from:\n", file, line);
1141 fprintf(out, "%s", backtrace().c_str());
1142 fflush(out);
1143}
#define BOLD_MAGENTA
Definition Log.cpp:182
#define BOLD_BLUE
Definition Log.cpp:181
#define BOLD_GREEN
Definition Log.cpp:179
#define BRI_CYAN
Definition Log.cpp:197
#define BOLD_YELLOW
Definition Log.cpp:180
#define BRI_MAGENTA
Definition Log.cpp:196
#define BOLD_RED
Definition Log.cpp:178
#define BRI_WHITE
Definition Log.cpp:198
#define CLEAR
Definition Log.cpp:206
#define BOLD_CYAN
Definition Log.cpp:183
#define MAGENTA
Definition Log.cpp:189
#define BLUE
Definition Log.cpp:188
#define BRI_BLUE
Definition Log.cpp:195
#define WHITE
Definition Log.cpp:191
#define BOLD_WHITE
Definition Log.cpp:184
void yarp_print_trace(FILE *out, const char *file, unsigned int line)
Low level function for printing a stack trace, if implemented (ACE or gcc/Linux).
Definition Log.cpp:1138
#define YELLOW
Definition Log.cpp:187
#define GREEN
Definition Log.cpp:186
#define CYAN
Definition Log.cpp:190
#define BRI_GREEN
Definition Log.cpp:193
#define RED_BG
Definition Log.cpp:199
#define BRI_YELLOW
Definition Log.cpp:194
A mini-server for performing network communication in the background.
constexpr const char * name() const
LogCallback printCallback(yarp::os::Log::LogType t) const
LogCallback forwardCallback(yarp::os::Log::LogType t) const
LogStream trace() const
Definition Log.cpp:1021
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:892
void warning(const char *msg,...) const
Definition Log.cpp:1075
static LogType defaultMinimumPrintLevel()
Get default minimum print level.
Definition Log.cpp:857
virtual ~Log()
Definition Log.cpp:994
void debug(const char *msg,...) const
Definition Log.cpp:1034
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:852
static LogCallback forwardCallback()
Get current forward callback (or nullptr if forwarding is not enabled)
Definition Log.cpp:914
static LogType minimumForwardLevel()
Get current minimum forward level (or LogTypeReserved if forwarding is not enabled)
Definition Log.cpp:872
LogStream error() const
Definition Log.cpp:1104
LogStream warning() const
Definition Log.cpp:1083
static void setPrintCallback(LogCallback)
Set current print callback.
Definition Log.cpp:887
LogStream debug() const
Definition Log.cpp:1042
@ ErrorType
Definition Log.h:96
@ LogTypeReserved
Definition Log.h:98
@ DebugType
Definition Log.h:93
@ TraceType
Definition Log.h:92
@ InfoType
Definition Log.h:94
@ FatalType
Definition Log.h:97
@ WarningType
Definition Log.h:95
static LogType defaultMinimumForwardLevel()
Get default minimum forward level (or LogTypeReserved if forwarding is not enabled)
Definition Log.cpp:877
static void setForwardCallback(LogCallback)
Set current forward callback (it does nothing if forwarding is not enabled)
Definition Log.cpp:907
LogStream fatal() const
Definition Log.cpp:1126
static void setMinimumForwardLevel(LogType level)
Set current minimum forward level (it does nothing if forwarding is not enabled)
Definition Log.cpp:865
static LogCallback defaultPrintCallback()
Get default print callback.
Definition Log.cpp:897
LogStream info() const
Definition Log.cpp:1062
void(*)(yarp::os::Log::LogType type, const char *msg, const char *file, const unsigned int line, const char *func, double systemtime, double networktime, double externaltime, const char *comp_name, const char *id) LogCallback
Definition Log.h:130
static LogCallback defaultForwardCallback()
Get default forward callback (or nullptr if forwarding is not enabled)
Definition Log.cpp:919
static void setMinimumPrintLevel(LogType level)
Set current minimum print level.
Definition Log.cpp:847
Utilities for manipulating the YARP network, excluding initialization and shutdown.
Definition Network.h:38
static bool isNetworkInitialized()
Definition Network.cpp:945
static ProcessInfo getProcessInfo(int pid=0)
gets the operating system process information given by its PID.
static LogForwarder & getInstance()
static std::atomic< bool > forward_hostname
Definition Log.cpp:159
static std::atomic< Log::LogCallback > current_forward_callback
Definition Log.cpp:171
const unsigned int line
Definition Log.cpp:142
static std::atomic< bool > debug_output
Definition Log.cpp:155
void log(yarp::os::Log::LogType type, const char *msg, va_list args) const
Definition Log.cpp:726
static std::atomic< bool > forward_codeinfo
Definition Log.cpp:158
static void forward_callback(yarp::os::Log::LogType t, const char *msg, const char *file, const unsigned int line, const char *func, double systemtime, double networktime, double externaltime, const char *comp_name, const char *id)
Definition Log.cpp:705
static void print_callback(yarp::os::Log::LogType t, const char *msg, const char *file, const unsigned int line, const char *func, double systemtime, double networktime, double externaltime, const char *comp_name, const char *id)
Definition Log.cpp:671
static std::atomic< yarp::os::Log::LogType > current_minimum_print_level
Definition Log.cpp:167
static std::atomic< bool > colored_output
Definition Log.cpp:152
static std::atomic< Log::LogCallback > current_print_callback
Definition Log.cpp:170
const yarp::os::Log::Predicate pred
Definition Log.cpp:147
static std::atomic< bool > forward_processinfo
Definition Log.cpp:160
static std::atomic< yarp::os::Log::LogType > current_minimum_forward_level
Definition Log.cpp:168
static std::atomic< bool > trace_output
Definition Log.cpp:156
static std::atomic< bool > verbose_output
Definition Log.cpp:153
static std::atomic< bool > debug_log
Definition Log.cpp:162
static const LogComponent & logInternalComponent()
LogPrivate(const char *file, const unsigned int line, const char *func, const char *id, const double externaltime, const yarp::os::Log::Predicate pred, const LogComponent &comp)
Definition Log.cpp:647
static const yarp::os::LogComponent log_internal_component
Definition Log.cpp:173
static std::atomic< bool > yarprun_format
Definition Log.cpp:151
static void call_current_print_callback(yarp::os::Log::LogType type, const char *msg, const char *file, const unsigned int line, const char *func, double systemtime, double networktime, double externaltime, const char *comp_name, const char *id)
Definition Log.cpp:101
static std::atomic< bool > forward_output
Definition Log.cpp:157
static std::atomic< bool > compact_output
Definition Log.cpp:154
static std::atomic< bool > forward_backtrace
Definition Log.cpp:161
static void call_current_forward_callback(yarp::os::Log::LogType type, const char *msg, const char *file, const unsigned int line, const char *func, double systemtime, double networktime, double externaltime, const char *comp_name, const char *id)
Definition Log.cpp:119
static void do_log(yarp::os::Log::LogType type, const char *msg, const char *file, const unsigned int line, const char *func, double systemtime, double networktime, double externaltime, const LogComponent &comp_name, const std::string_view id)
Definition Log.cpp:788
const LogComponent & comp
Definition Log.cpp:148
static std::string quotedString(const std::string &x)
Definition Storable.cpp:448
static long int getKeyOfCaller()
STL namespace.
bool get_bool(const std::string &key, bool defaultValue=false)
Read a bool value from an environment variable.
std::string to_hex_string(IntegerType i)
Definition numeric.h:322
std::string to_string(IntegerType x)
Definition numeric.h:116
bool isSystemClock()
Check if YARP is providing system time.
Definition Time.cpp:255
std::string gethostname()
Portable wrapper for the gethostname() function.
Definition Os.cpp:98
The main, catch-all namespace for YARP.
Definition dirs.h:16
The ProcessInfo struct provides the operating system process information.
Definition SystemInfo.h:112
#define YARP_UNUSED(var)
Definition api.h:162