From 1b7a5372ec44fa5342833fc1b0f01a04c51563e9 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Fri, 5 Jan 2024 14:47:30 +0000 Subject: [PATCH] Debug: Provide UserError, FatalError fmt macros --- src/debug_fmt.h | 6 ++++++ src/openttd.cpp | 49 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/debug_fmt.h b/src/debug_fmt.h index 9fa2b0ee86..5ad25d559f 100644 --- a/src/debug_fmt.h +++ b/src/debug_fmt.h @@ -21,4 +21,10 @@ */ #define Debug(name, level, format_string, ...) if ((level) == 0 || _debug_ ## name ## _level >= (level)) debug_print(#name, fmt::format(FMT_STRING(format_string), ## __VA_ARGS__).c_str()) +void NORETURN usererror_str(const char *msg); +void NORETURN fatalerror_str(const char *msg); + +#define UserError(format_string, ...) usererror_str(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__).c_str()) +#define FatalError(format_string, ...) fatalerror_str(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__).c_str()) + #endif /* DEBUG_FMT_H */ diff --git a/src/openttd.cpp b/src/openttd.cpp index a924281909..8cc0670b91 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -139,6 +139,22 @@ std::mutex _music_driver_mutex; static std::string _music_driver_params; static std::atomic _music_inited; +void NORETURN usererror_str(const char *msg) +{ + ShowOSErrorBox(msg, false); + if (VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop(); + +#ifdef __EMSCRIPTEN__ + emscripten_exit_pointerlock(); + /* In effect, the game ends here. As emscripten_set_main_loop() caused + * the stack to be unwound, the code after MainLoop() in + * openttd_main() is never executed. */ + EM_ASM(if (window["openttd_abort"]) openttd_abort()); +#endif + + _exit(1); +} + /** * Error handling for fatal user errors. * @param s the string to print. @@ -153,18 +169,18 @@ void CDECL usererror(const char *s, ...) vseprintf(buf, lastof(buf), s, va); va_end(va); - ShowOSErrorBox(buf, false); - if (VideoDriver::GetInstance() != nullptr) VideoDriver::GetInstance()->Stop(); + usererror_str(buf); +} -#ifdef __EMSCRIPTEN__ - emscripten_exit_pointerlock(); - /* In effect, the game ends here. As emscripten_set_main_loop() caused - * the stack to be unwound, the code after MainLoop() in - * openttd_main() is never executed. */ - EM_ASM(if (window["openttd_abort"]) openttd_abort()); -#endif +static void NORETURN fatalerror_common(const char *msg) +{ + if (VideoDriver::GetInstance() == nullptr || VideoDriver::GetInstance()->HasGUI()) { + ShowOSErrorBox(msg, true); + } - _exit(1); + /* Set the error message for the crash log and then invoke it. */ + CrashLog::SetErrorMessage(msg); + DoOSAbort(); } /** @@ -183,13 +199,14 @@ void CDECL error(const char *s, ...) vseprintf(buf, lastof(buf), s, va); va_end(va); - if (VideoDriver::GetInstance() == nullptr || VideoDriver::GetInstance()->HasGUI()) { - ShowOSErrorBox(buf, true); - } + fatalerror_common(buf); +} - /* Set the error message for the crash log and then invoke it. */ - CrashLog::SetErrorMessage(buf); - DoOSAbort(); +void fatalerror_str(const char *msg) +{ + if (CrashLog::HaveAlreadyCrashed()) DoOSAbort(); + + fatalerror_common(msg); } void CDECL assert_msg_error(int line, const char *file, const char *expr, const char *extra, const char *str, ...)