Common/MsgHandler: Use caller's file and line number in log messages

This will assist with finding the source of a panic alert based on logs; before, Common\MsgHandler.cpp:113 (or similar) was always used.
This commit is contained in:
Pokechu22 2021-11-16 18:48:30 -08:00
parent f55571ee5d
commit c296c34e00
2 changed files with 20 additions and 15 deletions

View File

@ -107,10 +107,13 @@ std::string GetStringT(const char* string)
}
static bool ShowMessageAlert(std::string_view text, bool yes_no, Common::Log::LogType log_type,
MsgType style)
MsgType style, const char* file, int line)
{
const char* caption = GetCaption(style);
GENERIC_LOG_FMT(log_type, Common::Log::LogLevel::LERROR, "{}: {}", caption, text);
// Directly call GenericLogFmt rather than using the normal log macros so that we can use the
// caller's line file and line number
Common::Log::GenericLogFmt<2>(Common::Log::LogLevel::LERROR, log_type, file, line,
FMT_STRING("{}: {}"), caption, text);
// Panic alerts.
if (style == MsgType::Warning && s_abort_on_panic_alert)
@ -130,11 +133,11 @@ static bool ShowMessageAlert(std::string_view text, bool yes_no, Common::Log::Lo
// This is the first stop for gui alerts where the log is updated and the
// correct window is shown, when using fmt
bool MsgAlertFmtImpl(bool yes_no, MsgType style, Common::Log::LogType log_type,
fmt::string_view format, const fmt::format_args& args)
bool MsgAlertFmtImpl(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file,
int line, fmt::string_view format, const fmt::format_args& args)
{
const auto message = fmt::vformat(format, args);
return ShowMessageAlert(message, yes_no, log_type, style);
return ShowMessageAlert(message, yes_no, log_type, style, file, line);
}
} // namespace Common

View File

@ -31,24 +31,25 @@ void RegisterStringTranslator(StringTranslator translator);
std::string GetStringT(const char* string);
bool MsgAlertFmtImpl(bool yes_no, MsgType style, Common::Log::LogType log_type,
fmt::string_view format, const fmt::format_args& args);
bool MsgAlertFmtImpl(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file,
int line, fmt::string_view format, const fmt::format_args& args);
template <std::size_t NumFields, typename S, typename... Args>
bool MsgAlertFmt(bool yes_no, MsgType style, Common::Log::LogType log_type, const S& format,
const Args&... args)
bool MsgAlertFmt(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file,
int line, const S& format, const Args&... args)
{
static_assert(NumFields == sizeof...(args),
"Unexpected number of replacement fields in format string; did you pass too few or "
"too many arguments?");
static_assert(fmt::is_compile_string<S>::value);
return MsgAlertFmtImpl(yes_no, style, log_type, format,
return MsgAlertFmtImpl(yes_no, style, log_type, file, line, format,
fmt::make_args_checked<Args...>(format, args...));
}
template <std::size_t NumFields, bool has_non_positional_args, typename S, typename... Args>
bool MsgAlertFmtT(bool yes_no, MsgType style, Common::Log::LogType log_type, const S& format,
fmt::string_view translated_format, const Args&... args)
bool MsgAlertFmtT(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file,
int line, const S& format, fmt::string_view translated_format,
const Args&... args)
{
static_assert(!has_non_positional_args,
"Translatable strings must use positional arguments (e.g. {0} instead of {})");
@ -62,7 +63,7 @@ bool MsgAlertFmtT(bool yes_no, MsgType style, Common::Log::LogType log_type, con
// translations. Still, verifying that the English string is correct will help ensure that
// translations use valid strings.
auto arg_list = fmt::make_args_checked<Args...>(format, args...);
return MsgAlertFmtImpl(yes_no, style, log_type, translated_format, arg_list);
return MsgAlertFmtImpl(yes_no, style, log_type, file, line, translated_format, arg_list);
}
void SetEnableAlert(bool enable);
@ -80,12 +81,13 @@ std::string FmtFormatT(const char* string, Args&&... args)
#define GenericAlertFmt(yes_no, style, log_type, format, ...) \
Common::MsgAlertFmt<Common::CountFmtReplacementFields(format)>( \
yes_no, style, Common::Log::LogType::log_type, FMT_STRING(format), ##__VA_ARGS__)
yes_no, style, Common::Log::LogType::log_type, __FILE__, __LINE__, FMT_STRING(format), \
##__VA_ARGS__)
#define GenericAlertFmtT(yes_no, style, log_type, format, ...) \
Common::MsgAlertFmtT<Common::CountFmtReplacementFields(format), \
Common::ContainsNonPositionalArguments(format)>( \
yes_no, style, Common::Log::LogType::log_type, FMT_STRING(format), \
yes_no, style, Common::Log::LogType::log_type, __FILE__, __LINE__, FMT_STRING(format), \
Common::GetStringT(format), ##__VA_ARGS__)
#define SuccessAlertFmt(format, ...) \