36# include <ace/Stack_Trace.h>
41#elif defined(YARP_HAS_EXECINFO_H)
45#ifdef YARP_HAS_WIN_VT_SUPPORT
54 const unsigned int line,
68 const unsigned int line,
73 const char* comp_name,
79 const unsigned int line,
84 const char* comp_name,
91 const unsigned int line,
97 const std::string_view
id);
104 const unsigned int line,
109 const char* comp_name,
122 const unsigned int line,
127 const char* comp_name,
137#ifdef YARP_HAS_WIN_VT_SUPPORT
138 bool enable_vt_colors();
163#ifdef YARP_HAS_WIN_VT_SUPPORT
164 static std::atomic<bool> vt_colors_enabled;
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 RED_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[41m" : "")
193#define GREEN_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[42m" : "")
194#define YELLOW_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[43m" : "")
195#define BLUE_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[44m" : "")
196#define MAGENTA_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[45m" : "")
197#define CYAN_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[46m" : "")
198#define WHITE_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[47m" : "")
199#define CLEAR (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[00m" : "")
260inline const char* compNameToColor(
const char* comp_name)
273 static std::hash<std::string_view> hsh;
294inline const char* idToColor(
const char*
id)
307 static std::hash<std::string_view> hsh;
327std::string backtrace()
330 ACE_Stack_Trace st(-1);
334#elif defined(YARP_HAS_EXECINFO_H)
335 const size_t max_depth = 100;
337 void* stack_addrs[max_depth];
338 char** stack_strings;
339 stack_depth = ::backtrace(stack_addrs, max_depth);
340 stack_strings = backtrace_symbols(stack_addrs, stack_depth);
341 std::ostringstream ost;
342 for (
size_t i = 1; i < stack_depth; i++) {
343 ost << stack_strings[i] <<
'\n';
353inline void forwardable_output(std::ostream* ost,
357 const unsigned int line,
362 const char* comp_name,
365 const char *level = logTypeToString(
t);
390 if (externaltime != 0.0) {
395 *ost <<
" (line " << line <<
")";
404 static std::string cmd(processInfo.
name.substr(processInfo.
name.find_last_of(
"\\/") + 1));
406 *ost <<
" (pid " << processInfo.
pid <<
")";
414 if (
id &&
id[0] !=
'\0') {
425inline void printable_output(std::ostream* ost,
429 const unsigned int line,
434 const char* comp_name,
443#if !defined (_MSC_VER)
444 static constexpr const char* level_char = u8
"\u25CF";
446 static constexpr const char* level_char =
"*";
449 const char* level_string = logTypeToString(
t);
450 const char* level_color = logTypeToColor(
t);
451 const char* level_bgcolor = logTypeToBgColor(
t);
453 const char* comp_color = compNameToColor(comp_name);
454 const char* id_color = idToColor(
id);
458 *ost << level_color << level_bgcolor << level_char <<
CLEAR <<
" ";
460 *ost <<
"[" << level_color << level_bgcolor << level_string <<
CLEAR <<
"] ";
465 *ost << level_color << func <<
CLEAR << ((msg[0] || comp_name) ?
": " :
"");
470 *ost <<
"|" << comp_color << comp_name <<
CLEAR <<
"|";
472 if (
id &&
id[0] !=
'\0') {
476 *ost << id_color <<
id <<
CLEAR <<
"| ";
477 }
else if (comp_name) {
483 *ost << reserved_color <<
'^' <<
CLEAR;
487 *ost << reserved_color <<
'$' <<
CLEAR;
491inline void printable_output_verbose(std::ostream* ost,
495 const unsigned int line,
500 const char* comp_name,
505 const char* level_string = logTypeToString(
t);
506 const char *level_color = logTypeToColor(
t);
507 const char *level_bgcolor = logTypeToBgColor(
t);
509 const char* comp_color = compNameToColor(comp_name);
510 const char* id_color = idToColor(comp_name);
513 if (externaltime != 0.0) {
514 *ost <<
"[" << std::fixed << externaltime <<
"] ";
518 *ost <<
"[" << std::fixed << networktime <<
"] ";
522 *ost <<
"[" << level_color << level_bgcolor << level_string <<
CLEAR <<
"] ";
525 *ost << file <<
":" << line <<
" " << level_color << level_bgcolor << func <<
CLEAR <<
" ";
532 *ost <<
"|" << comp_color << comp_name <<
CLEAR <<
"|";
534 if (
id &&
id[0] !=
'\0') {
538 *ost << id_color <<
id <<
CLEAR <<
"| ";
539 }
else if (comp_name) {
545 *ost << reserved_color <<
'^' <<
CLEAR;
549 *ost << reserved_color <<
'$' <<
CLEAR;
563#if defined(_WIN32) && !defined(YARP_HAS_WIN_VT_SUPPORT)
592#ifdef YARP_HAS_WIN_VT_SUPPORT
593std::atomic<bool> yarp::os::impl::LogPrivate::vt_colors_enabled =
false;
632 const double externaltime,
640 externaltime(externaltime),
645#ifdef YARP_HAS_WIN_VT_SUPPORT
646 if (
colored_output.load() && !yarp::os::impl::LogPrivate::vt_colors_enabled.load()) {
655 const unsigned int line,
660 const char* comp_name,
672 static std::mutex log_mutex;
673 std::lock_guard<std::mutex> lock(log_mutex);
675 if (yarprun_format.load()) {
677 forwardable_output(ost,
t, msg, file, line, func, systemtime, networktime, externaltime, comp_name,
id);
678 }
else if (verbose_output.load()) {
679 printable_output_verbose(ost,
t, msg, file, line, func, systemtime, networktime, externaltime, comp_name,
id);
681 printable_output(ost,
t, msg, file, line, func, systemtime, networktime, externaltime, comp_name,
id);
689 const unsigned int line,
694 const char* comp_name,
702 std::stringstream stringstream_buffer;
703 forwardable_output(&stringstream_buffer,
t, msg, file, line, func, systemtime, networktime, externaltime, comp_name,
id);
711 constexpr size_t YARP_MAX_STATIC_LOG_MSG_SIZE = 1024;
713 if (msg !=
nullptr) {
714 if (!pred || pred()) {
715 char buf[YARP_MAX_STATIC_LOG_MSG_SIZE];
716 char* dyn_buf =
nullptr;
719 size_t buf_size = YARP_MAX_STATIC_LOG_MSG_SIZE;
725 va_copy(args_bak, args);
727 auto log_line_size =
static_cast<size_t>(std::vsnprintf(buf, YARP_MAX_STATIC_LOG_MSG_SIZE, msg, args));
729 if (log_line_size > YARP_MAX_STATIC_LOG_MSG_SIZE) {
733 size_t dyn_buf_size = [](
size_t x) {
743 dyn_buf =
new char[dyn_buf_size];
744 std::vsnprintf(dyn_buf, dyn_buf_size, msg, args_bak);
748 buf_size = dyn_buf_size;
751 auto p = std::min(log_line_size - 1, buf_size);
752 if (log_line_size > 0 && p < buf_size && out[p] ==
'\n' && msg[strlen(msg) - 1] ==
'\n') {
753 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
warning(
"Removing extra '\\n' (c-style)");
757 do_log(type, out, file, line, func, systemtime, networktime, externaltime, comp,
id);
760 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
warning(
"Previous message was longer than the static buffer size, dynamic allocation was used");
765 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
warning(
"Unexpected nullptr received");
772 const unsigned int line,
778 const std::string_view
id)
782 print_cb(type, msg, file, line, func, systemtime, networktime, externaltime, comp.
name(),
id.data());
784 if (comp != log_internal_component) {
786 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
debug(
"Not printing [%s][%s]", comp.
name(), msg);
788 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
debug(
"Not printing [%s]", msg);
795 forward_cb(type, msg, file, line, func, systemtime, networktime, externaltime, comp.
name(),
id.data());
797 if (comp != log_internal_component) {
799 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
debug(
"Not forwarding [%s][%s]", comp.
name(), msg);
801 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
debug(
"Not forwarding [%s]", msg);
807#ifdef YARP_HAS_WIN_VT_SUPPORT
808bool yarp::os::impl::LogPrivate::enable_vt_colors()
810 DWORD handleMode = 0;
811 HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
812 bool success =
false;
814 if (hStdout != INVALID_HANDLE_VALUE && GetConsoleMode(hStdout, &handleMode)) {
815 handleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
816 success = SetConsoleMode(hStdout, handleMode);
819 yarp::os::impl::LogPrivate::vt_colors_enabled =
true;
868#ifndef YARP_NO_DEPRECATED
920 minimumForwardLevel(),
938 mPriv(new
yarp::os::impl::LogPrivate(file, line, func,
"", 0.0, pred, comp))
949 mPriv(new
yarp::os::impl::LogPrivate(file, line, func, id.data(), 0.0, pred, comp))
957 const double externaltime,
960 mPriv(new
yarp::os::impl::LogPrivate(file, line, func,
"", externaltime, pred, comp))
970 const double externaltime,
973 mPriv(new
yarp::os::impl::LogPrivate(file, line, func, id.data(), externaltime, pred, comp))
978 mPriv(new
yarp::os::impl::LogPrivate(nullptr, 0, nullptr, nullptr, 0.0, nullptr, nullptr))
990 const unsigned int line,
996 const std::string_view
id)
1005 va_start(args, msg);
1017 mPriv->externaltime,
1026 va_start(args, msg);
1038 mPriv->externaltime,
1046 va_start(args, msg);
1058 mPriv->externaltime,
1067 va_start(args, msg);
1079 mPriv->externaltime,
1088 va_start(args, msg);
1100 mPriv->externaltime,
1108 va_start(args, msg);
1122 mPriv->externaltime,
1129 fprintf(out,
"Trace requested at %s:%u by code called from:\n", file, line);
1130 fprintf(out,
"%s", backtrace().c_str());
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).
constexpr const char * name() const
LogCallback printCallback(yarp::os::Log::LogType t) const
LogCallback forwardCallback(yarp::os::Log::LogType t) const
static void setLogCallback(LogCallback)
static LogCallback printCallback()
Get current print callback.
void warning(const char *msg,...) const
static LogType defaultMinimumPrintLevel()
Get default minimum print level.
void debug(const char *msg,...) const
static LogType minimumPrintLevel()
Get current minimum print level.
static LogCallback forwardCallback()
Get current forward callback (or nullptr if forwarding is not enabled)
static LogType minimumForwardLevel()
Get current minimum forward level (or LogTypeReserved if forwarding is not enabled)
LogStream warning() const
static void setPrintCallback(LogCallback)
Set current print callback.
static LogType defaultMinimumForwardLevel()
Get default minimum forward level (or LogTypeReserved if forwarding is not enabled)
static void setForwardCallback(LogCallback)
Set current forward callback (it does nothing if forwarding is not enabled)
static void setMinimumForwardLevel(LogType level)
Set current minimum forward level (it does nothing if forwarding is not enabled)
static LogCallback defaultPrintCallback()
Get default print callback.
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
static LogCallback defaultForwardCallback()
Get default forward callback (or nullptr if forwarding is not enabled)
static void setMinimumPrintLevel(LogType level)
Set current minimum print level.
Utilities for manipulating the YARP network, excluding initialization and shutdown.
static bool isNetworkInitialized()
static ProcessInfo getProcessInfo(int pid=0)
gets the operating system process information given by its PID.
void forward(const std::string &message)
static LogForwarder & getInstance()
static std::atomic< bool > forward_hostname
static std::atomic< Log::LogCallback > current_forward_callback
static std::atomic< bool > debug_output
void log(yarp::os::Log::LogType type, const char *msg, va_list args) const
static std::atomic< bool > forward_codeinfo
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)
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)
static std::atomic< yarp::os::Log::LogType > current_minimum_print_level
static std::atomic< bool > colored_output
static std::atomic< Log::LogCallback > current_print_callback
const yarp::os::Log::Predicate pred
static std::atomic< bool > forward_processinfo
static std::atomic< yarp::os::Log::LogType > current_minimum_forward_level
static std::atomic< bool > trace_output
static std::atomic< bool > verbose_output
static std::atomic< bool > debug_log
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)
static const yarp::os::LogComponent log_internal_component
static std::atomic< bool > yarprun_format
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)
static std::atomic< bool > forward_output
static std::atomic< bool > compact_output
static std::atomic< bool > forward_backtrace
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)
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)
const LogComponent & comp
static std::string quotedString(const std::string &x)
static long int getKeyOfCaller()
yarp::rosmsg::std_msgs::Time Time
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)
std::string to_string(IntegerType x)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
bool isSystemClock()
Check if YARP is providing system time.
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
The main, catch-all namespace for YARP.
The ProcessInfo struct provides the operating system process information.