From 5c3b7f75e1a5e40a8e9761863d7022b3003c252f Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Wed, 4 Nov 2026 18:26:31 -0600 Subject: Common/Logging/LogManager: Clean up "path cutoff point" calculation. --- Source/Core/Common/Logging/LogManager.cpp | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'Source/Core/Common/Logging/LogManager.cpp') diff --git a/Source/Core/Common/Logging/LogManager.cpp b/Source/Core/Common/Logging/LogManager.cpp index 89ae089a44..5a586623cb 100644 --- a/Source/Core/Common/Logging/LogManager.cpp +++ b/Source/Core/Common/Logging/LogManager.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -19,7 +18,6 @@ #include "Common/FileUtil.h" #include "Common/Logging/ConsoleListener.h" #include "Common/Logging/Log.h" -#include "Common/StringUtil.h" namespace Common::Log { @@ -74,22 +72,17 @@ void GenericLogFmtImpl(LogLevel level, LogType type, const char* file, int line, instance->Log(level, type, file, line, message.c_str()); } -static size_t DeterminePathCutOffPoint() +static consteval size_t GetPathCutOffPoint() { - constexpr const char* pattern = "/source/core/"; -#ifdef _WIN32 - constexpr const char* pattern2 = "\\source\\core\\"; + constexpr std::string_view pattern = +#if defined(_WIN32) + "Common\\Logging\\LogManager.cpp"; +#else + "Common/Logging/LogManager.cpp"; #endif - std::string path = __FILE__; - Common::ToLower(&path); - size_t pos = path.find(pattern); -#ifdef _WIN32 - if (pos == std::string::npos) - pos = path.find(pattern2); -#endif - if (pos != std::string::npos) - return pos + strlen(pattern); - return 0; + constexpr std::string_view filename = __FILE__; + static_assert(filename.ends_with(pattern)); + return filename.length() - pattern.length(); } LogManager::LogManager() @@ -170,8 +163,6 @@ LogManager::LogManager() Config::Info{{Config::System::Logger, "Logs", container.m_short_name}, false}); } - m_path_cutoff_point = DeterminePathCutOffPoint(); - m_config_changed_callback_id = Config::AddConfigChangedCallback([this]() { SetEffectiveLogLevel(); }); } @@ -205,7 +196,7 @@ void LogManager::Log(LogLevel level, LogType type, const char* file, int line, c if (!IsEnabled(type, level) || !static_cast(m_listener_ids)) return; - LogWithFullPath(level, type, file + m_path_cutoff_point, line, message); + LogWithFullPath(level, type, file + GetPathCutOffPoint(), line, message); } std::string LogManager::GetTimestamp() -- cgit v1.2.3 From d02fc5f4bb1733a0cfd45ee00b4a8e3ff2a53ca3 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Wed, 4 Nov 2026 18:58:44 -0600 Subject: Common/Logging/LogManager: Add IsListenerEnabled function to avoid string formatting when log types are enabled but all listeners are disabled. Also eliminate redundant IsEnabled(type, level) check. --- Source/Core/Common/Logging/LogManager.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'Source/Core/Common/Logging/LogManager.cpp') diff --git a/Source/Core/Common/Logging/LogManager.cpp b/Source/Core/Common/Logging/LogManager.cpp index 5a586623cb..ae3ea6a66c 100644 --- a/Source/Core/Common/Logging/LogManager.cpp +++ b/Source/Core/Common/Logging/LogManager.cpp @@ -65,7 +65,7 @@ void GenericLogFmtImpl(LogLevel level, LogType type, const char* file, int line, if (instance == nullptr) return; - if (!instance->IsEnabled(type, level)) + if (!instance->IsEnabled(type, level) || !instance->IsAnyListenerEnabled()) return; const auto message = fmt::vformat(format, args); @@ -193,9 +193,6 @@ void LogManager::SaveSettings() void LogManager::Log(LogLevel level, LogType type, const char* file, int line, const char* message) { - if (!IsEnabled(type, level) || !static_cast(m_listener_ids)) - return; - LogWithFullPath(level, type, file + GetPathCutOffPoint(), line, message); } -- cgit v1.2.3 From 055767399abab0eff6f55acbf480e54fcd83f6bd Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Wed, 4 Nov 2026 18:59:40 -0600 Subject: Common/Logging/LogManager: Pass string_view instead of char* to give fmt string length awareness without having to perform strlen calls. --- Source/Core/Common/Logging/LogManager.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'Source/Core/Common/Logging/LogManager.cpp') diff --git a/Source/Core/Common/Logging/LogManager.cpp b/Source/Core/Common/Logging/LogManager.cpp index ae3ea6a66c..0bc51bfa5b 100644 --- a/Source/Core/Common/Logging/LogManager.cpp +++ b/Source/Core/Common/Logging/LogManager.cpp @@ -69,7 +69,7 @@ void GenericLogFmtImpl(LogLevel level, LogType type, const char* file, int line, return; const auto message = fmt::vformat(format, args); - instance->Log(level, type, file, line, message.c_str()); + instance->Log(level, type, file, line, message); } static consteval size_t GetPathCutOffPoint() @@ -191,7 +191,8 @@ void LogManager::SaveSettings() Config::Save(); } -void LogManager::Log(LogLevel level, LogType type, const char* file, int line, const char* message) +void LogManager::Log(LogLevel level, LogType type, const char* file, int line, + std::string_view message) { LogWithFullPath(level, type, file + GetPathCutOffPoint(), line, message); } @@ -209,7 +210,7 @@ std::string LogManager::GetTimestamp() } void LogManager::LogWithFullPath(LogLevel level, LogType type, const char* file, int line, - const char* message) + std::string_view message) { const std::string msg = fmt::format("{} {}:{} {}[{}]: {}\n", GetTimestamp(), file, line, -- cgit v1.2.3 From 43c95acd01968c33c8cc2a02c27e5d6d872022ef Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Wed, 4 Nov 2026 19:00:59 -0600 Subject: Common/Logging/LogManager: Combine GetTimestamp functionality into LogWithFullPath to eliminate one std::string construction. --- Source/Core/Common/Logging/LogManager.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'Source/Core/Common/Logging/LogManager.cpp') diff --git a/Source/Core/Common/Logging/LogManager.cpp b/Source/Core/Common/Logging/LogManager.cpp index 0bc51bfa5b..f35826793f 100644 --- a/Source/Core/Common/Logging/LogManager.cpp +++ b/Source/Core/Common/Logging/LogManager.cpp @@ -197,7 +197,8 @@ void LogManager::Log(LogLevel level, LogType type, const char* file, int line, LogWithFullPath(level, type, file + GetPathCutOffPoint(), line, message); } -std::string LogManager::GetTimestamp() +void LogManager::LogWithFullPath(LogLevel level, LogType type, const char* file, int line, + std::string_view message) { // NOTE: the Qt LogWidget hardcodes the expected length of the timestamp portion of the log line, // so ensure they stay in sync @@ -206,14 +207,9 @@ std::string LogManager::GetTimestamp() const auto now = std::chrono::system_clock::now(); const auto now_s = std::chrono::floor(now); const auto now_ms = std::chrono::floor(now); - return fmt::format("{:%M:%S}:{:03}", now_s, (now_ms - now_s).count()); -} -void LogManager::LogWithFullPath(LogLevel level, LogType type, const char* file, int line, - std::string_view message) -{ const std::string msg = - fmt::format("{} {}:{} {}[{}]: {}\n", GetTimestamp(), file, line, + fmt::format("{:%M:%S}:{:03} {}:{} {}[{}]: {}\n", now_s, (now_ms - now_s).count(), file, line, LOG_LEVEL_TO_CHAR[static_cast(level)], GetShortName(type), message); for (const auto listener_id : m_listener_ids) -- cgit v1.2.3