36 # include <ace/Stack_Trace.h>
41 #elif defined(YARP_HAS_EXECINFO_H)
42 # include <execinfo.h>
45 #ifdef YARP_HAS_WIN_VT_SUPPORT
54 const unsigned int line,
67 const unsigned int line,
72 const char* comp_name);
77 const unsigned int line,
82 const char* comp_name);
88 const unsigned int line,
100 const unsigned int line,
105 const char* comp_name)
117 const unsigned int line,
122 const char* comp_name)
131 #ifdef YARP_HAS_WIN_VT_SUPPORT
132 bool enable_vt_colors();
156 #ifdef YARP_HAS_WIN_VT_SUPPORT
157 static std::atomic<bool> vt_colors_enabled;
171 #define BOLD_RED (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;31m" : "")
172 #define BOLD_GREEN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;32m" : "")
173 #define BOLD_YELLOW (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;33m" : "")
174 #define BOLD_BLUE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;34m" : "")
175 #define BOLD_MAGENTA (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;35m" : "")
176 #define BOLD_CYAN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;36m" : "")
177 #define BOLD_WHITE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[01;37m" : "")
178 #define RED (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[31m" : "")
179 #define GREEN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[32m" : "")
180 #define YELLOW (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[33m" : "")
181 #define BLUE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[34m" : "")
182 #define MAGENTA (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[35m" : "")
183 #define CYAN (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[36m" : "")
184 #define WHITE (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[37m" : "")
185 #define RED_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[41m" : "")
186 #define GREEN_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[42m" : "")
187 #define YELLOW_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[43m" : "")
188 #define BLUE_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[44m" : "")
189 #define MAGENTA_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[45m" : "")
190 #define CYAN_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[46m" : "")
191 #define WHITE_BG (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[47m" : "")
192 #define CLEAR (yarp::os::impl::LogPrivate::colored_output.load() ? "\033[00m" : "")
253 inline const char* compNameToColor(
const char* comp_name)
266 static std::hash<std::string> hsh;
298 std::string backtrace()
301 ACE_Stack_Trace st(-1);
305 #elif defined(YARP_HAS_EXECINFO_H)
306 const size_t max_depth = 100;
308 void* stack_addrs[max_depth];
309 char** stack_strings;
310 stack_depth = ::backtrace(stack_addrs, max_depth);
311 stack_strings = backtrace_symbols(stack_addrs, stack_depth);
312 std::ostringstream ost;
313 for (
size_t i = 1; i < stack_depth; i++) {
314 ost << stack_strings[i] <<
'\n';
324 inline void forwardable_output(std::ostream* ost,
328 const unsigned int line,
333 const char* comp_name)
335 const char *level = logTypeToString(
t);
359 if (externaltime != 0.0) {
364 *ost <<
" (line " << line <<
")";
373 static std::string cmd(processInfo.
name.substr(processInfo.
name.find_last_of(
"\\/") + 1));
375 *ost <<
" (pid " << processInfo.
pid <<
")";
391 inline void printable_output(std::ostream* ost,
395 const unsigned int line,
400 const char* comp_name)
408 #if !defined (_MSC_VER)
409 static constexpr
const char* level_char = u8
"\u25CF";
411 static constexpr
const char* level_char =
"*";
414 const char* level_string = logTypeToString(
t);
415 const char* level_color = logTypeToColor(
t);
416 const char* level_bgcolor = logTypeToBgColor(
t);
418 const char* comp_color = compNameToColor(comp_name);
422 *ost << level_color << level_bgcolor << level_char <<
CLEAR <<
" ";
424 *ost <<
"[" << level_color << level_bgcolor << level_string <<
CLEAR <<
"] ";
429 *ost << level_color << func <<
CLEAR << ((msg[0] || comp_name) ?
": " :
"");
434 *ost <<
"|" << comp_color << comp_name <<
CLEAR <<
"| ";
439 *ost << reserved_color <<
'^' <<
CLEAR;
443 *ost << reserved_color <<
'$' <<
CLEAR;
447 inline void printable_output_verbose(std::ostream* ost,
451 const unsigned int line,
456 const char* comp_name)
460 const char* level_string = logTypeToString(
t);
461 const char *level_color = logTypeToColor(
t);
462 const char *level_bgcolor = logTypeToBgColor(
t);
464 const char* comp_color = compNameToColor(comp_name);
467 if (externaltime != 0.0) {
468 *ost <<
"[" << std::fixed << externaltime <<
"] ";
472 *ost <<
"[" << std::fixed << networktime <<
"] ";
476 *ost <<
"[" << level_color << level_bgcolor << level_string <<
CLEAR <<
"] ";
479 *ost << file <<
":" << line <<
" " << level_color << level_bgcolor << func <<
CLEAR <<
" ";
486 *ost <<
"|" << comp_color << comp_name <<
CLEAR <<
"| ";
491 *ost << reserved_color <<
'^' <<
CLEAR;
495 *ost << reserved_color <<
'$' <<
CLEAR;
509 #if defined(_WIN32) && !defined(YARP_HAS_WIN_VT_SUPPORT)
538 #ifdef YARP_HAS_WIN_VT_SUPPORT
539 std::atomic<bool> yarp::os::impl::LogPrivate::vt_colors_enabled =
false;
577 const double externaltime,
585 externaltime(externaltime),
589 #ifdef YARP_HAS_WIN_VT_SUPPORT
590 if (
colored_output.load() && !yarp::os::impl::LogPrivate::vt_colors_enabled.load()) {
599 const unsigned int line,
604 const char* comp_name)
615 static std::mutex log_mutex;
616 std::lock_guard<std::mutex> lock(log_mutex);
618 if (yarprun_format.load()) {
620 forwardable_output(ost,
t, msg, file, line, func, systemtime, networktime, externaltime, comp_name);
621 }
else if (verbose_output.load()) {
622 printable_output_verbose(ost,
t, msg, file, line, func, systemtime, networktime, externaltime, comp_name);
624 printable_output(ost,
t, msg, file, line, func, systemtime, networktime, externaltime, comp_name);
632 const unsigned int line,
637 const char* comp_name)
644 std::stringstream stringstream_buffer;
645 forwardable_output(&stringstream_buffer,
t, msg, file, line, func, systemtime, networktime, externaltime, comp_name);
653 constexpr
size_t YARP_MAX_STATIC_LOG_MSG_SIZE = 1024;
655 if (msg !=
nullptr) {
656 if (!pred || pred()) {
657 char buf[YARP_MAX_STATIC_LOG_MSG_SIZE];
658 char* dyn_buf =
nullptr;
661 size_t buf_size = YARP_MAX_STATIC_LOG_MSG_SIZE;
667 va_copy(args_bak, args);
669 auto log_line_size =
static_cast<size_t>(std::vsnprintf(buf, YARP_MAX_STATIC_LOG_MSG_SIZE, msg, args));
671 if (log_line_size > YARP_MAX_STATIC_LOG_MSG_SIZE) {
675 size_t dyn_buf_size = [](
size_t x) {
685 dyn_buf =
new char[dyn_buf_size];
686 std::vsnprintf(dyn_buf, dyn_buf_size, msg, args_bak);
690 buf_size = dyn_buf_size;
693 auto p = std::min(log_line_size - 1, buf_size);
694 if (log_line_size > 0 && p < buf_size && out[p] ==
'\n' && msg[strlen(msg) - 1] ==
'\n') {
695 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
warning(
"Removing extra '\\n' (c-style)");
699 do_log(type, out, file, line, func, systemtime, networktime, externaltime, comp);
702 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
warning(
"Previous message was longer than the static buffer size, dynamic allocation was used");
707 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
warning(
"Unexpected nullptr received");
714 const unsigned int line,
723 print_cb(type, msg, file, line, func, systemtime, networktime, externaltime, comp.
name());
725 if (comp != log_internal_component) {
727 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
debug(
"Not printing [%s][%s]", comp.
name(), msg);
729 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
debug(
"Not printing [%s]", msg);
736 forward_cb(type, msg, file, line, func, systemtime, networktime, externaltime, comp.
name());
738 if (comp != log_internal_component) {
740 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
debug(
"Not forwarding [%s][%s]", comp.
name(), msg);
742 yarp::os::Log(file, line, func,
nullptr, log_internal_component).
debug(
"Not forwarding [%s]", msg);
748 #ifdef YARP_HAS_WIN_VT_SUPPORT
749 bool yarp::os::impl::LogPrivate::enable_vt_colors()
751 DWORD handleMode = 0;
752 HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
753 bool success =
false;
755 if (hStdout != INVALID_HANDLE_VALUE && GetConsoleMode(hStdout, &handleMode)) {
756 handleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
757 success = SetConsoleMode(hStdout, handleMode);
760 yarp::os::impl::LogPrivate::vt_colors_enabled =
true;
809 #ifndef YARP_NO_DEPRECATED
861 minimumForwardLevel(),
879 mPriv(new
yarp::os::impl::LogPrivate(file, line, func, 0.0, pred, comp))
887 const double externaltime,
890 mPriv(new
yarp::os::impl::LogPrivate(file, line, func, externaltime, pred, comp))
895 mPriv(new
yarp::os::impl::LogPrivate(nullptr, 0, nullptr, 0.0, nullptr, nullptr))
907 const unsigned int line,
1000 va_start(args, msg);
1011 mPriv->externaltime,
1019 va_start(args, msg);
1032 mPriv->externaltime,
1039 fprintf(out,
"Trace requested at %s:%u by code called from:\n", file, line);
1040 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
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) LogCallback
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.
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 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)
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 std::atomic< yarp::os::Log::LogType > current_minimum_print_level
static const LogComponent & logInternalComponent()
static std::atomic< bool > colored_output
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)
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 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)
static const yarp::os::LogComponent log_internal_component
static std::atomic< bool > yarprun_format
static std::atomic< bool > forward_output
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)
static std::atomic< bool > compact_output
static std::atomic< bool > forward_backtrace
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)
LogPrivate(const char *file, const unsigned int line, const char *func, const double externaltime, const yarp::os::Log::Predicate pred, const LogComponent &comp)
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.