diff --git a/src/openttd.cpp b/src/openttd.cpp index 58f011b082..cc382bdbf0 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -123,6 +123,25 @@ void CDECL error(const char *s, ...) abort(); } +void CDECL assert_msg_error(int line, const char *file, const char *expr, const char *str, ...) +{ + va_list va; + char buf[2048]; + + char *b = buf; + b += seprintf(b, lastof(buf), "Assertion failed at line %i of %s: %s\n\t", line, file, expr); + + va_start(va, str); + vseprintf(b, lastof(buf), str, va); + va_end(va); + + ShowOSErrorBox(buf, true); + + /* Set the error message for the crash log and then invoke it. */ + CrashLog::SetErrorMessage(buf); + abort(); +} + /** * Shows some information on the console/a popup box depending on the OS. * @param str the text to show. diff --git a/src/stdafx.h b/src/stdafx.h index 4ca0a12c30..cf9fe92f99 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -433,6 +433,7 @@ assert_compile(SIZE_MAX >= UINT32_MAX); void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2); void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2); +void NORETURN CDECL assert_msg_error(int line, const char *file, const char *expr, const char *str, ...) WARN_FORMAT(4, 5); #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__) /* For non-debug builds with assertions enabled use the special assertion handler: @@ -447,6 +448,9 @@ void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2); /* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */ #if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT)) #define OTTD_ASSERT + #define assert_msg(expression, ...) if (!(expression)) assert_msg_error(__LINE__, __FILE__, #expression, __VA_ARGS__); +#else + #define assert_msg(expression, ...) #endif #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)