From 027c17558fde26c51688e44707f6eb3a13c67be4 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 16 Jun 2019 23:24:26 -0400 Subject: Common/MsgHandler: Use fmt::print on non-Windows OSes Provides the same behavior. --- Source/Core/Common/MsgHandler.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'Source/Core/Common/MsgHandler.cpp') diff --git a/Source/Core/Common/MsgHandler.cpp b/Source/Core/Common/MsgHandler.cpp index 43f24c9865..01a51a7e3f 100644 --- a/Source/Core/Common/MsgHandler.cpp +++ b/Source/Core/Common/MsgHandler.cpp @@ -2,20 +2,23 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include "Common/MsgHandler.h" + #include -#include #include +#ifdef _WIN32 +#include +#else +#include +#include +#endif + #include "Common/Common.h" #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" -#include "Common/MsgHandler.h" #include "Common/StringUtil.h" -#ifdef _WIN32 -#include -#endif - bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, MsgType style); static MsgAlertHandler msg_handler = DefaultMsgHandler; static bool AlertEnabled = true; @@ -111,7 +114,7 @@ bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, MsgTy return IDYES == MessageBox(0, UTF8ToTStr(text).c_str(), UTF8ToTStr(caption).c_str(), window_style | (yes_no ? MB_YESNO : MB_OK)); #else - fprintf(stderr, "%s\n", text); + fmt::print(stderr, "{}\n", text); // Return no to any question (which will in general crash the emulator) return false; -- cgit v1.2.3 From 76b675e9f02b9ae6d8c969298a24b846e322c649 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 16 Jun 2019 23:30:46 -0400 Subject: Common/MsgHandler: Make default message handler and translator's internally linked Previously these functions were declared without the static specifier, giving them external linkage, which isn't really ideal. Instead, we can place these functions up by the relevant file-scope variables and place them inside an anonymous namespace with said variables, giving them internal linkage. --- Source/Core/Common/MsgHandler.cpp | 62 +++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'Source/Core/Common/MsgHandler.cpp') diff --git a/Source/Core/Common/MsgHandler.cpp b/Source/Core/Common/MsgHandler.cpp index 01a51a7e3f..2c36172cd2 100644 --- a/Source/Core/Common/MsgHandler.cpp +++ b/Source/Core/Common/MsgHandler.cpp @@ -19,12 +19,38 @@ #include "Common/Logging/Log.h" #include "Common/StringUtil.h" -bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, MsgType style); -static MsgAlertHandler msg_handler = DefaultMsgHandler; -static bool AlertEnabled = true; +namespace +{ +// Default non library dependent panic alert +bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, MsgType style) +{ +#ifdef _WIN32 + int window_style = MB_ICONINFORMATION; + if (style == MsgType::Question) + window_style = MB_ICONQUESTION; + if (style == MsgType::Warning) + window_style = MB_ICONWARNING; + + return IDYES == MessageBox(0, UTF8ToTStr(text).c_str(), UTF8ToTStr(caption).c_str(), + window_style | (yes_no ? MB_YESNO : MB_OK)); +#else + fmt::print(stderr, "{}\n", text); + + // Return no to any question (which will in general crash the emulator) + return false; +#endif +} + +// Default (non) translator +std::string DefaultStringTranslator(const char* text) +{ + return text; +} -std::string DefaultStringTranslator(const char* text); -static StringTranslator str_translator = DefaultStringTranslator; +MsgAlertHandler msg_handler = DefaultMsgHandler; +StringTranslator str_translator = DefaultStringTranslator; +bool AlertEnabled = true; +} // Anonymous namespace // Select which of these functions that are used for message boxes. If // Qt is enabled we will use QtMsgAlertHandler() that is defined in Main.cpp @@ -100,29 +126,3 @@ bool MsgAlert(bool yes_no, MsgType style, const char* format, ...) return true; } - -// Default non library dependent panic alert -bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, MsgType style) -{ -#ifdef _WIN32 - int window_style = MB_ICONINFORMATION; - if (style == MsgType::Question) - window_style = MB_ICONQUESTION; - if (style == MsgType::Warning) - window_style = MB_ICONWARNING; - - return IDYES == MessageBox(0, UTF8ToTStr(text).c_str(), UTF8ToTStr(caption).c_str(), - window_style | (yes_no ? MB_YESNO : MB_OK)); -#else - fmt::print(stderr, "{}\n", text); - - // Return no to any question (which will in general crash the emulator) - return false; -#endif -} - -// Default (non) translator -std::string DefaultStringTranslator(const char* text) -{ - return text; -} -- cgit v1.2.3 From 1968643297189887b4b3a76890e385b0f7a66ec7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 16 Jun 2019 23:34:09 -0400 Subject: Common/MsgHandler: Amend file-scope variable names Makes them follow our coding style. --- Source/Core/Common/MsgHandler.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'Source/Core/Common/MsgHandler.cpp') diff --git a/Source/Core/Common/MsgHandler.cpp b/Source/Core/Common/MsgHandler.cpp index 2c36172cd2..360a77ca0f 100644 --- a/Source/Core/Common/MsgHandler.cpp +++ b/Source/Core/Common/MsgHandler.cpp @@ -47,33 +47,33 @@ std::string DefaultStringTranslator(const char* text) return text; } -MsgAlertHandler msg_handler = DefaultMsgHandler; -StringTranslator str_translator = DefaultStringTranslator; -bool AlertEnabled = true; +MsgAlertHandler s_msg_handler = DefaultMsgHandler; +StringTranslator s_str_translator = DefaultStringTranslator; +bool s_alert_enabled = true; } // Anonymous namespace // Select which of these functions that are used for message boxes. If // Qt is enabled we will use QtMsgAlertHandler() that is defined in Main.cpp void RegisterMsgAlertHandler(MsgAlertHandler handler) { - msg_handler = handler; + s_msg_handler = handler; } // Select translation function. void RegisterStringTranslator(StringTranslator translator) { - str_translator = translator; + s_str_translator = translator; } // enable/disable the alert handler void SetEnableAlert(bool enable) { - AlertEnabled = enable; + s_alert_enabled = enable; } std::string GetStringT(const char* string) { - return str_translator(string); + return s_str_translator(string); } // This is the first stop for gui alerts where the log is updated and the @@ -91,10 +91,10 @@ bool MsgAlert(bool yes_no, MsgType style, const char* format, ...) if (!info_caption.length()) { - info_caption = str_translator(_trans("Information")); - ques_caption = str_translator(_trans("Question")); - warn_caption = str_translator(_trans("Warning")); - crit_caption = str_translator(_trans("Critical")); + info_caption = s_str_translator(_trans("Information")); + ques_caption = s_str_translator(_trans("Question")); + warn_caption = s_str_translator(_trans("Warning")); + crit_caption = s_str_translator(_trans("Critical")); } switch (style) @@ -115,14 +115,14 @@ bool MsgAlert(bool yes_no, MsgType style, const char* format, ...) va_list args; va_start(args, format); - CharArrayFromFormatV(buffer, sizeof(buffer) - 1, str_translator(format).c_str(), args); + CharArrayFromFormatV(buffer, sizeof(buffer) - 1, s_str_translator(format).c_str(), args); va_end(args); ERROR_LOG(MASTER_LOG, "%s: %s", caption.c_str(), buffer); // Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored - if (msg_handler && (AlertEnabled || style == MsgType::Question || style == MsgType::Critical)) - return msg_handler(caption.c_str(), buffer, yes_no, style); + if (s_msg_handler && (s_alert_enabled || style == MsgType::Question || style == MsgType::Critical)) + return s_msg_handler(caption.c_str(), buffer, yes_no, style); return true; } -- cgit v1.2.3 From 0eddf6dd8f2eb02bba48e38c24a90180e45b3799 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 19 Jun 2019 15:58:46 -0400 Subject: Common/MsgHandler: Use std::string's empty for emptiness checking in MsgAlert Provides more straightforward code compared to negating a length check. --- Source/Core/Common/MsgHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/Common/MsgHandler.cpp') diff --git a/Source/Core/Common/MsgHandler.cpp b/Source/Core/Common/MsgHandler.cpp index 360a77ca0f..4744a5ff44 100644 --- a/Source/Core/Common/MsgHandler.cpp +++ b/Source/Core/Common/MsgHandler.cpp @@ -89,7 +89,7 @@ bool MsgAlert(bool yes_no, MsgType style, const char* format, ...) static std::string ques_caption; static std::string crit_caption; - if (!info_caption.length()) + if (info_caption.empty()) { info_caption = s_str_translator(_trans("Information")); ques_caption = s_str_translator(_trans("Question")); -- cgit v1.2.3 From e7dd46a531c342ee3e041de2e1edc96a123ee5dd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 19 Jun 2019 16:01:24 -0400 Subject: Common/MsgHandler: Brace if statement in MsgAlert The condition travels more than one line, so the body should be braced. While we're at it, make the comparison against nullptr explicit. --- Source/Core/Common/MsgHandler.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'Source/Core/Common/MsgHandler.cpp') diff --git a/Source/Core/Common/MsgHandler.cpp b/Source/Core/Common/MsgHandler.cpp index 4744a5ff44..41979c1ef0 100644 --- a/Source/Core/Common/MsgHandler.cpp +++ b/Source/Core/Common/MsgHandler.cpp @@ -121,8 +121,11 @@ bool MsgAlert(bool yes_no, MsgType style, const char* format, ...) ERROR_LOG(MASTER_LOG, "%s: %s", caption.c_str(), buffer); // Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored - if (s_msg_handler && (s_alert_enabled || style == MsgType::Question || style == MsgType::Critical)) + if (s_msg_handler != nullptr && + (s_alert_enabled || style == MsgType::Question || style == MsgType::Critical)) + { return s_msg_handler(caption.c_str(), buffer, yes_no, style); + } return true; } -- cgit v1.2.3 From 4f1f55093fcf98aa1592b6b8b1ad0d33c4461b50 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 16 Jun 2019 23:45:37 -0400 Subject: Common/MsgHandler: Namespace code within the Common namespace Closes another gap in the Common library where code isn't being namespaced under it. --- Source/Core/Common/MsgHandler.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'Source/Core/Common/MsgHandler.cpp') diff --git a/Source/Core/Common/MsgHandler.cpp b/Source/Core/Common/MsgHandler.cpp index 41979c1ef0..6b24991526 100644 --- a/Source/Core/Common/MsgHandler.cpp +++ b/Source/Core/Common/MsgHandler.cpp @@ -19,6 +19,8 @@ #include "Common/Logging/Log.h" #include "Common/StringUtil.h" +namespace Common +{ namespace { // Default non library dependent panic alert @@ -129,3 +131,4 @@ bool MsgAlert(bool yes_no, MsgType style, const char* format, ...) return true; } +} // namespace Common -- cgit v1.2.3