diff options
Diffstat (limited to 'src/zenutil/logging')
| -rw-r--r-- | src/zenutil/logging/fullformatter.cpp | 30 | ||||
| -rw-r--r-- | src/zenutil/logging/jsonformatter.cpp | 2 | ||||
| -rw-r--r-- | src/zenutil/logging/logging.cpp | 43 | ||||
| -rw-r--r-- | src/zenutil/logging/rotatingfilesink.cpp | 2 |
4 files changed, 48 insertions, 29 deletions
diff --git a/src/zenutil/logging/fullformatter.cpp b/src/zenutil/logging/fullformatter.cpp index 2a4840241..283a8bc37 100644 --- a/src/zenutil/logging/fullformatter.cpp +++ b/src/zenutil/logging/fullformatter.cpp @@ -12,6 +12,7 @@ #include <atomic> #include <chrono> #include <string> +#include "zencore/logging.h" namespace zen::logging { @@ -25,7 +26,7 @@ struct FullFormatter::Impl { } - explicit Impl(std::string_view LogId) : m_LogId(LogId), m_LinePrefix(128, ' '), m_UseFullDate(true) {} + explicit Impl(std::string_view LogId) : m_LogId(LogId), m_LinePrefix(128, ' ') {} std::chrono::time_point<std::chrono::system_clock> m_Epoch; std::tm m_CachedLocalTm{}; @@ -155,15 +156,7 @@ FullFormatter::Format(const LogMessage& Msg, MemoryBuffer& OutBuffer) OutBuffer.push_back(' '); } - // append logger name if exists - if (Msg.GetLoggerName().size() > 0) - { - OutBuffer.push_back('['); - helpers::AppendStringView(Msg.GetLoggerName(), OutBuffer); - OutBuffer.push_back(']'); - OutBuffer.push_back(' '); - } - + // level OutBuffer.push_back('['); if (IsColorEnabled()) { @@ -177,6 +170,23 @@ FullFormatter::Format(const LogMessage& Msg, MemoryBuffer& OutBuffer) OutBuffer.push_back(']'); OutBuffer.push_back(' '); + // logger name + if (Msg.GetLoggerName().size() > 0) + { + if (IsColorEnabled()) + { + OutBuffer.append("\033[1m"sv); + } + OutBuffer.push_back('['); + helpers::AppendStringView(Msg.GetLoggerName(), OutBuffer); + OutBuffer.push_back(']'); + if (IsColorEnabled()) + { + OutBuffer.append("\033[0m"sv); + } + OutBuffer.push_back(' '); + } + // add source location if present if (Msg.GetSource()) { diff --git a/src/zenutil/logging/jsonformatter.cpp b/src/zenutil/logging/jsonformatter.cpp index 673a03c94..c63ad891e 100644 --- a/src/zenutil/logging/jsonformatter.cpp +++ b/src/zenutil/logging/jsonformatter.cpp @@ -19,8 +19,6 @@ static void WriteEscapedString(MemoryBuffer& Dest, std::string_view Text) { // Strip ANSI SGR sequences before escaping so they don't appear in JSON output - static const auto IsEscapeStart = [](char C) { return C == '\033'; }; - const char* RangeStart = Text.data(); const char* End = Text.data() + Text.size(); diff --git a/src/zenutil/logging/logging.cpp b/src/zenutil/logging/logging.cpp index ea2448a42..c1636da61 100644 --- a/src/zenutil/logging/logging.cpp +++ b/src/zenutil/logging/logging.cpp @@ -8,6 +8,7 @@ #include <zencore/logging.h> #include <zencore/logging/ansicolorsink.h> #include <zencore/logging/asyncsink.h> +#include <zencore/logging/broadcastsink.h> #include <zencore/logging/logger.h> #include <zencore/logging/msvcsink.h> #include <zencore/logging/registry.h> @@ -23,8 +24,9 @@ namespace zen { -static bool g_IsLoggingInitialized; -logging::SinkPtr g_FileSink; +static bool g_IsLoggingInitialized; +logging::SinkPtr g_FileSink; +Ref<logging::BroadcastSink> g_BroadcastSink; logging::SinkPtr GetFileSink() @@ -32,6 +34,12 @@ GetFileSink() return g_FileSink; } +Ref<logging::BroadcastSink> +GetDefaultBroadcastSink() +{ + return g_BroadcastSink; +} + void InitializeLogging(const LoggingOptions& LogOptions) { @@ -47,7 +55,6 @@ BeginInitializeLogging(const LoggingOptions& LogOptions) ZEN_MEMSCOPE(ELLMTag::Logging); zen::logging::InitializeLogging(); - zen::logging::EnableVTMode(); // Sinks @@ -117,8 +124,10 @@ BeginInitializeLogging(const LoggingOptions& LogOptions) LoggerRef DefaultLogger = zen::logging::Default(); - // Collect sinks into a local vector first so we can optionally wrap them - std::vector<logging::SinkPtr> Sinks; + // Build the broadcast sink - a shared indirection point that all + // loggers cloned from the default will share. Adding or removing + // a child sink later is immediately visible to every logger. + std::vector<logging::SinkPtr> BroadcastChildren; if (LogOptions.NoConsoleOutput) { @@ -126,17 +135,18 @@ BeginInitializeLogging(const LoggingOptions& LogOptions) } else { - logging::SinkPtr ConsoleSink(new logging::AnsiColorStdoutSink()); + logging::SinkPtr ConsoleSink( + new logging::AnsiColorStdoutSink(LogOptions.ForceColor ? logging::ColorMode::On : logging::ColorMode::Auto)); if (LogOptions.QuietConsole) { ConsoleSink->SetLevel(logging::Warn); } - Sinks.push_back(ConsoleSink); + BroadcastChildren.push_back(ConsoleSink); } if (FileSink) { - Sinks.push_back(FileSink); + BroadcastChildren.push_back(FileSink); } #if ZEN_PLATFORM_WINDOWS @@ -144,21 +154,21 @@ BeginInitializeLogging(const LoggingOptions& LogOptions) { logging::SinkPtr DebugSink(new logging::MsvcSink()); DebugSink->SetLevel(logging::Debug); - Sinks.push_back(DebugSink); + BroadcastChildren.push_back(DebugSink); } #endif + g_BroadcastSink = Ref<logging::BroadcastSink>(new logging::BroadcastSink(std::move(BroadcastChildren))); + bool IsAsync = LogOptions.AllowAsync && !LogOptions.IsDebug && !LogOptions.IsTest; if (IsAsync) { - std::vector<logging::SinkPtr> AsyncSinks; - AsyncSinks.emplace_back(new logging::AsyncSink(std::move(Sinks))); - DefaultLogger->SetSinks(std::move(AsyncSinks)); + DefaultLogger->SetSink(logging::SinkPtr(new logging::AsyncSink({logging::SinkPtr(g_BroadcastSink.Get())}))); } else { - DefaultLogger->SetSinks(std::move(Sinks)); + DefaultLogger->SetSink(logging::SinkPtr(g_BroadcastSink.Get())); } static struct : logging::ErrorHandler @@ -169,7 +179,7 @@ BeginInitializeLogging(const LoggingOptions& LogOptions) { return; } - static constinit logging::LogPoint ErrorPoint{{}, logging::Err, "{}"}; + static constinit logging::LogPoint ErrorPoint{0, 0, logging::Err, "{}"}; if (auto ErrLogger = zen::logging::ErrorLog()) { try @@ -239,7 +249,7 @@ FinishInitializeLogging(const LoggingOptions& LogOptions) const std::string StartLogTime = zen::DateTime::Now().ToIso8601(); logging::Registry::Instance().ApplyAll([&](auto Logger) { - static constinit logging::LogPoint LogStartPoint{{}, logging::Info, "log starting at {}"}; + static constinit logging::LogPoint LogStartPoint{0, 0, logging::Info, "log starting at {}"}; Logger->Log(LogStartPoint, fmt::make_format_args(StartLogTime)); }); } @@ -258,7 +268,8 @@ ShutdownLogging() zen::logging::ShutdownLogging(); - g_FileSink = nullptr; + g_FileSink = nullptr; + g_BroadcastSink = nullptr; } } // namespace zen diff --git a/src/zenutil/logging/rotatingfilesink.cpp b/src/zenutil/logging/rotatingfilesink.cpp index 23cf60d16..df59af5fe 100644 --- a/src/zenutil/logging/rotatingfilesink.cpp +++ b/src/zenutil/logging/rotatingfilesink.cpp @@ -85,7 +85,7 @@ struct RotatingFileSink::Impl m_CurrentSize = m_CurrentFile.FileSize(OutEc); if (OutEc) { - // FileSize failed but we have an open file — reset to 0 + // FileSize failed but we have an open file - reset to 0 // so we can at least attempt writes from the start m_CurrentSize = 0; OutEc.clear(); |