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 | |
| 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')
| -rw-r--r-- | src/zencore/include/zencore/logbase.h | 2 | ||||
| -rw-r--r-- | src/zencore/include/zencore/logging.h | 1 | ||||
| -rw-r--r-- | src/zencore/logging.cpp | 31 | ||||
| -rw-r--r-- | src/zencore/sentryintegration.cpp | 2 |
4 files changed, 30 insertions, 6 deletions
diff --git a/src/zencore/include/zencore/logbase.h b/src/zencore/include/zencore/logbase.h index 046e96db3..ad2ab218d 100644 --- a/src/zencore/include/zencore/logbase.h +++ b/src/zencore/include/zencore/logbase.h @@ -101,7 +101,7 @@ struct LoggerRef inline logging::Logger* operator->() const; inline logging::Logger& operator*() const; - bool ShouldLog(logging::LogLevel Level) const { return m_Logger && m_Logger->ShouldLog(Level); } + bool ShouldLog(logging::LogLevel Level) const { return m_Logger->ShouldLog(Level); } void SetLogLevel(logging::LogLevel NewLogLevel) { m_Logger->SetLevel(NewLogLevel); } logging::LogLevel GetLogLevel() { return m_Logger->GetLevel(); } diff --git a/src/zencore/include/zencore/logging.h b/src/zencore/include/zencore/logging.h index dc5e05656..3427991d2 100644 --- a/src/zencore/include/zencore/logging.h +++ b/src/zencore/include/zencore/logging.h @@ -18,6 +18,7 @@ namespace zen::logging { void InitializeLogging(); void ShutdownLogging(); +bool IsLoggingInitialized(); bool EnableVTMode(); void FlushLogging(); 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 diff --git a/src/zencore/sentryintegration.cpp b/src/zencore/sentryintegration.cpp index 634045cfb..c9c843dd6 100644 --- a/src/zencore/sentryintegration.cpp +++ b/src/zencore/sentryintegration.cpp @@ -160,7 +160,7 @@ SentryLogFunction(sentry_level_t Level, const char* Message, va_list Args, [[may // been configured, we ignore the callbacks for DEBUG/INFO explicitly here // which means users don't see every possible log message if they're trying // to configure the levels using --log-debug=sentry-sdk - if (!TheDefaultLogger || !s_SentryLogEnabled.load(std::memory_order_acquire)) + if (!zen::logging::IsLoggingInitialized() || !s_SentryLogEnabled.load(std::memory_order_acquire)) { switch (Level) { |