MsgHandler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstdint>
  5. #include <string>
  6. #include <utility>
  7. #include <fmt/format.h>
  8. #include "Common/FormatUtil.h"
  9. #include "Common/Logging/Log.h"
  10. namespace Common
  11. {
  12. // Message alerts
  13. enum class MsgType
  14. {
  15. Information,
  16. Question,
  17. Warning,
  18. Critical
  19. };
  20. using MsgAlertHandler = bool (*)(const char* caption, const char* text, bool yes_no, MsgType style);
  21. using StringTranslator = std::string (*)(const char* text);
  22. void RegisterMsgAlertHandler(MsgAlertHandler handler);
  23. void RegisterStringTranslator(StringTranslator translator);
  24. [[nodiscard]] std::string GetStringT(const char* string);
  25. bool MsgAlertFmtImpl(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file,
  26. int line, fmt::string_view format, const fmt::format_args& args);
  27. template <std::size_t NumFields, typename S, typename... Args>
  28. bool MsgAlertFmt(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file,
  29. int line, const S& format, const Args&... args)
  30. {
  31. static_assert(NumFields == sizeof...(args),
  32. "Unexpected number of replacement fields in format string; did you pass too few or "
  33. "too many arguments?");
  34. #if FMT_VERSION >= 110000
  35. static_assert(std::is_base_of_v<fmt::detail::compile_string, S>);
  36. auto&& format_str = fmt::format_string<Args...>(format);
  37. #elif FMT_VERSION >= 90000
  38. static_assert(fmt::detail::is_compile_string<S>::value);
  39. auto&& format_str = format;
  40. #else
  41. static_assert(fmt::is_compile_string<S>::value);
  42. auto&& format_str = format;
  43. #endif
  44. return MsgAlertFmtImpl(yes_no, style, log_type, file, line, format_str,
  45. fmt::make_format_args(args...));
  46. }
  47. template <std::size_t NumFields, bool has_non_positional_args, typename S, typename... Args>
  48. bool MsgAlertFmtT(bool yes_no, MsgType style, Common::Log::LogType log_type, const char* file,
  49. int line, const S& format, fmt::string_view translated_format,
  50. const Args&... args)
  51. {
  52. static_assert(!has_non_positional_args,
  53. "Translatable strings must use positional arguments (e.g. {0} instead of {})");
  54. static_assert(NumFields == sizeof...(args),
  55. "Unexpected number of replacement fields in format string; did you pass too few or "
  56. "too many arguments?");
  57. #if FMT_VERSION >= 110000
  58. static_assert(std::is_base_of_v<fmt::detail::compile_string, S>);
  59. #elif FMT_VERSION >= 90000
  60. static_assert(fmt::detail::is_compile_string<S>::value);
  61. #else
  62. static_assert(fmt::is_compile_string<S>::value);
  63. #endif
  64. auto arg_list = fmt::make_format_args(args...);
  65. return MsgAlertFmtImpl(yes_no, style, log_type, file, line, translated_format, arg_list);
  66. }
  67. void SetEnableAlert(bool enable);
  68. void SetAbortOnPanicAlert(bool should_abort);
  69. // Like fmt::format, except the string becomes translatable
  70. template <typename... Args>
  71. std::string FmtFormatT(const char* string, Args&&... args)
  72. {
  73. return fmt::format(fmt::runtime(Common::GetStringT(string)), std::forward<Args>(args)...);
  74. }
  75. } // namespace Common
  76. // Fmt-capable variants of the macros
  77. #define GenericAlertFmt(yes_no, style, log_type, format, ...) \
  78. Common::MsgAlertFmt<Common::CountFmtReplacementFields(format)>( \
  79. yes_no, style, Common::Log::LogType::log_type, __FILE__, __LINE__, \
  80. FMT_STRING(format) __VA_OPT__(, ) __VA_ARGS__)
  81. #define GenericAlertFmtT(yes_no, style, log_type, format, ...) \
  82. Common::MsgAlertFmtT<Common::CountFmtReplacementFields(format), \
  83. Common::ContainsNonPositionalArguments(format)>( \
  84. yes_no, style, Common::Log::LogType::log_type, __FILE__, __LINE__, FMT_STRING(format), \
  85. Common::GetStringT(format) __VA_OPT__(, ) __VA_ARGS__)
  86. #define SuccessAlertFmt(format, ...) \
  87. GenericAlertFmt(false, Common::MsgType::Information, MASTER_LOG, \
  88. format __VA_OPT__(, ) __VA_ARGS__)
  89. #define PanicAlertFmt(format, ...) \
  90. GenericAlertFmt(false, Common::MsgType::Warning, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__)
  91. #define PanicYesNoFmt(format, ...) \
  92. GenericAlertFmt(true, Common::MsgType::Warning, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__)
  93. #define AskYesNoFmt(format, ...) \
  94. GenericAlertFmt(true, Common::MsgType::Question, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__)
  95. #define CriticalAlertFmt(format, ...) \
  96. GenericAlertFmt(false, Common::MsgType::Critical, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__)
  97. // Use these macros (that do the same thing) if the message should be translated.
  98. #define SuccessAlertFmtT(format, ...) \
  99. GenericAlertFmtT(false, Common::MsgType::Information, MASTER_LOG, \
  100. format __VA_OPT__(, ) __VA_ARGS__)
  101. #define PanicAlertFmtT(format, ...) \
  102. GenericAlertFmtT(false, Common::MsgType::Warning, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__)
  103. #define PanicYesNoFmtT(format, ...) \
  104. GenericAlertFmtT(true, Common::MsgType::Warning, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__)
  105. #define AskYesNoFmtT(format, ...) \
  106. GenericAlertFmtT(true, Common::MsgType::Question, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__)
  107. #define CriticalAlertFmtT(format, ...) \
  108. GenericAlertFmtT(false, Common::MsgType::Critical, MASTER_LOG, format __VA_OPT__(, ) __VA_ARGS__)
  109. // Variant that takes a log type, used by the assert macros
  110. #define PanicYesNoFmtAssert(log_type, format, ...) \
  111. GenericAlertFmt(true, Common::MsgType::Warning, log_type, format __VA_OPT__(, ) __VA_ARGS__)