From d25baf7bd3c53a89ddfe0b9a2992cf1967e18187 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 16 Jul 2016 14:12:16 +0100 Subject: [PATCH] improve assertion messages --- src/core/Console.cpp | 8 ++++++-- src/core/Console.hpp | 1 + src/core/Guard.cpp | 49 ++++++++++++++++++++++++++++++++++++++++---- src/core/Guard.hpp | 24 ++++++++++++++++------ 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/core/Console.cpp b/src/core/Console.cpp index d82064022d..5c75d249a0 100644 --- a/src/core/Console.cpp +++ b/src/core/Console.cpp @@ -94,11 +94,15 @@ namespace Console void WriteLine(const utf8 * format, ...) { va_list args; - va_start(args, format); + WriteLine_VA(format, args); + va_end(args); + } + + void WriteLine_VA(const utf8 * format, va_list args) + { vfprintf(stdout, format, args); puts(""); - va_end(args); } } } \ No newline at end of file diff --git a/src/core/Console.hpp b/src/core/Console.hpp index 4f8171235b..9b1d82f310 100644 --- a/src/core/Console.hpp +++ b/src/core/Console.hpp @@ -37,5 +37,6 @@ namespace Console void WriteFormat(const utf8 * format, ...); void WriteLine(); void WriteLine(const utf8 * format, ...); + void WriteLine_VA(const utf8 * format, va_list args); } } diff --git a/src/core/Guard.cpp b/src/core/Guard.cpp index f88d871846..1e2e336dfc 100644 --- a/src/core/Guard.cpp +++ b/src/core/Guard.cpp @@ -15,34 +15,75 @@ #pragma endregion #include +#include #include +#include #include "Console.hpp" #include "Diagnostics.hpp" #include "Guard.hpp" +extern "C" +{ + #include "../openrct2.h" +} + namespace Guard { - void Assert(bool expression, const char * message) + void Assert(bool expression, const char * message, ...) + { + va_list args; + va_start(args, message); + Assert_VA(expression, message, args); + va_end(args); + } + + void Assert_VA(bool expression, const char * message, va_list args) { if (expression) return; if (message != nullptr) { - Console::Error::WriteLine(message); + Console::Error::WriteLine("Assertion failed:"); + Console::Error::WriteLine_VA(message, args); } #if DEBUG Debug::Break(); #endif +#if __WINDOWS__ + char version[128]; + openrct2_write_full_version_info(version, sizeof(version)); + + char buffer[512]; + strcpy(buffer, "An assertion failed, please report this to the OpenRCT2 developers.\r\n\r\nVersion: "); + strcat(buffer, version); + strcat(buffer, "\r\n"); + vsprintf((char *)strchr(buffer, 0), message, args); + int result = MessageBox(nullptr, buffer, OPENRCT2_NAME, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION); + if (result == IDABORT) + { + // Force a crash that breakpad will handle allowing us to get a dump + *((void**)0) = 0; + } +#else assert(false); +#endif } - void Fail(const char * message) + void Fail(const char * message, ...) + { + va_list args; + va_start(args, message); + Fail_VA(message, args); + va_end(args); + } + + void Fail_VA(const char * message, va_list args) { if (message != nullptr) { - Console::Error::WriteLine(message); + Console::Error::WriteLine_VA(message, args); } #if DEBUG diff --git a/src/core/Guard.hpp b/src/core/Guard.hpp index c4ba8cdefa..683514e4dd 100644 --- a/src/core/Guard.hpp +++ b/src/core/Guard.hpp @@ -16,23 +16,35 @@ #pragma once +#include + /** * Utility methods for asserting function parameters. */ namespace Guard { - void Assert(bool expression, const char * message = nullptr); - void Fail(const char * message = nullptr); + void Assert(bool expression, const char * message = nullptr, ...); + void Assert_VA(bool expression, const char * message, va_list args); + void Fail(const char * message = nullptr, ...); + void Fail_VA(const char * message, va_list args); template - void ArgumentNotNull(T * argument, const char * message = nullptr) + void ArgumentNotNull(T * argument, const char * message = nullptr, ...) { - Assert(argument != nullptr, message); + va_list args; + va_start(args, message); + Assert_VA(argument != nullptr, message, args); + va_end(args); } template - void ArgumentInRange(T argument, T min, T max, const char * message = nullptr) + void ArgumentInRange(T argument, T min, T max, const char * message = nullptr, ...) { - Assert(argument >= min && argument <= max, message); + va_list args; + va_start(args, message); + Assert(argument >= min && argument <= max, message, args); + va_end(args); } }; + +#define GUARD_LINE "Location: %s:%d", __func__, __LINE__