From 266b87fe09f81eaff2405900de1733eb47472e7d Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Wed, 4 Nov 2020 14:04:29 +0100 Subject: [PATCH] Logger: force color output if both out&err are term Force color output if both stdout and stderr are connected to terminal. The setting is global and it is possible that only stdout is redirected (eg. to pager) while the check here was for clog (cerr) only. Thus escape sequences had been unintentionally output to the pipe. --- src/debug.cpp | 18 ++++++++++++++++++ src/debug.h | 15 +-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/debug.cpp b/src/debug.cpp index f80604396..5bd7e545f 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -242,6 +242,24 @@ bool set_log_level(const char *optarg, bool *logger_repeat_msgs) { return false; } +void Logger::preinit(bool skip_repeated) +{ + Logger::skip_repeated = skip_repeated; + if (rang::rang_implementation::supportsColor() + && rang::rang_implementation::isTerminal(std::cout.rdbuf()) + && rang::rang_implementation::isTerminal(std::cerr.rdbuf())) { + // force ANSI sequences even when written to ostringstream + rang::setControlMode(rang::control::Force); +#ifdef _WIN32 + // ANSI control sequences need to be explicitly set in Windows + if (rang::rang_implementation::setWinTermAnsiColors(std::cout.rdbuf()) && + rang::rang_implementation::setWinTermAnsiColors(std::cerr.rdbuf())) { + rang::setWinTermMode(rang::winTerm::Ansi); + } +#endif + } +} + std::atomic Logger::last_msg{}; bool Logger::skip_repeated = true; diff --git a/src/debug.h b/src/debug.h index 5e4aaacc6..723fb4ef8 100644 --- a/src/debug.h +++ b/src/debug.h @@ -96,20 +96,7 @@ bool set_log_level(const char *optarg, bool *logger_repeat_msgs); class Logger { public: - static void preinit(bool skip_repeated) { - Logger::skip_repeated = skip_repeated; - if (rang::rang_implementation::supportsColor() - && rang::rang_implementation::isTerminal(std::clog.rdbuf())) { - // force ANSI sequences even when written to ostringstream - rang::setControlMode(rang::control::Force); -#ifdef _WIN32 - // ANSI control sequences need to be explicitly set in Windows - if (rang::rang_implementation::setWinTermAnsiColors(std::clog.rdbuf())) { - rang::setWinTermMode(rang::winTerm::Ansi); - } -#endif - } - } + static void preinit(bool skip_repeated); inline Logger(int l) : level(l) {} inline ~Logger() { rang::fg color = rang::fg::reset;