diff options
| author | Léo Lam <leo@leolam.fr> | 2020-10-21 19:11:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-21 19:11:19 +0200 |
| commit | 09e87b79f19ce0fb06b236e15ebed414af57558a (patch) | |
| tree | 199f03308f3d349aa0f20bda3d880e75a76b8150 /Source/Core/Common/Logging | |
| parent | f265c412b449f7054468fae6e13a890bb0d3c098 (diff) | |
| parent | 425f2aa013a9051a15b58a722668358a2dae5c70 (diff) | |
Merge pull request #9173 from lioncash/fmtlog
Common/Log: Add basic fmt-capable functions to the interface.
Diffstat (limited to 'Source/Core/Common/Logging')
| -rw-r--r-- | Source/Core/Common/Logging/Log.h | 51 | ||||
| -rw-r--r-- | Source/Core/Common/Logging/LogManager.cpp | 48 | ||||
| -rw-r--r-- | Source/Core/Common/Logging/LogManager.h | 8 |
3 files changed, 88 insertions, 19 deletions
diff --git a/Source/Core/Common/Logging/Log.h b/Source/Core/Common/Logging/Log.h index a0d9e631b0..95f76b138a 100644 --- a/Source/Core/Common/Logging/Log.h +++ b/Source/Core/Common/Logging/Log.h @@ -4,6 +4,9 @@ #pragma once +#include <fmt/format.h> +#include <string_view> + namespace Common::Log { enum LOG_TYPE @@ -72,8 +75,17 @@ enum LOG_LEVELS static const char LOG_LEVEL_TO_CHAR[7] = "-NEWID"; -void GenericLog(Common::Log::LOG_LEVELS level, Common::Log::LOG_TYPE type, const char* file, - int line, const char* fmt, ...) +void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, + std::string_view format, const fmt::format_args& args); + +template <typename... Args> +void GenericLogFmt(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, + std::string_view format, const Args&... args) +{ + GenericLogFmtImpl(level, type, file, line, format, fmt::make_format_args(args...)); +} + +void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, ...) #ifdef __GNUC__ __attribute__((format(printf, 5, 6))) #endif @@ -121,3 +133,38 @@ void GenericLog(Common::Log::LOG_LEVELS level, Common::Log::LOG_TYPE type, const { \ GENERIC_LOG(Common::Log::t, Common::Log::LDEBUG, __VA_ARGS__); \ } while (0) + +// fmtlib capable API + +#define GENERIC_LOG_FMT(t, v, ...) \ + do \ + { \ + if (v <= MAX_LOGLEVEL) \ + Common::Log::GenericLogFmt(v, t, __FILE__, __LINE__, __VA_ARGS__); \ + } while (0) + +#define ERROR_LOG_FMT(t, ...) \ + do \ + { \ + GENERIC_LOG_FMT(Common::Log::t, Common::Log::LERROR, __VA_ARGS__); \ + } while (0) +#define WARN_LOG_FMT(t, ...) \ + do \ + { \ + GENERIC_LOG_FMT(Common::Log::t, Common::Log::LWARNING, __VA_ARGS__); \ + } while (0) +#define NOTICE_LOG_FMT(t, ...) \ + do \ + { \ + GENERIC_LOG_FMT(Common::Log::t, Common::Log::LNOTICE, __VA_ARGS__); \ + } while (0) +#define INFO_LOG_FMT(t, ...) \ + do \ + { \ + GENERIC_LOG_FMT(Common::Log::t, Common::Log::LINFO, __VA_ARGS__); \ + } while (0) +#define DEBUG_LOG_FMT(t, ...) \ + do \ + { \ + GENERIC_LOG_FMT(Common::Log::t, Common::Log::LDEBUG, __VA_ARGS__); \ + } while (0) diff --git a/Source/Core/Common/Logging/LogManager.cpp b/Source/Core/Common/Logging/LogManager.cpp index 99ddf83d67..2255fc6f8e 100644 --- a/Source/Core/Common/Logging/LogManager.cpp +++ b/Source/Core/Common/Logging/LogManager.cpp @@ -64,11 +64,34 @@ private: void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, ...) { + auto* instance = LogManager::GetInstance(); + if (instance == nullptr) + return; + + if (!instance->IsEnabled(type, level)) + return; + va_list args; va_start(args, fmt); - if (LogManager::GetInstance()) - LogManager::GetInstance()->Log(level, type, file, line, fmt, args); + char message[MAX_MSGLEN]; + CharArrayFromFormatV(message, MAX_MSGLEN, fmt, args); va_end(args); + + instance->Log(level, type, file, line, message); +} + +void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, + std::string_view format, const fmt::format_args& args) +{ + auto* instance = LogManager::GetInstance(); + if (instance == nullptr) + return; + + if (!instance->IsEnabled(type, level)) + return; + + const auto message = fmt::vformat(format, args); + instance->Log(level, type, file, line, message.c_str()); } static size_t DeterminePathCutOffPoint() @@ -196,27 +219,26 @@ void LogManager::SaveSettings() } void LogManager::Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, - const char* format, va_list args) -{ - return LogWithFullPath(level, type, file + m_path_cutoff_point, line, format, args); -} - -void LogManager::LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, - const char* format, va_list args) + const char* message) { if (!IsEnabled(type, level) || !static_cast<bool>(m_listener_ids)) return; - char temp[MAX_MSGLEN]; - CharArrayFromFormatV(temp, MAX_MSGLEN, format, args); + LogWithFullPath(level, type, file + m_path_cutoff_point, line, message); +} +void LogManager::LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, + const char* message) +{ const std::string msg = fmt::format("{} {}:{} {}[{}]: {}\n", Common::Timer::GetTimeFormatted(), file, line, - LOG_LEVEL_TO_CHAR[static_cast<int>(level)], GetShortName(type), temp); + LOG_LEVEL_TO_CHAR[static_cast<int>(level)], GetShortName(type), message); - for (auto listener_id : m_listener_ids) + for (const auto listener_id : m_listener_ids) + { if (m_listeners[listener_id]) m_listeners[listener_id]->Log(level, msg.c_str()); + } } LOG_LEVELS LogManager::GetLogLevel() const diff --git a/Source/Core/Common/Logging/LogManager.h b/Source/Core/Common/Logging/LogManager.h index eeff12d2f3..9332bfc074 100644 --- a/Source/Core/Common/Logging/LogManager.h +++ b/Source/Core/Common/Logging/LogManager.h @@ -38,10 +38,7 @@ public: static void Init(); static void Shutdown(); - void Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, - va_list args); - void LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, - va_list args); + void Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* message); LOG_LEVELS GetLogLevel() const; void SetLogLevel(LOG_LEVELS level); @@ -76,6 +73,9 @@ private: LogManager(LogManager&&) = delete; LogManager& operator=(LogManager&&) = delete; + void LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, + const char* message); + LOG_LEVELS m_level; std::array<LogContainer, NUMBER_OF_LOGS> m_log{}; std::array<LogListener*, LogListener::NUMBER_OF_LISTENERS> m_listeners{}; |
