From 2fd2752d411ea6a0c8c3b5f511cc792217fa3b75 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Wed, 18 Mar 2026 19:46:44 +0100 Subject: Pre-initialization of default logger (#859) Improved workaround for troubles with code potentially logging before logging is initialized. Any logging will be routed to a default console logger until logging is initialized fully --- src/zencore/logging.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'src/zencore/logging.cpp') diff --git a/src/zencore/logging.cpp b/src/zencore/logging.cpp index 900da6c6d..3206e380b 100644 --- a/src/zencore/logging.cpp +++ b/src/zencore/logging.cpp @@ -21,14 +21,35 @@ # pragma section(".zlog$z", read) #endif +namespace { + +// Bootstrap logger: a minimal stdout logger that exists for the entire lifetime +// of the process. TheDefaultLogger points here before InitializeLogging() runs +// (and is restored here after ShutdownLogging()) so that log macros always have +// a usable target — no null checks or lazy init required on the common path. +zen::Ref s_BootstrapLogger = [] { + zen::logging::SinkPtr Sink(new zen::logging::AnsiColorStdoutSink()); + return zen::Ref(new zen::logging::Logger("", Sink)); +}(); + +} // namespace + namespace zen { -LoggerRef TheDefaultLogger; +LoggerRef TheDefaultLogger{*s_BootstrapLogger}; } // namespace zen namespace zen::logging { +static bool g_LoggingInitialized = false; + +bool +IsLoggingInitialized() +{ + return g_LoggingInitialized; +} + ////////////////////////////////////////////////////////////////////////// LoggerRef @@ -232,7 +253,7 @@ GetLogLevel() LoggerRef Default() { - ZEN_ASSERT(TheDefaultLogger, "logging::InitializeLogging() must be called before using the logger"); + ZEN_ASSERT(g_LoggingInitialized, "logging::InitializeLogging() must be called before using the logger"); return TheDefaultLogger; } @@ -393,7 +414,8 @@ InitializeLogging() { ZEN_MEMSCOPE(ELLMTag::Logging); - TheDefaultLogger = LoggerRef(*Registry::Instance().DefaultLoggerRaw()); + TheDefaultLogger = LoggerRef(*Registry::Instance().DefaultLoggerRaw()); + g_LoggingInitialized = true; } void @@ -401,8 +423,9 @@ ShutdownLogging() { ZEN_MEMSCOPE(ELLMTag::Logging); + g_LoggingInitialized = false; Registry::Instance().Shutdown(); - TheDefaultLogger = {}; + TheDefaultLogger = LoggerRef(*s_BootstrapLogger); } bool -- cgit v1.2.3