| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/MsgHandler.h" |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <string> |
| 8 | |
| 9 | #ifdef _WIN32 |
| 10 | #include <windows.h> |
| 11 | #else |
| 12 | #include <fmt/format.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "Common/Logging/Log.h" |
| 16 | #ifdef _WIN32 |
| 17 | #include "Common/StringUtil.h" |
| 18 | #endif |
| 19 | |
| 20 | namespace Common |
| 21 | { |
| 22 | namespace |
| 23 | { |
| 24 | // Default non library dependent panic alert |
| 25 | bool DefaultMsgHandler(const char* caption, const char* text, bool yes_no, MsgType style) |
| 26 | { |
| 27 | #ifdef _WIN32 |
| 28 | int window_style = MB_ICONINFORMATION; |
| 29 | if (style == MsgType::Question) |
| 30 | window_style = MB_ICONQUESTION; |
| 31 | if (style == MsgType::Warning) |
| 32 | window_style = MB_ICONWARNING; |
| 33 | |
| 34 | return IDYES == MessageBox(0, UTF8ToTStr(text).c_str(), UTF8ToTStr(caption).c_str(), |
| 35 | window_style | (yes_no ? MB_YESNO : MB_OK)); |
| 36 | #else |
| 37 | fmt::print(stderr, "{}\n", text); |
| 38 | |
| 39 | // Return no to any question (which will in general crash the emulator) |
| 40 | return false; |
| 41 | #endif |
| 42 | } |
| 43 | |
| 44 | // Default (non) translator |
| 45 | std::string DefaultStringTranslator(const char* text) |
| 46 | { |
| 47 | return text; |
| 48 | } |
| 49 | |
| 50 | MsgAlertHandler s_msg_handler = DefaultMsgHandler; |
| 51 | StringTranslator s_str_translator = DefaultStringTranslator; |
| 52 | bool s_alert_enabled = true; |
| 53 | bool s_abort_on_panic_alert = false; |
| 54 | |
| 55 | const char* GetCaption(MsgType style) |
| 56 | { |
| 57 | static const std::string info_caption = GetStringT("Information"); |
| 58 | static const std::string ques_caption = GetStringT("Question"); |
| 59 | static const std::string warn_caption = GetStringT("Warning"); |
| 60 | static const std::string crit_caption = GetStringT("Critical"); |
| 61 | |
| 62 | switch (style) |
| 63 | { |
| 64 | case MsgType::Information: |
| 65 | return info_caption.c_str(); |
| 66 | case MsgType::Question: |
| 67 | return ques_caption.c_str(); |
| 68 | case MsgType::Warning: |
| 69 | return warn_caption.c_str(); |
| 70 | case MsgType::Critical: |
| 71 | return crit_caption.c_str(); |
| 72 | default: |
| 73 | return "Unhandled caption"; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | Log::LogLevel GetMessageAlertLogLevel(const MsgType style) |
| 78 | { |
| 79 | switch (style) |
| 80 | { |
| 81 | case MsgType::Critical: |
| 82 | return Log::LogLevel::LERROR; |
| 83 | case MsgType::Warning: |
| 84 | return Log::LogLevel::LWARNING; |
| 85 | default: |
| 86 | return Log::LogLevel::LINFO; |
| 87 | } |
| 88 | } |
| 89 | } // Anonymous namespace |
| 90 | |
| 91 | // Select which of these functions that are used for message boxes. If |
| 92 | // Qt is enabled we will use QtMsgAlertHandler() that is defined in Main.cpp |
| 93 | void RegisterMsgAlertHandler(MsgAlertHandler handler) |
| 94 | { |
| 95 | s_msg_handler = handler; |
| 96 | } |
| 97 | |
| 98 | // Select translation function. |
| 99 | void RegisterStringTranslator(StringTranslator translator) |
| 100 | { |
| 101 | s_str_translator = translator; |
| 102 | } |
| 103 | |
| 104 | // enable/disable the alert handler |
| 105 | void SetEnableAlert(bool enable) |
| 106 | { |
| 107 | s_alert_enabled = enable; |
| 108 | } |
| 109 | |
| 110 | void SetAbortOnPanicAlert(bool should_abort) |
| 111 | { |
| 112 | s_abort_on_panic_alert = should_abort; |
| 113 | } |
| 114 | |
| 115 | std::string GetStringT(const char* string) |
| 116 | { |
| 117 | return s_str_translator(string); |
| 118 | } |
| 119 | |
| 120 | static bool ShowMessageAlert(std::string_view text, bool yes_no, Common::Log::LogType log_type, |
| 121 | MsgType style, const char* file, int line) |
| 122 | { |
| 123 | const char* caption = GetCaption(style); |
| 124 | const Log::LogLevel level = GetMessageAlertLogLevel(style); |
| 125 | // Directly call GenericLogFmt rather than using the normal log macros so that we can use the |
| 126 | // caller's line file and line number |
| 127 | Common::Log::GenericLogFmt<2>(level, log_type, file, line, FMT_STRING("{}: {}"), caption, text); |
| 128 | |
| 129 | // Panic alerts. |
| 130 | if (style == MsgType::Warning && s_abort_on_panic_alert) |
| 131 | { |
| 132 | std::abort(); |
| 133 | } |
| 134 | |
| 135 | // Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored |
| 136 | if (s_msg_handler != nullptr && |
| 137 | (s_alert_enabled || style == MsgType::Question || style == MsgType::Critical)) |
| 138 | { |
| 139 | return s_msg_handler(caption, text.data(), yes_no, style); |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | // This is the first stop for gui alerts where the log is updated and the |
| 146 | // correct window is shown, when using fmt |
| 147 | bool MsgAlertFmtImpl(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file, |
| 148 | int line, fmt::string_view format, const fmt::format_args& args) |
| 149 | { |
| 150 | const auto message = fmt::vformat(format, args); |
| 151 | |
| 152 | return ShowMessageAlert(message, yes_no, log_type, style, file, line); |
| 153 | } |
| 154 | } // namespace Common |