diff --git a/src/Log/ConsoleLogger.cpp b/src/Log/ConsoleLogger.cpp index af43bf4..403b7da 100644 --- a/src/Log/ConsoleLogger.cpp +++ b/src/Log/ConsoleLogger.cpp @@ -4,6 +4,7 @@ #include #endif +#include #include #include @@ -15,12 +16,17 @@ namespace Void::Logging ColorPolicy console_default_policy() { ColorPolicy p = ColorPolicy::defaults(); + // Colored by default, with the standard opt-outs: NO_COLOR for + // the environment and the Stacktrace global toggle when present. + bool enabled = true; + if (const char* no_color = std::getenv("NO_COLOR"); + no_color != nullptr && *no_color != '\0') { + enabled = false; + } #ifdef VOID_HAS_STACKTRACE - // Honor the global color toggle when Stacktrace is present. - p.enabled = Void::Stacktrace::Color::g_color_enabled; -#else - p.enabled = false; + enabled = enabled && Void::Stacktrace::Color::g_color_enabled; #endif + p.enabled = enabled; return p; } } // namespace diff --git a/tests/standalone/Log/ConsoleLogger.cpp b/tests/standalone/Log/ConsoleLogger.cpp index 437f0a6..d999f26 100644 --- a/tests/standalone/Log/ConsoleLogger.cpp +++ b/tests/standalone/Log/ConsoleLogger.cpp @@ -24,6 +24,26 @@ class ConsoleLoggerTests : public Void::Testing::Fixture Void::Testing::expect(content.find("Info") != std::string::npos); } + Void::Testing::Test colors_by_default_with_no_color_opt_out() + { + ::unsetenv("NO_COLOR"); + { + std::ostringstream out; + Void::Logging::ConsoleLogger log{out, Void::Logging::Mode::Immediate}; + log.error("colored"); + Void::Testing::expect(out.str().find("\x1b[") != std::string::npos); + } + ::setenv("NO_COLOR", "1", 1); + { + std::ostringstream out; + Void::Logging::ConsoleLogger log{out, Void::Logging::Mode::Immediate}; + log.error("plain"); + Void::Testing::expect(out.str().find("\x1b[") == std::string::npos); + Void::Testing::expect(out.str().find("plain") != std::string::npos); + } + ::unsetenv("NO_COLOR"); + } + Void::Testing::Test level_filtering() { std::ostringstream out;