Files
OpenRCT2-Web/src/openrct2/core/Guard.cpp
T

163 lines
4.4 KiB
C++
Raw Normal View History

2017-06-01 21:55:10 +02:00
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
2016-04-10 17:01:16 +01:00
#include <cassert>
2016-07-16 14:12:16 +01:00
#include <stdarg.h>
2016-04-10 17:01:16 +01:00
#include <stdio.h>
2016-07-16 14:49:41 +01:00
#include "../common.h"
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
2016-07-16 14:49:41 +01:00
#define WIN32_LEAN_AND_MEAN
2016-07-16 14:12:16 +01:00
#include <windows.h>
2016-09-08 21:37:12 +01:00
#undef GetMessage
2016-07-16 14:49:41 +01:00
#endif
2016-04-10 17:01:16 +01:00
#include "../Version.h"
2016-04-10 17:01:16 +01:00
#include "Console.hpp"
#include "Diagnostics.hpp"
#include "Guard.hpp"
2017-02-01 23:11:47 +00:00
#include "String.hpp"
2016-04-10 17:01:16 +01:00
2016-07-16 14:12:16 +01:00
extern "C"
{
void openrct2_assert_fwd(bool expression, const char * message, ...)
2016-07-17 23:54:12 +02:00
{
va_list va;
va_start(va, message);
Guard::Assert_VA(expression, message, va);
va_end(va);
2016-07-17 23:54:12 +02:00
}
2016-07-16 14:12:16 +01:00
}
2016-04-10 17:01:16 +01:00
namespace Guard
{
2017-02-01 23:16:22 +00:00
constexpr const utf8 * ASSERTION_MESSAGE = "An assertion failed, please report this to the OpenRCT2 developers.";
2017-02-01 23:11:47 +00:00
// The default behaviour when an assertion is raised.
static ASSERT_BEHAVIOUR _assertBehaviour =
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
2017-02-01 23:11:47 +00:00
ASSERT_BEHAVIOUR::MESSAGE_BOX
#else
ASSERT_BEHAVIOUR::CASSERT
#endif
;
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
2017-02-01 23:11:47 +00:00
static void GetAssertMessage(char * buffer, size_t bufferSize, const char * formattedMessage);
static void ForceCrash();
#endif
ASSERT_BEHAVIOUR GetAssertBehaviour()
{
return _assertBehaviour;
}
void SetAssertBehaviour(ASSERT_BEHAVIOUR behaviour)
{
_assertBehaviour = behaviour;
}
2016-07-16 14:12:16 +01:00
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)
2016-04-10 17:01:16 +01:00
{
if (expression) return;
2017-02-01 23:16:22 +00:00
Console::Error::WriteLine(ASSERTION_MESSAGE);
2017-04-17 21:38:35 -05:00
Console::Error::WriteLine("Version: %s", gVersionInfoFull);
2017-02-01 23:11:47 +00:00
// This is never freed, but acceptable considering we are about to crash out
utf8 * formattedMessage = nullptr;
2016-04-10 17:01:16 +01:00
if (message != nullptr)
{
2017-02-01 23:11:47 +00:00
formattedMessage = String::Format_VA(message, args);
Console::Error::WriteLine(formattedMessage);
2016-04-10 17:01:16 +01:00
}
2016-07-16 14:34:10 +01:00
#ifdef DEBUG
2016-07-11 19:09:16 +01:00
Debug::Break();
#endif
2016-07-16 14:12:16 +01:00
2017-02-01 23:11:47 +00:00
switch (_assertBehaviour) {
case ASSERT_BEHAVIOUR::ABORT:
abort();
break;
default:
case ASSERT_BEHAVIOUR::CASSERT:
2016-07-16 14:34:10 +01:00
assert(false);
2017-02-01 23:11:47 +00:00
break;
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
2017-02-01 23:11:47 +00:00
case ASSERT_BEHAVIOUR::MESSAGE_BOX:
{
// Show message box if we are not building for testing
char buffer[512];
GetAssertMessage(buffer, sizeof(buffer), formattedMessage);
sint32 result = MessageBoxA(nullptr, buffer, OPENRCT2_NAME, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION);
if (result == IDABORT)
{
ForceCrash();
}
break;
2016-07-16 14:12:16 +01:00
}
2016-12-02 17:16:26 +00:00
#endif
2017-02-01 23:11:47 +00:00
}
2016-07-11 19:09:16 +01:00
}
2016-07-16 14:12:16 +01:00
void Fail(const char * message, ...)
{
va_list args;
va_start(args, message);
2016-07-17 22:55:33 +01:00
Assert_VA(false, message, args);
2016-07-16 14:12:16 +01:00
va_end(args);
}
void Fail_VA(const char * message, va_list args)
2016-07-11 19:09:16 +01:00
{
2016-07-17 22:55:33 +01:00
Assert_VA(false, message, args);
2016-04-10 17:01:16 +01:00
}
2017-02-01 23:11:47 +00:00
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
2017-02-01 23:11:47 +00:00
static void GetAssertMessage(char * buffer, size_t bufferSize, const char * formattedMessage)
{
2017-02-01 23:16:22 +00:00
String::Set(buffer, bufferSize, ASSERTION_MESSAGE);
String::Append(buffer, bufferSize, "\r\n\r\n");
String::Append(buffer, bufferSize, "Version: ");
2017-04-17 21:38:35 -05:00
String::Append(buffer, bufferSize, gVersionInfoFull);
2017-02-01 23:11:47 +00:00
if (formattedMessage != nullptr)
{
String::Append(buffer, bufferSize, "\r\n");
String::Append(buffer, bufferSize, formattedMessage);
}
}
static void ForceCrash()
{
#ifdef USE_BREAKPAD
// Force a crash that breakpad will handle allowing us to get a dump
*((void**)0) = 0;
#else
assert(false);
#endif
}
#endif
2016-04-10 17:01:16 +01:00
}