From 86cf77700a17d825e89f09b681b2d070318c4f69 Mon Sep 17 00:00:00 2001 From: captainurist <73941350+captainurist@users.noreply.github.com> Date: Mon, 30 Oct 2023 10:35:49 +0000 Subject: [PATCH] Properly use globals for log categories --- src/Media/FFmpegLogProxy.cpp | 13 +++++++++---- src/Media/FFmpegLogProxy.h | 2 -- src/Platform/Sdl/SdlPlatformSharedState.cpp | 10 +++++++--- src/Platform/Sdl/SdlPlatformSharedState.h | 2 -- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Media/FFmpegLogProxy.cpp b/src/Media/FFmpegLogProxy.cpp index 075a2cb9f102..32603f38bd20 100644 --- a/src/Media/FFmpegLogProxy.cpp +++ b/src/Media/FFmpegLogProxy.cpp @@ -9,7 +9,12 @@ extern "C" { #include "FFmpegLogSource.h" -static std::mutex globalFFmpegLogMutex; // Access to global logger ptr must be serialized, so we need a global mutex. +// Category should be a global so that it's registered at program startup. +static constinit FFmpegLogSource globalFFmpegLogSource; +static LogCategory globalFFmpegLogCategory("ffmpeg", &globalFFmpegLogSource); + +// Access to global logger ptr must be serialized, so we need a global mutex. +static std::mutex globalFFmpegLogMutex; static FFmpegLogProxy *globalFFmpegLogger = nullptr; static void ffmpegLogCallback(void *ptr, int level, const char *format, va_list args) { @@ -19,7 +24,7 @@ static void ffmpegLogCallback(void *ptr, int level, const char *format, va_list globalFFmpegLogger->log(ptr, level, format, args); } -FFmpegLogProxy::FFmpegLogProxy(Logger *logger): _logger(logger), _category("ffmpeg", &_source) { +FFmpegLogProxy::FFmpegLogProxy(Logger *logger): _logger(logger) { assert(logger); assert(globalFFmpegLogger == nullptr); globalFFmpegLogger = this; @@ -41,11 +46,11 @@ void FFmpegLogProxy::log(void *ptr, int level, const char *format, va_list args) char buffer[4096]; int status = av_log_format_line2(ptr, level, format, args, buffer, sizeof(buffer), &state.prefixFlag); if (status < 0) { - _logger->trace(_category, "av_log_format_line2 failed with error code {}", status); + _logger->trace(globalFFmpegLogCategory, "av_log_format_line2 failed with error code {}", status); } else { state.message += buffer; if (state.message.ends_with('\n')) { - _logger->log(_category, FFmpegLogSource::translateFFmpegLogLevel(level), "{}", state.message); + _logger->log(globalFFmpegLogCategory, FFmpegLogSource::translateFFmpegLogLevel(level), "{}", state.message); state.message.clear(); } } diff --git a/src/Media/FFmpegLogProxy.h b/src/Media/FFmpegLogProxy.h index 0f1a47cb0c81..c759c44ba952 100644 --- a/src/Media/FFmpegLogProxy.h +++ b/src/Media/FFmpegLogProxy.h @@ -25,7 +25,5 @@ class FFmpegLogProxy { private: Logger *_logger = nullptr; - FFmpegLogSource _source; - LogCategory _category; std::unordered_map _stateByThreadId; }; diff --git a/src/Platform/Sdl/SdlPlatformSharedState.cpp b/src/Platform/Sdl/SdlPlatformSharedState.cpp index 7c4339e517ff..50d927c67afe 100644 --- a/src/Platform/Sdl/SdlPlatformSharedState.cpp +++ b/src/Platform/Sdl/SdlPlatformSharedState.cpp @@ -11,7 +11,11 @@ #include "SdlLogSource.h" #include "SdlGamepad.h" -SdlPlatformSharedState::SdlPlatformSharedState(Logger *logger): _logger(logger), _logCategory("sdl", &_logSource) { +// Log category should be a global so that it's registered at program startup. +static constinit SdlLogSource globalSdlLogSource; +static LogCategory globalSdlLogCategory("sdl", &globalSdlLogSource); + +SdlPlatformSharedState::SdlPlatformSharedState(Logger *logger): _logger(logger) { assert(logger); } @@ -20,11 +24,11 @@ SdlPlatformSharedState::~SdlPlatformSharedState() { } void SdlPlatformSharedState::logSdlError(const char *sdlFunctionName) { - _logger->error(_logCategory, "SDL error in {}: {}", sdlFunctionName, SDL_GetError()); + _logger->error(globalSdlLogCategory, "SDL error in {}: {}", sdlFunctionName, SDL_GetError()); } const LogCategory &SdlPlatformSharedState::logCategory() const { - return _logCategory; + return globalSdlLogCategory; } Logger *SdlPlatformSharedState::logger() const { diff --git a/src/Platform/Sdl/SdlPlatformSharedState.h b/src/Platform/Sdl/SdlPlatformSharedState.h index 16c3b43b56f3..40cac68155c3 100644 --- a/src/Platform/Sdl/SdlPlatformSharedState.h +++ b/src/Platform/Sdl/SdlPlatformSharedState.h @@ -41,8 +41,6 @@ class SdlPlatformSharedState { private: Logger *_logger = nullptr; - SdlLogSource _logSource; - LogCategory _logCategory; std::unordered_map _windowById; std::unordered_map> _gamepadById; };