2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 05:09:55 +02:00
|
|
|
// Refer to the license.txt file included.
|
2009-02-23 07:15:48 +01:00
|
|
|
|
2019-06-17 05:24:26 +02:00
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <string>
|
2009-02-23 07:15:48 +01:00
|
|
|
|
2019-06-17 05:24:26 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <cstdio>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#endif
|
|
|
|
|
2015-09-28 17:57:16 +02:00
|
|
|
#include "Common/Common.h"
|
2014-09-08 03:06:58 +02:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 23:13:07 +02:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "Common/StringUtil.h"
|
2009-02-23 07:15:48 +01:00
|
|
|
|
2019-06-17 05:45:37 +02:00
|
|
|
namespace Common
|
|
|
|
{
|
2019-06-17 05:30:46 +02:00
|
|
|
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;
|
|
|
|
}
|
2009-02-23 07:15:48 +01:00
|
|
|
|
2019-06-17 05:34:09 +02:00
|
|
|
MsgAlertHandler s_msg_handler = DefaultMsgHandler;
|
|
|
|
StringTranslator s_str_translator = DefaultStringTranslator;
|
|
|
|
bool s_alert_enabled = true;
|
2020-11-09 13:50:35 +01:00
|
|
|
|
|
|
|
const char* GetCaption(MsgType style)
|
|
|
|
{
|
2021-06-12 23:42:27 +02:00
|
|
|
static const std::string info_caption = GetStringT("Information");
|
|
|
|
static const std::string ques_caption = GetStringT("Question");
|
|
|
|
static const std::string warn_caption = GetStringT("Warning");
|
|
|
|
static const std::string crit_caption = GetStringT("Critical");
|
2020-11-09 13:50:35 +01:00
|
|
|
|
|
|
|
switch (style)
|
|
|
|
{
|
|
|
|
case MsgType::Information:
|
|
|
|
return info_caption.c_str();
|
|
|
|
case MsgType::Question:
|
|
|
|
return ques_caption.c_str();
|
|
|
|
case MsgType::Warning:
|
|
|
|
return warn_caption.c_str();
|
|
|
|
case MsgType::Critical:
|
|
|
|
return crit_caption.c_str();
|
|
|
|
default:
|
|
|
|
return "Unhandled caption";
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 05:30:46 +02:00
|
|
|
} // Anonymous namespace
|
2011-01-13 03:05:58 +01:00
|
|
|
|
2011-02-24 06:05:25 +01:00
|
|
|
// Select which of these functions that are used for message boxes. If
|
2018-08-07 15:48:17 +02:00
|
|
|
// Qt is enabled we will use QtMsgAlertHandler() that is defined in Main.cpp
|
2009-02-23 07:15:48 +01:00
|
|
|
void RegisterMsgAlertHandler(MsgAlertHandler handler)
|
|
|
|
{
|
2019-06-17 05:34:09 +02:00
|
|
|
s_msg_handler = handler;
|
2009-02-23 07:15:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 15:48:17 +02:00
|
|
|
// Select translation function.
|
2011-01-13 03:05:58 +01:00
|
|
|
void RegisterStringTranslator(StringTranslator translator)
|
|
|
|
{
|
2019-06-17 05:34:09 +02:00
|
|
|
s_str_translator = translator;
|
2011-01-13 03:05:58 +01:00
|
|
|
}
|
|
|
|
|
2009-03-18 23:05:31 +01:00
|
|
|
// enable/disable the alert handler
|
2011-01-13 03:05:58 +01:00
|
|
|
void SetEnableAlert(bool enable)
|
|
|
|
{
|
2019-06-17 05:34:09 +02:00
|
|
|
s_alert_enabled = enable;
|
2009-03-18 23:05:31 +01:00
|
|
|
}
|
|
|
|
|
2017-07-26 06:11:17 +02:00
|
|
|
std::string GetStringT(const char* string)
|
2015-11-20 11:33:47 +01:00
|
|
|
{
|
2019-06-17 05:34:09 +02:00
|
|
|
return s_str_translator(string);
|
2015-11-20 11:33:47 +01:00
|
|
|
}
|
|
|
|
|
2011-02-24 06:05:25 +01:00
|
|
|
// This is the first stop for gui alerts where the log is updated and the
|
|
|
|
// correct window is shown
|
2017-07-26 06:11:17 +02:00
|
|
|
bool MsgAlert(bool yes_no, MsgType style, const char* format, ...)
|
2009-02-23 07:15:48 +01:00
|
|
|
{
|
|
|
|
// Read message and write it to the log
|
2020-11-09 13:50:35 +01:00
|
|
|
const char* caption = GetCaption(style);
|
2011-09-09 00:32:04 +02:00
|
|
|
char buffer[2048];
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2009-02-23 07:15:48 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2019-06-17 05:34:09 +02:00
|
|
|
CharArrayFromFormatV(buffer, sizeof(buffer) - 1, s_str_translator(format).c_str(), args);
|
2009-02-23 07:15:48 +01:00
|
|
|
va_end(args);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2020-10-23 20:41:30 +02:00
|
|
|
ERROR_LOG_FMT(MASTER_LOG, "{}: {}", caption, buffer);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2009-06-08 12:16:08 +02:00
|
|
|
// Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored
|
2019-06-19 22:01:24 +02:00
|
|
|
if (s_msg_handler != nullptr &&
|
|
|
|
(s_alert_enabled || style == MsgType::Question || style == MsgType::Critical))
|
|
|
|
{
|
2019-07-20 21:03:45 +02:00
|
|
|
return s_msg_handler(caption, buffer, yes_no, style);
|
2019-06-19 22:01:24 +02:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2011-02-23 17:00:01 +01:00
|
|
|
return true;
|
2009-02-23 07:15:48 +01:00
|
|
|
}
|
2020-11-09 13:50:35 +01:00
|
|
|
|
|
|
|
bool MsgAlertFmtImpl(bool yes_no, MsgType style, fmt::string_view format,
|
|
|
|
const fmt::format_args& args)
|
|
|
|
{
|
|
|
|
const char* caption = GetCaption(style);
|
|
|
|
const auto message = fmt::vformat(format, args);
|
|
|
|
ERROR_LOG_FMT(MASTER_LOG, "{}: {}", caption, message);
|
|
|
|
|
|
|
|
// Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored
|
|
|
|
if (s_msg_handler != nullptr &&
|
|
|
|
(s_alert_enabled || style == MsgType::Question || style == MsgType::Critical))
|
|
|
|
{
|
|
|
|
return s_msg_handler(caption, message.c_str(), yes_no, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-17 05:45:37 +02:00
|
|
|
} // namespace Common
|