Assert: Add more assert macros for using a simple string as the extra text

This commit is contained in:
Jonathan G Rennison
2024-01-08 19:22:38 +00:00
parent 61db988a4a
commit 5e2ac22ef5
2 changed files with 16 additions and 6 deletions

View File

@@ -227,13 +227,19 @@ void CDECL assert_msg_error(int line, const char *file, const char *expr, const
vseprintf(b, lastof(buf), str, va); vseprintf(b, lastof(buf), str, va);
va_end(va); va_end(va);
if (VideoDriver::GetInstance() == nullptr || VideoDriver::GetInstance()->HasGUI()) { fatalerror_common(buf);
ShowOSErrorBox(buf, true); }
}
/* Set the error message for the crash log and then invoke it. */ void assert_str_error(int line, const char *file, const char *expr, const char *str)
CrashLog::SetErrorMessage(buf); {
DoOSAbort(); char buf[2048];
seprintf(buf, lastof(buf), "Assertion failed at line %i of %s: %s\n%s", line, file, expr, str);
fatalerror_common(buf);
}
void assert_str_error(int line, const char *file, const char *expr, const std::string &str)
{
assert_str_error(line, file, expr, str.c_str());
} }
const char *assert_tile_info(uint32_t tile) { const char *assert_tile_info(uint32_t tile) {

View File

@@ -433,6 +433,8 @@ typedef uint64_t unaligned_uint64;
void NORETURN CDECL usererror(const char *str, ...) WARN_FORMAT(1, 2); 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 error(const char *str, ...) WARN_FORMAT(1, 2);
void NORETURN CDECL assert_msg_error(int line, const char *file, const char *expr, const char *extra, const char *str, ...) WARN_FORMAT(5, 6); void NORETURN CDECL assert_msg_error(int line, const char *file, const char *expr, const char *extra, const char *str, ...) WARN_FORMAT(5, 6);
void NORETURN assert_str_error(int line, const char *file, const char *expr, const char *str);
void NORETURN assert_str_error(int line, const char *file, const char *expr, const std::string &str);
const char *assert_tile_info(uint32_t tile); const char *assert_tile_info(uint32_t tile);
#define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__) #define NOT_REACHED() error("NOT_REACHED triggered at line %i of %s", __LINE__, __FILE__)
@@ -443,12 +445,14 @@ const char *assert_tile_info(uint32_t tile);
# define assert_msg(expression, ...) if (unlikely(!(expression))) assert_msg_error(__LINE__, __FILE__, #expression, nullptr, __VA_ARGS__); # define assert_msg(expression, ...) if (unlikely(!(expression))) assert_msg_error(__LINE__, __FILE__, #expression, nullptr, __VA_ARGS__);
# define assert_msg_tile(expression, tile, ...) if (unlikely(!(expression))) assert_msg_error(__LINE__, __FILE__, #expression, assert_tile_info(tile), __VA_ARGS__); # define assert_msg_tile(expression, tile, ...) if (unlikely(!(expression))) assert_msg_error(__LINE__, __FILE__, #expression, assert_tile_info(tile), __VA_ARGS__);
# define assert_tile(expression, tile) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s\n\t%s", __LINE__, __FILE__, #expression, assert_tile_info(tile)); # define assert_tile(expression, tile) if (unlikely(!(expression))) error("Assertion failed at line %i of %s: %s\n\t%s", __LINE__, __FILE__, #expression, assert_tile_info(tile));
# define assert_str(expression, str) if (unlikely(!(expression))) assert_str_error(__LINE__, __FILE__, #expression, str);
#else #else
# undef assert # undef assert
# define assert(expression) # define assert(expression)
# define assert_msg(expression, ...) # define assert_msg(expression, ...)
# define assert_msg_tile(expression, tile, ...) # define assert_msg_tile(expression, tile, ...)
# define assert_tile(expression, tile) # define assert_tile(expression, tile)
# define assert_str(expression, str)
#endif #endif
#if (!defined(NDEBUG) || defined(WITH_ASSERT)) && !defined(FEWER_ASSERTS) #if (!defined(NDEBUG) || defined(WITH_ASSERT)) && !defined(FEWER_ASSERTS)
# define WITH_FULL_ASSERTS # define WITH_FULL_ASSERTS