Skip to content

Commit

Permalink
Log: Avoid making strings that won't be printed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glebm committed Jun 16, 2024
1 parent e078b46 commit 5634212
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Source/controls/plrctrls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Source/engine/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Source/utils/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void Log(std::string_view fmt, Args &&...args)
template <typename... Args>
void LogVerbose(LogCategory category, std::string_view fmt, Args &&...args)
{
if (SDL_LogGetPriority(static_cast<int>(category)) > SDL_LOG_PRIORITY_VERBOSE) return;
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogVerbose(static_cast<int>(category), "%s", str.c_str());
}
Expand All @@ -86,6 +87,7 @@ void LogVerbose(std::string_view fmt, Args &&...args)
template <typename... Args>
void LogDebug(LogCategory category, std::string_view fmt, Args &&...args)
{
if (SDL_LogGetPriority(static_cast<int>(category)) > SDL_LOG_PRIORITY_DEBUG) return;
auto str = detail::format(fmt, std::forward<Args>(args)...);
SDL_LogDebug(static_cast<int>(category), "%s", str.c_str());
}
Expand Down
2 changes: 1 addition & 1 deletion Source/utils/sdl2_to_1_2_backports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(priority) < 0 || priority >= SDL_NUM_LOG_PRIORITIES || priority < SDL_LogGetPriority(category))
if (static_cast<int>(priority) < 0 || priority >= SDL_NUM_LOG_PRIORITIES || SDL_LogGetPriority(category) > priority)
return;

::devilution::printfInConsole("%s: ", SDL_priority_prefixes[priority]);
Expand Down

0 comments on commit 5634212

Please sign in to comment.