From c296c34e0053abd5b0ec7ab18631b1261ab324f9 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 16 Nov 2021 18:48:30 -0800 Subject: [PATCH] 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. --- Source/Core/Common/MsgHandler.cpp | 13 ++++++++----- Source/Core/Common/MsgHandler.h | 22 ++++++++++++---------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Source/Core/Common/MsgHandler.cpp b/Source/Core/Common/MsgHandler.cpp index 26f0f0a89a..660782ad9d 100644 --- a/Source/Core/Common/MsgHandler.cpp +++ b/Source/Core/Common/MsgHandler.cpp @@ -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 diff --git a/Source/Core/Common/MsgHandler.h b/Source/Core/Common/MsgHandler.h index dc232fc785..84afaafbe9 100644 --- a/Source/Core/Common/MsgHandler.h +++ b/Source/Core/Common/MsgHandler.h @@ -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 -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::value); - return MsgAlertFmtImpl(yes_no, style, log_type, format, + return MsgAlertFmtImpl(yes_no, style, log_type, file, line, format, fmt::make_args_checked(format, args...)); } template -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(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( \ - 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( \ - 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, ...) \