2013-03-26 09:44:01 +01:00
|
|
|
/*
|
2020-01-02 06:44:10 +01:00
|
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
2013-03-26 09:44:01 +01:00
|
|
|
*
|
|
|
|
|
* This program 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 2 of the License, or (at your
|
|
|
|
|
* option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-03-25 13:26:48 +01:00
|
|
|
#include "Errors.h"
|
2018-08-29 23:59:24 +02:00
|
|
|
#include "StringFormat.h"
|
2014-07-20 16:06:35 +02:00
|
|
|
#include <cstdio>
|
2013-05-16 01:10:53 +01:00
|
|
|
#include <cstdlib>
|
2014-07-01 00:54:09 +02:00
|
|
|
#include <thread>
|
2014-09-26 20:47:48 +02:00
|
|
|
#include <cstdarg>
|
2013-03-25 13:26:48 +01:00
|
|
|
|
2015-09-21 17:18:55 +02:00
|
|
|
/**
|
|
|
|
|
@file Errors.cpp
|
|
|
|
|
|
|
|
|
|
@brief This file contains definitions of functions used for reporting critical application errors
|
|
|
|
|
|
2020-08-14 17:06:03 +02:00
|
|
|
It is very important that (std::)abort is NEVER called in place of *((volatile int*)nullptr) = 0;
|
2015-09-21 17:18:55 +02:00
|
|
|
Calling abort() on Windows does not invoke unhandled exception filters - a mechanism used by WheatyExceptionReport
|
|
|
|
|
to log crashes. exit(1) calls here are for static analysis tools to indicate that calling functions defined in this file
|
|
|
|
|
terminates the application.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-08-29 23:59:24 +02:00
|
|
|
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
2018-09-27 14:42:42 +02:00
|
|
|
#include <Windows.h>
|
2018-08-29 23:59:24 +02:00
|
|
|
#define Crash(message) \
|
|
|
|
|
ULONG_PTR execeptionArgs[] = { reinterpret_cast<ULONG_PTR>(strdup(message)), reinterpret_cast<ULONG_PTR>(_ReturnAddress()) }; \
|
|
|
|
|
RaiseException(EXCEPTION_ASSERTION_FAILURE, 0, 2, execeptionArgs);
|
|
|
|
|
#else
|
|
|
|
|
// should be easily accessible in gdb
|
2021-10-31 00:18:32 +02:00
|
|
|
extern "C" { TC_COMMON_API char const* TrinityAssertionFailedMessage = nullptr; }
|
2018-08-29 23:59:24 +02:00
|
|
|
#define Crash(message) \
|
|
|
|
|
TrinityAssertionFailedMessage = strdup(message); \
|
|
|
|
|
*((volatile int*)nullptr) = 0; \
|
|
|
|
|
exit(1);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
std::string FormatAssertionMessage(char const* format, va_list args)
|
|
|
|
|
{
|
|
|
|
|
std::string formatted;
|
|
|
|
|
va_list len;
|
|
|
|
|
|
|
|
|
|
va_copy(len, args);
|
|
|
|
|
int32 length = vsnprintf(nullptr, 0, format, len);
|
|
|
|
|
va_end(len);
|
|
|
|
|
|
2018-12-14 22:01:16 +01:00
|
|
|
formatted.resize(length);
|
2018-12-16 12:09:20 +01:00
|
|
|
vsnprintf(&formatted[0], length + 1, format, args);
|
2018-08-29 23:59:24 +02:00
|
|
|
|
|
|
|
|
return formatted;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Trinity
|
|
|
|
|
{
|
2013-03-25 13:26:48 +01:00
|
|
|
|
2018-12-14 22:01:16 +01:00
|
|
|
void Assert(char const* file, int line, char const* function, std::string debugInfo, char const* message)
|
2013-03-25 13:26:48 +01:00
|
|
|
{
|
2018-12-14 22:01:16 +01:00
|
|
|
std::string formattedMessage = StringFormat("\n%s:%i in %s ASSERTION FAILED:\n %s\n", file, line, function, message) + debugInfo + '\n';
|
2018-08-29 23:59:24 +02:00
|
|
|
fprintf(stderr, "%s", formattedMessage.c_str());
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
Crash(formattedMessage.c_str());
|
2013-03-25 13:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-14 22:01:16 +01:00
|
|
|
void Assert(char const* file, int line, char const* function, std::string debugInfo, char const* message, char const* format, ...)
|
2014-09-26 20:47:48 +02:00
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
|
2018-12-14 22:01:16 +01:00
|
|
|
std::string formattedMessage = StringFormat("\n%s:%i in %s ASSERTION FAILED:\n %s\n", file, line, function, message) + FormatAssertionMessage(format, args) + '\n' + debugInfo + '\n';
|
2018-08-29 23:59:24 +02:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "%s", formattedMessage.c_str());
|
2014-09-26 20:47:48 +02:00
|
|
|
fflush(stderr);
|
|
|
|
|
|
2018-08-29 23:59:24 +02:00
|
|
|
Crash(formattedMessage.c_str());
|
2014-09-26 20:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
2015-11-13 22:43:36 +01:00
|
|
|
void Fatal(char const* file, int line, char const* function, char const* message, ...)
|
2013-03-25 13:26:48 +01:00
|
|
|
{
|
2015-11-13 22:43:36 +01:00
|
|
|
va_list args;
|
|
|
|
|
va_start(args, message);
|
|
|
|
|
|
2018-10-27 22:14:37 +02:00
|
|
|
std::string formattedMessage = StringFormat("\n%s:%i in %s FATAL ERROR:\n", file, line, function) + FormatAssertionMessage(message, args) + '\n';
|
2018-08-29 23:59:24 +02:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "%s", formattedMessage.c_str());
|
2015-11-13 22:43:36 +01:00
|
|
|
fflush(stderr);
|
2014-07-01 00:54:09 +02:00
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(10));
|
2018-08-29 23:59:24 +02:00
|
|
|
Crash(formattedMessage.c_str());
|
2013-03-25 13:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
2013-05-21 08:37:28 +02:00
|
|
|
void Error(char const* file, int line, char const* function, char const* message)
|
2013-03-25 13:26:48 +01:00
|
|
|
{
|
2018-08-29 23:59:24 +02:00
|
|
|
std::string formattedMessage = StringFormat("\n%s:%i in %s ERROR:\n %s\n", file, line, function, message);
|
|
|
|
|
fprintf(stderr, "%s", formattedMessage.c_str());
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
Crash(formattedMessage.c_str());
|
2013-03-25 13:26:48 +01:00
|
|
|
}
|
|
|
|
|
|
2013-05-21 08:37:28 +02:00
|
|
|
void Warning(char const* file, int line, char const* function, char const* message)
|
2013-03-25 13:26:48 +01:00
|
|
|
{
|
2013-03-26 09:44:01 +01:00
|
|
|
fprintf(stderr, "\n%s:%i in %s WARNING:\n %s\n",
|
2013-03-25 13:26:48 +01:00
|
|
|
file, line, function, message);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-21 15:17:05 +02:00
|
|
|
void Abort(char const* file, int line, char const* function)
|
|
|
|
|
{
|
2018-08-29 23:59:24 +02:00
|
|
|
std::string formattedMessage = StringFormat("\n%s:%i in %s ABORTED.\n", file, line, function);
|
|
|
|
|
fprintf(stderr, "%s", formattedMessage.c_str());
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
Crash(formattedMessage.c_str());
|
2015-09-21 15:17:05 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-09 20:06:13 +01:00
|
|
|
void Abort(char const* file, int line, char const* function, char const* message, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, message);
|
|
|
|
|
|
|
|
|
|
std::string formattedMessage = StringFormat("\n%s:%i in %s ABORTED:\n", file, line, function) + FormatAssertionMessage(message, args) + '\n';
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "%s", formattedMessage.c_str());
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
|
|
Crash(formattedMessage.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 23:59:24 +02:00
|
|
|
void AbortHandler(int sigval)
|
2016-03-03 17:29:48 +01:00
|
|
|
{
|
|
|
|
|
// nothing useful to log here, no way to pass args
|
2018-08-29 23:59:24 +02:00
|
|
|
std::string formattedMessage = StringFormat("Caught signal %i\n", sigval);
|
|
|
|
|
fprintf(stderr, "%s", formattedMessage.c_str());
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
Crash(formattedMessage.c_str());
|
2016-03-03 17:29:48 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-25 13:26:48 +01:00
|
|
|
} // namespace Trinity
|
2018-12-14 22:01:16 +01:00
|
|
|
|
|
|
|
|
std::string GetDebugInfo()
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|