diff options
| author | Stefan Boberg <[email protected]> | 2026-03-18 19:46:44 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-03-18 19:46:44 +0100 |
| commit | 2fd2752d411ea6a0c8c3b5f511cc792217fa3b75 (patch) | |
| tree | 295a90b3488fd861b760a75a59c726672a337ed4 /src/zencore/logging.cpp | |
| parent | allow xmake sln with open at end (#865) (diff) | |
| download | zen-2fd2752d411ea6a0c8c3b5f511cc792217fa3b75.tar.xz zen-2fd2752d411ea6a0c8c3b5f511cc792217fa3b75.zip | |
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
Diffstat (limited to 'src/zencore/logging.cpp')
| -rw-r--r-- | src/zencore/logging.cpp | 31 |
1 files changed, 27 insertions, 4 deletions
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<zen::logging::Logger> s_BootstrapLogger = [] { + zen::logging::SinkPtr Sink(new zen::logging::AnsiColorStdoutSink()); + return zen::Ref<zen::logging::Logger>(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 |