Skip to content

Commit

Permalink
Properly use globals for log categories
Browse files Browse the repository at this point in the history
  • Loading branch information
captainurist committed Oct 30, 2023
1 parent 2a91962 commit 86cf777
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
13 changes: 9 additions & 4 deletions src/Media/FFmpegLogProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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();
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/Media/FFmpegLogProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ class FFmpegLogProxy {

private:
Logger *_logger = nullptr;
FFmpegLogSource _source;
LogCategory _category;
std::unordered_map<std::thread::id, LogState> _stateByThreadId;
};
10 changes: 7 additions & 3 deletions src/Platform/Sdl/SdlPlatformSharedState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions src/Platform/Sdl/SdlPlatformSharedState.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class SdlPlatformSharedState {

private:
Logger *_logger = nullptr;
SdlLogSource _logSource;
LogCategory _logCategory;
std::unordered_map<uint32_t, SdlWindow *> _windowById;
std::unordered_map<SDL_JoystickID, std::unique_ptr<SdlGamepad>> _gamepadById;
};

0 comments on commit 86cf777

Please sign in to comment.