mirror of https://github.com/OpenRCT2/OpenRCT2.git
improve assertion messages
This commit is contained in:
parent
601f344b40
commit
d25baf7bd3
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,34 +15,75 @@
|
|||
#pragma endregion
|
||||
|
||||
#include <cassert>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <windows.h>
|
||||
|
||||
#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
|
||||
|
|
|
@ -16,23 +16,35 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
/**
|
||||
* 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<typename T>
|
||||
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<typename T>
|
||||
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__
|
||||
|
|
Loading…
Reference in New Issue