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.
This commit is contained in:
Martin Pulec
2020-11-04 14:04:29 +01:00
parent 024494f4b8
commit 266b87fe09
2 changed files with 19 additions and 14 deletions

View File

@@ -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_message *> Logger::last_msg{};
bool Logger::skip_repeated = true;