diff --git a/common/Console.cpp b/common/Console.cpp index 17842dfc53..e6f0a29fa1 100644 --- a/common/Console.cpp +++ b/common/Console.cpp @@ -98,14 +98,25 @@ __ri void Log::WriteToConsole(LOGLEVEL level, ConsoleColors color, std::string_v static constexpr size_t BUFFER_SIZE = 512; +#ifdef _WIN32 + constexpr bool supports_color_codes = true; +#else + const int fd = (level <= LOGLEVEL_WARNING) ? STDERR_FILENO : STDOUT_FILENO; + const bool supports_color_codes = static_cast(isatty(fd)); +#endif + SmallStackString buffer; buffer.reserve(static_cast(32 + message.length())); - buffer.append(s_ansi_color_codes[color]); + if (supports_color_codes) + buffer.append(s_ansi_color_codes[color]); if (s_log_timestamps) buffer.append_format(TIMESTAMP_FORMAT_STRING, Log::GetCurrentMessageTime()); buffer.append(message); + + if (supports_color_codes) + buffer.append(s_ansi_color_codes[Color_Default]); buffer.append('\n'); #ifdef _WIN32 @@ -134,7 +145,6 @@ cleanup: if (wmessage_buf != wbuf) std::free(wmessage_buf); #else - const int fd = (level <= LOGLEVEL_WARNING) ? STDERR_FILENO : STDOUT_FILENO; write(fd, buffer.data(), buffer.length()); #endif } @@ -157,7 +167,7 @@ void Log::SetConsoleOutputLevel(LOGLEVEL level) if (was_enabled == now_enabled) return; - // Worst that happens here is we write to a bad handle.. + // Worst that happens here is we write to a bad handle.. #if defined(_WIN32) static constexpr auto enable_virtual_terminal_processing = [](HANDLE hConsole) {