| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <string> |
| 7 |
#include <utility> |
| 8 |
|
| 9 |
#include <fmt/format.h> |
| 10 |
|
| 11 |
#include "Common/Logging/Log.h" |
| 12 |
|
| 13 |
namespace Common |
| 14 |
{ |
| 15 |
|
| 16 |
enum class MsgType |
| 17 |
{ |
| 18 |
Information, |
| 19 |
Question, |
| 20 |
Warning, |
| 21 |
Critical |
| 22 |
}; |
| 23 |
|
| 24 |
using MsgAlertHandler = bool (*)(const char* caption, const char* text, bool yes_no, MsgType style); |
| 25 |
using StringTranslator = std::string (*)(const char* text); |
| 26 |
|
| 27 |
void RegisterMsgAlertHandler(MsgAlertHandler handler); |
| 28 |
void RegisterStringTranslator(StringTranslator translator); |
| 29 |
|
| 30 |
[[nodiscard]] std::string GetStringT(const char* string); |
| 31 |
|
| 32 |
bool MsgAlertFmtImpl(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file, |
| 33 |
int line, fmt::string_view format, const fmt::format_args& args); |
| 34 |
|
| 35 |
template <std::size_t NumFields, typename S, typename... Args> |
| 36 |
bool MsgAlertFmt(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file, |
| 37 |
int line, const S& format, const Args&... args) |
| 38 |
{ |
| 39 |
static_assert(NumFields == sizeof...(args), |
| 40 |
"Unexpected number of replacement fields in format string; did you pass too few or " |
| 41 |
"too many arguments?"); |
| 42 |
|
| 43 |
#if FMT_VERSION >= 120200 |
| 44 |
auto&& format_str = format; |
| 45 |
#elif FMT_VERSION >= 110000 |
| 46 |
static_assert(std::is_base_of_v<fmt::detail::compile_string, S>); |
| 47 |
auto&& format_str = fmt::format_string<Args...>(format); |
| 48 |
#elif FMT_VERSION >= 90000 |
| 49 |
static_assert(fmt::detail::is_compile_string<S>::value); |
| 50 |
auto&& format_str = format; |
| 51 |
#else |
| 52 |
static_assert(fmt::is_compile_string<S>::value); |
| 53 |
auto&& format_str = format; |
| 54 |
#endif |
| 55 |
return MsgAlertFmtImpl(yes_no, style, log_type, file, line, format_str, |
| 56 |
fmt::make_format_args(args...)); |
| 57 |
} |
| 58 |
|
| 59 |
template <std::size_t NumFields, bool has_non_positional_args, typename S, typename... Args> |
| 60 |
bool MsgAlertFmtT(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file, |
| 61 |
int line, const S& format, fmt::string_view translated_format, |
| 62 |
const Args&... args) |
| 63 |
{ |
| 64 |
static_assert(!has_non_positional_args, |
| 65 |
"Translatable strings must use positional arguments (e.g. {0} instead of {})"); |
| 66 |
static_assert(NumFields == sizeof...(args), |
| 67 |
"Unexpected number of replacement fields in format string; did you pass too few or " |
| 68 |
"too many arguments?"); |
| 69 |
#if FMT_VERSION < 90000 |
| 70 |
static_assert(fmt::is_compile_string<S>::value); |
| 71 |
#elif FMT_VERSION < 110000 |
| 72 |
static_assert(fmt::detail::is_compile_string<S>::value); |
| 73 |
#elif FMT_VERSION < 120200 |
| 74 |
static_assert(std::is_base_of_v<fmt::detail::compile_string, S>); |
| 75 |
#endif |
| 76 |
|
| 77 |
auto arg_list = fmt::make_format_args(args...); |
| 78 |
return MsgAlertFmtImpl(yes_no, style, log_type, file, line, translated_format, arg_list); |
| 79 |
} |
| 80 |
|
| 81 |
void SetEnableAlert(bool enable); |
| 82 |
void SetAbortOnPanicAlert(bool should_abort); |
| 83 |
|
| 84 |
|
| 85 |
template <typename... Args> |
| 86 |
std::string FmtFormatT(const char* string, Args&&... args) |
| 87 |
{ |
| 88 |
return fmt::format(fmt::runtime(Common::GetStringT(string)), std::forward<Args>(args)...); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#define GenericAlertFmt(yes_no, style, log_type, format, ...) \ |
| 95 |
Common::MsgAlertFmt<Common::CountFmtReplacementFields(format)>( \ |
| 96 |
yes_no, style, Common::Log::LogType::log_type, __FILE__, __LINE__, \ |
| 97 |
FMT_STRING(format) __VA_OPT__(, ) __VA_ARGS__) |
| 98 |
|
| 99 |
#define GenericAlertFmtT(yes_no, style, log_type, format, ...) \ |
| 100 |
Common::MsgAlertFmtT<Common::CountFmtReplacementFields(format), \ |
| 101 |
Common::ContainsNonPositionalArguments(format)>( \ |
| 102 |
yes_no, style, Common::Log::LogType::log_type, __FILE__, __LINE__, FMT_STRING(format), \ |
| 103 |
Common::GetStringT(format) __VA_OPT__(, ) __VA_ARGS__) |
| 104 |
|
| 105 |
#define SuccessAlertFmt(format, ...) \ |
| 106 |
GenericAlertFmt(false, Common::MsgType::Information, MASTER_LOG, \ |
| 107 |
format __VA_OPT__(, ) __VA_ARGS__) |
| 108 |
|
| 109 |
#define PanicAlertFmt(format, ...) \ |
| 110 |
GenericAlertFmt(false, Common::MsgType::Warning, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__) |
| 111 |
|
| 112 |
#define PanicYesNoFmt(format, ...) \ |
| 113 |
GenericAlertFmt(true, Common::MsgType::Warning, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__) |
| 114 |
|
| 115 |
#define AskYesNoFmt(format, ...) \ |
| 116 |
GenericAlertFmt(true, Common::MsgType::Question, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__) |
| 117 |
|
| 118 |
#define CriticalAlertFmt(format, ...) \ |
| 119 |
GenericAlertFmt(false, Common::MsgType::Critical, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__) |
| 120 |
|
| 121 |
|
| 122 |
#define SuccessAlertFmtT(format, ...) \ |
| 123 |
GenericAlertFmtT(false, Common::MsgType::Information, MASTER_LOG, \ |
| 124 |
format __VA_OPT__(, ) __VA_ARGS__) |
| 125 |
|
| 126 |
#define PanicAlertFmtT(format, ...) \ |
| 127 |
GenericAlertFmtT(false, Common::MsgType::Warning, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__) |
| 128 |
|
| 129 |
#define PanicYesNoFmtT(format, ...) \ |
| 130 |
GenericAlertFmtT(true, Common::MsgType::Warning, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__) |
| 131 |
|
| 132 |
#define AskYesNoFmtT(format, ...) \ |
| 133 |
GenericAlertFmtT(true, Common::MsgType::Question, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__) |
| 134 |
|
| 135 |
#define CriticalAlertFmtT(format, ...) \ |
| 136 |
GenericAlertFmtT(false, Common::MsgType::Critical, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__) |
| 137 |
|
| 138 |
|
| 139 |
#define PanicYesNoFmtAssert(log_type, format, ...) \ |
| 140 |
GenericAlertFmt(true, Common::MsgType::Warning, log_type, format __VA_OPT__(, ) __VA_ARGS__) |