mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
Merge pull request #9260 from leoetlino/fmt-checks
Common/Log: Add compile-time format string checks
This commit is contained in:
@ -4,8 +4,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <fmt/format.h>
|
||||
#include <string_view>
|
||||
#include "Common/FormatUtil.h"
|
||||
|
||||
namespace Common::Log
|
||||
{
|
||||
@ -77,13 +79,17 @@ enum LOG_LEVELS
|
||||
static const char LOG_LEVEL_TO_CHAR[7] = "-NEWID";
|
||||
|
||||
void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
|
||||
std::string_view format, const fmt::format_args& args);
|
||||
fmt::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)
|
||||
template <std::size_t NumFields, typename S, typename... Args>
|
||||
void GenericLogFmt(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const S& format,
|
||||
const Args&... args)
|
||||
{
|
||||
GenericLogFmtImpl(level, type, file, line, format, fmt::make_format_args(args...));
|
||||
static_assert(NumFields == sizeof...(args),
|
||||
"Unexpected number of replacement fields in format string; did you pass too few or "
|
||||
"too many arguments?");
|
||||
GenericLogFmtImpl(level, type, file, line, format,
|
||||
fmt::make_args_checked<Args...>(format, args...));
|
||||
}
|
||||
|
||||
void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, ...)
|
||||
@ -137,11 +143,16 @@ void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, con
|
||||
|
||||
// fmtlib capable API
|
||||
|
||||
#define GENERIC_LOG_FMT(t, v, ...) \
|
||||
#define GENERIC_LOG_FMT(t, v, format, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (v <= MAX_LOGLEVEL) \
|
||||
Common::Log::GenericLogFmt(v, t, __FILE__, __LINE__, __VA_ARGS__); \
|
||||
{ \
|
||||
/* Use a macro-like name to avoid shadowing warnings */ \
|
||||
constexpr auto GENERIC_LOG_FMT_N = Common::CountFmtReplacementFields(format); \
|
||||
Common::Log::GenericLogFmt<GENERIC_LOG_FMT_N>(v, t, __FILE__, __LINE__, FMT_STRING(format), \
|
||||
##__VA_ARGS__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define ERROR_LOG_FMT(t, ...) \
|
||||
|
Reference in New Issue
Block a user