From 56342126bfd1cdfc33c9c377cceebf6c21bdfa0a Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 16 Jun 2024 17:46:25 +0100 Subject: [PATCH] Log: Avoid making strings that won't be printed When we switched to `fmt` for logging, the log functions were implemented in such a way that the strings were always constructed, even if log priority was such that they weren't ever logged. --- Source/controls/plrctrls.cpp | 4 ++-- Source/engine/assets.cpp | 2 +- Source/utils/log.hpp | 2 ++ Source/utils/sdl2_to_1_2_backports.cpp | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Source/controls/plrctrls.cpp b/Source/controls/plrctrls.cpp index 335f8c3bd..b61ef9ccb 100644 --- a/Source/controls/plrctrls.cpp +++ b/Source/controls/plrctrls.cpp @@ -1484,7 +1484,7 @@ std::string_view ControlTypeToString(ControlTypes controlType) void LogControlDeviceAndModeChange(ControlTypes newControlDevice, ControlTypes newControlMode) { - if (SDL_LOG_PRIORITY_VERBOSE < SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION)) + if (SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION) > SDL_LOG_PRIORITY_VERBOSE) return; if (newControlDevice == ControlDevice && newControlMode == ControlMode) return; @@ -1514,7 +1514,7 @@ std::string_view GamepadTypeToString(GamepadLayout gamepadLayout) void LogGamepadChange(GamepadLayout newGamepad) { - if (SDL_LOG_PRIORITY_VERBOSE < SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION)) + if (SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION) > SDL_LOG_PRIORITY_VERBOSE) return; constexpr auto DebugChange = [](GamepadLayout before, GamepadLayout after) -> std::string { if (before == after) diff --git a/Source/engine/assets.cpp b/Source/engine/assets.cpp index d69026b6f..691ebc356 100644 --- a/Source/engine/assets.cpp +++ b/Source/engine/assets.cpp @@ -41,7 +41,7 @@ char *FindUnpackedMpqFile(char *relativePath) #else bool IsDebugLogging() { - return SDL_LOG_PRIORITY_DEBUG >= SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION); + return SDL_LogGetPriority(SDL_LOG_CATEGORY_APPLICATION) <= SDL_LOG_PRIORITY_DEBUG; } SDL_RWops *OpenOptionalRWops(const std::string &path) diff --git a/Source/utils/log.hpp b/Source/utils/log.hpp index 976424348..e56414784 100644 --- a/Source/utils/log.hpp +++ b/Source/utils/log.hpp @@ -73,6 +73,7 @@ void Log(std::string_view fmt, Args &&...args) template void LogVerbose(LogCategory category, std::string_view fmt, Args &&...args) { + if (SDL_LogGetPriority(static_cast(category)) > SDL_LOG_PRIORITY_VERBOSE) return; auto str = detail::format(fmt, std::forward(args)...); SDL_LogVerbose(static_cast(category), "%s", str.c_str()); } @@ -86,6 +87,7 @@ void LogVerbose(std::string_view fmt, Args &&...args) template void LogDebug(LogCategory category, std::string_view fmt, Args &&...args) { + if (SDL_LogGetPriority(static_cast(category)) > SDL_LOG_PRIORITY_DEBUG) return; auto str = detail::format(fmt, std::forward(args)...); SDL_LogDebug(static_cast(category), "%s", str.c_str()); } diff --git a/Source/utils/sdl2_to_1_2_backports.cpp b/Source/utils/sdl2_to_1_2_backports.cpp index af280f133..fcff69fd1 100644 --- a/Source/utils/sdl2_to_1_2_backports.cpp +++ b/Source/utils/sdl2_to_1_2_backports.cpp @@ -168,7 +168,7 @@ void SDL_LogMessage(int category, SDL_LogPriority priority, const char *fmt, ... void SDL_LogMessageV(int category, SDL_LogPriority priority, const char *fmt, va_list ap) { - if (static_cast(priority) < 0 || priority >= SDL_NUM_LOG_PRIORITIES || priority < SDL_LogGetPriority(category)) + if (static_cast(priority) < 0 || priority >= SDL_NUM_LOG_PRIORITIES || SDL_LogGetPriority(category) > priority) return; ::devilution::printfInConsole("%s: ", SDL_priority_prefixes[priority]);