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,
79 const unsigned int line,
91 const unsigned int line,
97 const std::string_view
id);
104 const unsigned int line,
122 const unsigned int line,
137#ifdef YARP_HAS_WIN_VT_SUPPORT
163#ifdef YARP_HAS_WIN_VT_SUPPORT
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" : "")
267inline const char* compNameToColor(
const char* comp_name)
280 static std::hash<std::string_view> hsh;
281 std::size_t comp_hash = hsh(comp_name) % 6;
301inline const char* idToColor(
const char*
id)
314 static std::hash<std::string_view> hsh;
315 std::size_t id_hash = hsh(
id) % 12;
346std::string backtrace()
349 ACE_Stack_Trace st(-1);
353#elif defined(YARP_HAS_EXECINFO_H)
354 const size_t max_depth = 100;
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';
372inline void forwardable_output(std::ostream* ost,
376 const unsigned int line,
381 const char* comp_name,
384 const char *level = logTypeToString(t);
409 if (externaltime != 0.0) {
414 *ost <<
" (line " << line <<
")";
423 static std::string cmd(processInfo.
name.substr(processInfo.
name.find_last_of(
"\\/") + 1));
425 *ost <<
" (pid " << processInfo.
pid <<
")";
433 if (
id &&
id[0] !=
'\0') {
444inline void printable_output(std::ostream* ost,
448 const unsigned int line,
453 const char* comp_name,
462#if !defined (_MSC_VER)
463 static constexpr const char level_char[] =
"\u25cf";
465 static constexpr const char* level_char =
"*";
468 const char* level_string = logTypeToString(t);
469 const char* level_color = logTypeToColor(t);
470 const char* level_bgcolor = logTypeToBgColor(t);
472 const char* comp_color = compNameToColor(comp_name);
473 const char* id_color = idToColor(
id);
477 *ost << level_color << level_bgcolor << level_char <<
CLEAR <<
" ";
479 *ost <<
"[" << level_color << level_bgcolor << level_string <<
CLEAR <<
"] ";
484 *ost << level_color << func <<
CLEAR << ((msg[0] || comp_name) ?
": " :
"");
489 *ost <<
"|" << comp_color << comp_name <<
CLEAR <<
"|";
491 if (
id &&
id[0] !=
'\0') {
495 *ost << id_color <<
id <<
CLEAR <<
"| ";
496 }
else if (comp_name) {
502 *ost << reserved_color <<
'^' <<
CLEAR;
506 *ost << reserved_color <<
'$' <<
CLEAR;
510inline void printable_output_verbose(std::ostream* ost,
514 const unsigned int line,
519 const char* comp_name,
524 const char* level_string = logTypeToString(t);
525 const char *level_color = logTypeToColor(t);
526 const char *level_bgcolor = logTypeToBgColor(t);
528 const char* comp_color = compNameToColor(comp_name);
529 const char* id_color = idToColor(comp_name);
532 if (externaltime != 0.0) {
533 *ost <<
"[" << std::fixed << externaltime <<
"] ";
537 *ost <<
"[" << std::fixed << networktime <<
"] ";
541 *ost <<
"[" << level_color << level_bgcolor << level_string <<
CLEAR <<
"] ";
544 *ost << file <<
":" << line <<
" " << level_color << level_bgcolor << func <<
CLEAR <<
" ";
551 *ost <<
"|" << comp_color << comp_name <<
CLEAR <<
"|";
553 if (
id &&
id[0] !=
'\0') {
557 *ost << id_color <<
id <<
CLEAR <<
"| ";
558 }
else if (comp_name) {
564 *ost << reserved_color <<
'^' <<
CLEAR;
568 *ost << reserved_color <<
'$' <<
CLEAR;
582#if defined(_WIN32) && !defined(YARP_HAS_WIN_VT_SUPPORT)
611#ifdef YARP_HAS_WIN_VT_SUPPORT
612std::atomic<bool> yarp::os::impl::LogPrivate::vt_colors_enabled =
false;
618 (!
yarp::os::impl::LogPrivate::debug_output.load() ?
yarp::os::Log::InfoType :
619 yarp::os::Log::defaultMinimumPrintLevel())));
623 :
yarp::os::Log::LogTypeReserved);
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
651 const double externaltime,
658 networktime(!
yarp::os::
NetworkBase::isNetworkInitialized() ? 0.0 : (
yarp::os::Time::isSystemClock() ? systemtime :
yarp::os::Time::now())),
659 externaltime(externaltime),
664#ifdef YARP_HAS_WIN_VT_SUPPORT
665 if (
colored_output.load() && !yarp::os::impl::LogPrivate::vt_colors_enabled.load()) {
674 const unsigned int line,
692 std::lock_guard<std::mutex> lock(
log_mutex);
694 if (yarprun_format.load()) {
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);
700 printable_output(
ost, t,
msg, file, line, func, systemtime, networktime, externaltime,
comp_name,
id);
708 const unsigned int line,
722 forwardable_output(&
stringstream_buffer, t,
msg, file, line, func, systemtime, networktime, externaltime,
comp_name,
id);
732 if (
msg !=
nullptr) {
733 if (!pred || pred()) {
772 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
warning(
"Removing extra '\\n' (c-style)");
776 do_log(type, out, file, line, func, systemtime, networktime, externaltime, comp,
id);
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");
784 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
warning(
"Unexpected nullptr received");
791 const unsigned int line,
797 const std::string_view
id)
801 print_cb(type,
msg, file, line, func, systemtime, networktime, externaltime, comp.
name(),
id.data());
803 if (comp != log_internal_component) {
814 forward_cb(type,
msg, file, line, func, systemtime, networktime, externaltime, comp.
name(),
id.data());
816 if (comp != log_internal_component) {
826#ifdef YARP_HAS_WIN_VT_SUPPORT
827bool yarp::os::impl::LogPrivate::enable_vt_colors()
838 yarp::os::impl::LogPrivate::vt_colors_enabled =
true;
932 minimumForwardLevel(),
950 mPriv(
std::
make_unique<
yarp::os::impl::LogPrivate>(file, line, func,
"", 0.0, pred, comp))
961 mPriv(
std::
make_unique<
yarp::os::impl::LogPrivate>(file, line, func, id.data(), 0.0, pred, comp))
969 const double externaltime,
972 mPriv(
std::
make_unique<
yarp::os::impl::LogPrivate>(file, line, func,
"", externaltime, pred, comp))
982 const double externaltime,
985 mPriv(
std::
make_unique<
yarp::os::impl::LogPrivate>(file, line, func, id.data(), externaltime, pred, comp))
990 mPriv(
std::
make_unique<
yarp::os::impl::LogPrivate>(nullptr, 0, nullptr, nullptr, 0.0, nullptr, nullptr))
1001 const unsigned int line,
1005 double externaltime,
1007 const std::string_view
id)
1028 mPriv->externaltime,
1049 mPriv->externaltime,
1069 mPriv->externaltime,
1090 mPriv->externaltime,
1111 mPriv->externaltime,
1133 mPriv->externaltime,
1140 fprintf(out,
"Trace requested at %s:%u by code called from:\n", file, line);
1141 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).
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
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.
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()
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)
bool isSystemClock()
Check if YARP is providing system time.
std::string gethostname()
Portable wrapper for the gethostname() function.
The main, catch-all namespace for YARP.
The ProcessInfo struct provides the operating system process information.