From 7719e8aa4790ec5351c9d4b60e68e95f837b31ba Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 9 Aug 2022 09:23:22 +0200 Subject: [PATCH] logger: get rid of rang references --- src/debug.cpp | 2 +- src/debug.h | 38 ++++++++++++++++---------------------- src/utils/color_out.cpp | 6 +++++- src/utils/color_out.h | 3 ++- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/debug.cpp b/src/debug.cpp index 59e5155a0..4fb0b6b13 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -357,5 +357,5 @@ thread_local std::string Log_output::buffer(initial_buf_size, '\0'); Log_output::Log_output(){ last_msg.reserve(initial_buf_size); - interactive = rang::rang_implementation::isTerminal(std::cout.rdbuf()); + interactive = color_output_init(); } diff --git a/src/debug.h b/src/debug.h index f00a217c1..cd289e984 100644 --- a/src/debug.h +++ b/src/debug.h @@ -115,7 +115,7 @@ bool parse_log_cfg(const char *conf_str, #include #include #include "compat/platform_time.h" -#include "rang.hpp" +#include "utils/color_out.h" class Log_output{ class Buffer{ @@ -184,39 +184,31 @@ private: }; inline const std::string& Log_output::get_level_style(int lvl){ + static std::string empty = ""; + + if (!is_interactive()) { + return empty; + } + switch(lvl){ case LOG_LEVEL_FATAL: { - static std::string style = [](){ - std::ostringstream o; o << rang::fg::red << rang::style::bold; - return o.str(); - }(); + static std::string style = TERM_BOLD TERM_FG_RED; return style; } case LOG_LEVEL_ERROR: { - static std::string style = [](){ - std::ostringstream o; o << rang::fg::red; - return o.str(); - }(); + static std::string style = TERM_FG_RED; return style; } case LOG_LEVEL_WARNING: { - static std::string style = [](){ - std::ostringstream o; o << rang::fg::yellow; - return o.str(); - }(); + static std::string style = TERM_FG_YELLOW; return style; } case LOG_LEVEL_NOTICE: { - static std::string style = [](){ - std::ostringstream o; o << rang::fg::green; - return o.str(); - }(); - return style; - } - default: { - static std::string style = ""; + static std::string style = TERM_FG_GREEN; return style; } + default: + return empty; } } @@ -278,7 +270,9 @@ public: } inline ~Logger() { - oss << rang::style::reset << rang::fg::reset; + if (get_log_output().is_interactive()) { + oss << TERM_RESET; + } std::string msg = oss.str(); diff --git a/src/utils/color_out.cpp b/src/utils/color_out.cpp index 1d1f502f9..1ee68e5b0 100644 --- a/src/utils/color_out.cpp +++ b/src/utils/color_out.cpp @@ -168,12 +168,16 @@ bool isMsysPty(int fd) { #endif // defined _WIN32 } -void color_output_init() { +/** + * @returns whether stdout can process ANSI escape sequences + */ +bool color_output_init() { #ifdef _WIN32 color_stdout = setWinTermAnsiColors(STD_OUTPUT_HANDLE) || isMsysPty(fileno(stdout)); #else color_stdout = isatty(fileno(stdout)); #endif + return color_stdout; } static int prune_ansi_sequences(const char *in, char *out) { diff --git a/src/utils/color_out.h b/src/utils/color_out.h index b5d9f0a47..8d246d94d 100644 --- a/src/utils/color_out.h +++ b/src/utils/color_out.h @@ -85,6 +85,7 @@ #define TERM_RESET "\e[0m" #define TERM_BOLD "\e[1m" #define TERM_FG_RED "\e[31m" +#define TERM_FG_GREEN "\e[32m" #define TERM_FG_YELLOW "\e[33m" #define TERM_FG_BLUE "\e[34m" #define TERM_FG_MAGENTA "\e[34m" @@ -102,7 +103,7 @@ extern "C" { void color_out(uint32_t modificators, const char *format, ...) ATTRIBUTE(format (printf, 2, 3)); // new API -void color_output_init(void); +bool color_output_init(void); int color_printf(const char *format, ...) ATTRIBUTE(format (printf, 1, 2)); // utils bool isMsysPty(int fd);