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

153 lines
4.1 KiB
C++
Raw Normal View History

/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
2018-07-21 16:17:06 +02:00
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
2016-07-16 14:49:41 +01:00
#endif
2016-04-10 17:01:16 +01:00
#include "../Version.h"
#include "../common.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
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
void openrct2_assert_fwd(bool expression, const char* message, ...)
2016-07-16 14:12:16 +01:00
{
2018-02-01 18:49:14 +01:00
va_list va;
va_start(va, message);
Guard::Assert_VA(expression, message, va);
va_end(va);
2016-07-16 14:12:16 +01:00
}
2016-04-10 17:01:16 +01:00
namespace Guard
{
2018-06-22 22:58:39 +02:00
constexpr const utf8* ASSERTION_MESSAGE = "An assertion failed, please report this to the OpenRCT2 developers.";
2017-02-01 23:16:22 +00:00
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
2018-06-22 22:58:39 +02:00
static void GetAssertMessage(char* buffer, size_t bufferSize, const char* formattedMessage);
2017-02-01 23:11:47 +00:00
static void ForceCrash();
#endif
ASSERT_BEHAVIOUR GetAssertBehaviour()
{
return _assertBehaviour;
}
void SetAssertBehaviour(ASSERT_BEHAVIOUR behaviour)
{
_assertBehaviour = behaviour;
}
2018-06-22 22:58:39 +02:00
void Assert(bool expression, const char* message, ...)
2016-07-16 14:12:16 +01:00
{
va_list args;
va_start(args, message);
Assert_VA(expression, message, args);
va_end(args);
}
2018-06-22 22:58:39 +02:00
void Assert_VA(bool expression, const char* message, va_list args)
2016-04-10 17:01:16 +01:00
{
2018-06-22 22:58:39 +02:00
if (expression)
return;
2016-04-10 17:01:16 +01:00
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
2018-06-22 22:58:39 +02:00
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
2018-06-22 22:58:39 +02:00
switch (_assertBehaviour)
2017-02-01 23:11:47 +00:00
{
2018-06-22 22:58:39 +02:00
case ASSERT_BEHAVIOUR::ABORT:
abort();
default:
case ASSERT_BEHAVIOUR::CASSERT:
assert(false);
break;
#ifdef _WIN32
case ASSERT_BEHAVIOUR::MESSAGE_BOX:
2017-02-01 23:11:47 +00:00
{
2018-06-22 22:58:39 +02:00
// Show message box if we are not building for testing
char buffer[512];
GetAssertMessage(buffer, sizeof(buffer), formattedMessage);
int32_t result = MessageBoxA(nullptr, buffer, OPENRCT2_NAME, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION);
if (result == IDABORT)
{
ForceCrash();
}
break;
2017-02-01 23:11:47 +00: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
}
2018-06-22 22:58:39 +02:00
void Fail(const char* message, ...)
2016-07-16 14:12:16 +01:00
{
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);
}
2018-06-22 22:58:39 +02:00
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
2018-06-22 22:58:39 +02:00
static void GetAssertMessage(char* buffer, size_t bufferSize, const char* formattedMessage)
2017-02-01 23:11:47 +00:00
{
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()
{
2018-07-21 16:17:06 +02:00
# ifdef USE_BREAKPAD
2017-02-01 23:11:47 +00:00
// Force a crash that breakpad will handle allowing us to get a dump
*((void**)0) = 0;
2018-07-21 16:17:06 +02:00
# else
2017-02-01 23:11:47 +00:00
assert(false);
2018-07-21 16:17:06 +02:00
# endif
2017-02-01 23:11:47 +00:00
}
#endif
2018-05-04 22:40:09 +02:00
} // namespace Guard