From 921d0944444fbf1fc7173b58fac7b47e0704217c Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Wed, 19 Sep 2018 10:26:20 +0200 Subject: [PATCH] Debug: use the rang library for color output --- src/debug.cpp | 81 +++++++++------------------------------------------ src/host.cpp | 1 - src/host.h | 1 - 3 files changed, 14 insertions(+), 69 deletions(-) diff --git a/src/debug.cpp b/src/debug.cpp index 817ad1b8c..1389c08cf 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -48,11 +48,7 @@ #include "debug.h" #include "host.h" - -#include -#ifdef WIN32 -#include -#endif +#include "rang.hpp" static void _dprintf(const char *format, ...) { @@ -79,9 +75,7 @@ static void _dprintf(const char *format, ...) void log_msg(int level, const char *format, ...) { - static std::mutex log_lock; va_list ap; - const char *color_nix = NULL; if (log_level < level) { return; @@ -100,87 +94,40 @@ void log_msg(int level, const char *format, ...) } #endif /* WIN32 */ -#ifdef WIN32 - HANDLE con; - bool no_color = true; - CONSOLE_SCREEN_BUFFER_INFO con_info; - uint32_t color_win = 0u; + rang::fg color = rang::fg::reset; + rang::style style = rang::style::reset; switch (level) { - case LOG_LEVEL_FATAL: color_win = FOREGROUND_RED | FOREGROUND_INTENSITY; break; - case LOG_LEVEL_ERROR: color_win = FOREGROUND_RED; break; - case LOG_LEVEL_WARNING: color_win = FOREGROUND_RED | FOREGROUND_GREEN; break; - case LOG_LEVEL_NOTICE: color_win = FOREGROUND_GREEN; break; + case LOG_LEVEL_FATAL: color = rang::fg::red; style = rang::style::bold; break; + case LOG_LEVEL_ERROR: color = rang::fg::red; break; + case LOG_LEVEL_WARNING: color = rang::fg::yellow; break; + case LOG_LEVEL_NOTICE: color = rang::fg::green; break; } -#else - switch (level) { - case LOG_LEVEL_FATAL: color_nix = "\033[1;31m"; break; - case LOG_LEVEL_ERROR: color_nix = "\033[0;31m"; break; - case LOG_LEVEL_WARNING: color_nix = "\033[0;33m"; break; - case LOG_LEVEL_NOTICE: color_nix = "\033[0;32m"; break; - } -#endif - - // format new format string - char *format_new = (char *) alloca(strlen(format) + 7 /* col start */ + 4 /* col end */ + - (3 + 20 /* 64b int dec */ + 1 /* dot */ + 3 /* ms */) /* time */ + 1); - format_new[0] = '\0'; - if (color_nix_term && color_nix) { - strcat(format_new, color_nix); - } + auto timestamp = (char *) alloca((3 /* "[] " */ + 20 /* 64b int dec */ + 1 /* dot */ + 3 /* ms */) /* time */ + 1); + timestamp[0] = '\0'; if (log_level >= LOG_LEVEL_VERBOSE) { unsigned long long time_ms = time_since_epoch_in_ms(); - sprintf(format_new + strlen(format_new), "[%llu.%03llu] ", time_ms / 1000, + sprintf(timestamp, "[%llu.%03llu] ", time_ms / 1000, time_ms % 1000); } - strcat(format_new, format); - if (color_nix_term && color_nix) { - strcat(format_new, "\033[0m"); - } // get number of required bytes va_start(ap, format); - int size = vsnprintf(NULL, 0, format_new, ap); + int size = vsnprintf(NULL, 0, format, ap); va_end(ap); // format the string char *buffer = (char *) alloca(size + 1); va_start(ap, format); - if (vsprintf(buffer, format_new, ap) != size) { + if (vsprintf(buffer, format, ap) != size) { va_end(ap); return; } va_end(ap); - // print it - log_lock.lock(); -#ifdef WIN32 - if (color_win != 0u) { - con = GetStdHandle(STD_ERROR_HANDLE); - no_color = con == INVALID_HANDLE_VALUE || getenv("NO_COLOR") - || (getenv("TERM") && strcmp(getenv("TERM"), "xterm") == 0); // MSYS2 pseudoterminal - if (!no_color) { - GetConsoleScreenBufferInfo(con, &con_info); - SetConsoleTextAttribute(con, ((con_info.wAttributes) & 0xF0) | color_win); - } - } -#endif - int written = 0; - do { - ssize_t ret = write(STDERR_FILENO, buffer + written, size - written); - if (ret <= 0) { - break; - } else { - written += ret; - } - } while (written < size); -#ifdef WIN32 - if (!no_color) { - SetConsoleTextAttribute(con, con_info.wAttributes); - } -#endif - log_lock.unlock(); + std::cerr << style << color << timestamp << + buffer << rang::style::reset << rang::fg::reset; } /** diff --git a/src/host.cpp b/src/host.cpp index 24b0631d0..2b20f3660 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -58,7 +58,6 @@ char *export_dir = NULL; volatile bool should_exit = false; volatile int log_level = LOG_LEVEL_INFO; -bool color_nix_term = (getenv("TERM") && set{"linux", "screen", "xterm", "xterm-256color"}.count(getenv("TERM")) > 0) && isatty(1) && isatty(2); volatile int audio_offset; volatile int video_offset; diff --git a/src/host.h b/src/host.h index 9647d7405..1bc1dfa95 100644 --- a/src/host.h +++ b/src/host.h @@ -105,7 +105,6 @@ extern unsigned int cuda_devices_count; #define LOG_LEVEL_DEBUG 7 #define LOG_LEVEL_MAX LOG_LEVEL_DEBUG extern volatile int log_level; -extern bool color_nix_term; #define MODE_SENDER (1<<0) #define MODE_RECEIVER (1<<1)