mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-02-15 16:59:18 +01:00
Common/Log: Add basic fmt-capable functions to the interface.
Provides a basic extension to the interface to begin migration off of the printf-based logging system. Everything will go through macros with the same style naming as the old logging system, except the macros will have the _FMT suffix, while the migration is in process. This allows for peacemeal migration over time instead of pulling everything out and replacing it all in a single pull request, which makes for much easier reviewing.
This commit is contained in:
parent
dc5ae5ee66
commit
425f2aa013
@ -18,19 +18,19 @@ static ptrdiff_t s_path_cutoff_point = 0;
|
|||||||
|
|
||||||
static void LogCallback(const char* format, ...)
|
static void LogCallback(const char* format, ...)
|
||||||
{
|
{
|
||||||
if (!Common::Log::LogManager::GetInstance())
|
auto* instance = Common::Log::LogManager::GetInstance();
|
||||||
|
if (instance == nullptr)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
|
||||||
const char* filename = va_arg(args, const char*) + s_path_cutoff_point;
|
const char* filename = va_arg(args, const char*) + s_path_cutoff_point;
|
||||||
int lineno = va_arg(args, int);
|
const int lineno = va_arg(args, int);
|
||||||
std::string adapted_format(StripSpaces(format + strlen("%s:%d:")));
|
const std::string adapted_format(StripSpaces(format + strlen("%s:%d:")));
|
||||||
|
const std::string message = StringFromFormatV(adapted_format.c_str(), args);
|
||||||
Common::Log::LogManager::GetInstance()->LogWithFullPath(
|
|
||||||
Common::Log::LNOTICE, Common::Log::AUDIO, filename, lineno, adapted_format.c_str(), args);
|
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
|
instance->Log(Common::Log::LNOTICE, Common::Log::AUDIO, filename, lineno, message.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DestroyContext(cubeb* ctx)
|
static void DestroyContext(cubeb* ctx)
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <fmt/format.h>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace Common::Log
|
namespace Common::Log
|
||||||
{
|
{
|
||||||
enum LOG_TYPE
|
enum LOG_TYPE
|
||||||
@ -72,8 +75,17 @@ enum LOG_LEVELS
|
|||||||
|
|
||||||
static const char LOG_LEVEL_TO_CHAR[7] = "-NEWID";
|
static const char LOG_LEVEL_TO_CHAR[7] = "-NEWID";
|
||||||
|
|
||||||
void GenericLog(Common::Log::LOG_LEVELS level, Common::Log::LOG_TYPE type, const char* file,
|
void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
|
||||||
int line, const char* fmt, ...)
|
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__
|
#ifdef __GNUC__
|
||||||
__attribute__((format(printf, 5, 6)))
|
__attribute__((format(printf, 5, 6)))
|
||||||
#endif
|
#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__); \
|
GENERIC_LOG(Common::Log::t, Common::Log::LDEBUG, __VA_ARGS__); \
|
||||||
} while (0)
|
} 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)
|
||||||
|
@ -64,11 +64,34 @@ private:
|
|||||||
|
|
||||||
void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, ...)
|
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_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
if (LogManager::GetInstance())
|
char message[MAX_MSGLEN];
|
||||||
LogManager::GetInstance()->Log(level, type, file, line, fmt, args);
|
CharArrayFromFormatV(message, MAX_MSGLEN, fmt, args);
|
||||||
va_end(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()
|
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,
|
void LogManager::Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
|
||||||
const char* format, va_list args)
|
const char* message)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if (!IsEnabled(type, level) || !static_cast<bool>(m_listener_ids))
|
if (!IsEnabled(type, level) || !static_cast<bool>(m_listener_ids))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
char temp[MAX_MSGLEN];
|
LogWithFullPath(level, type, file + m_path_cutoff_point, line, message);
|
||||||
CharArrayFromFormatV(temp, MAX_MSGLEN, format, args);
|
}
|
||||||
|
|
||||||
|
void LogManager::LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
|
||||||
|
const char* message)
|
||||||
|
{
|
||||||
const std::string msg =
|
const std::string msg =
|
||||||
fmt::format("{} {}:{} {}[{}]: {}\n", Common::Timer::GetTimeFormatted(), file, line,
|
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])
|
if (m_listeners[listener_id])
|
||||||
m_listeners[listener_id]->Log(level, msg.c_str());
|
m_listeners[listener_id]->Log(level, msg.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_LEVELS LogManager::GetLogLevel() const
|
LOG_LEVELS LogManager::GetLogLevel() const
|
||||||
|
@ -38,10 +38,7 @@ public:
|
|||||||
static void Init();
|
static void Init();
|
||||||
static void Shutdown();
|
static void Shutdown();
|
||||||
|
|
||||||
void Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt,
|
void Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* message);
|
||||||
va_list args);
|
|
||||||
void LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt,
|
|
||||||
va_list args);
|
|
||||||
|
|
||||||
LOG_LEVELS GetLogLevel() const;
|
LOG_LEVELS GetLogLevel() const;
|
||||||
void SetLogLevel(LOG_LEVELS level);
|
void SetLogLevel(LOG_LEVELS level);
|
||||||
@ -76,6 +73,9 @@ private:
|
|||||||
LogManager(LogManager&&) = delete;
|
LogManager(LogManager&&) = delete;
|
||||||
LogManager& operator=(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;
|
LOG_LEVELS m_level;
|
||||||
std::array<LogContainer, NUMBER_OF_LOGS> m_log{};
|
std::array<LogContainer, NUMBER_OF_LOGS> m_log{};
|
||||||
std::array<LogListener*, LogListener::NUMBER_OF_LISTENERS> m_listeners{};
|
std::array<LogListener*, LogListener::NUMBER_OF_LISTENERS> m_listeners{};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user