TrinityCore
Errors.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "Errors.h"
19#include "StringFormat.h"
20#include <cstdio>
21#include <cstdlib>
22#include <thread>
23#include <cstdarg>
24
36#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
37#include <Windows.h>
38#include <intrin.h>
39#define Crash(message) \
40 ULONG_PTR execeptionArgs[] = { reinterpret_cast<ULONG_PTR>(strdup(message)), reinterpret_cast<ULONG_PTR>(_ReturnAddress()) }; \
41 RaiseException(EXCEPTION_ASSERTION_FAILURE, 0, 2, execeptionArgs);
42#else
43#include <cstring>
44// should be easily accessible in gdb
45extern "C" { TC_COMMON_API char const* TrinityAssertionFailedMessage = nullptr; }
46#define Crash(message) \
47 TrinityAssertionFailedMessage = strdup(message); \
48 *((volatile int*)nullptr) = 0; \
49 exit(1);
50#endif
51
52namespace
53{
54 std::string FormatAssertionMessage(char const* format, va_list args)
55 {
56 std::string formatted;
57 va_list len;
58
59 va_copy(len, args);
60 int32 length = vsnprintf(nullptr, 0, format, len);
61 va_end(len);
62
63 formatted.resize(length);
64 vsnprintf(&formatted[0], length + 1, format, args);
65
66 return formatted;
67 }
68}
69
70namespace Trinity
71{
72
73void Assert(char const* file, int line, char const* function, std::string debugInfo, char const* message)
74{
75 std::string formattedMessage = StringFormat("\n{}:{} in {} ASSERTION FAILED:\n {}\n", file, line, function, message) + debugInfo + '\n';
76 fprintf(stderr, "%s", formattedMessage.c_str());
77 fflush(stderr);
78 Crash(formattedMessage.c_str());
79}
80
81void Assert(char const* file, int line, char const* function, std::string debugInfo, char const* message, char const* format, ...)
82{
83 va_list args;
84 va_start(args, format);
85
86 std::string formattedMessage = StringFormat("\n{}:{} in {} ASSERTION FAILED:\n {}\n", file, line, function, message) + FormatAssertionMessage(format, args) + '\n' + debugInfo + '\n';
87 va_end(args);
88
89 fprintf(stderr, "%s", formattedMessage.c_str());
90 fflush(stderr);
91
92 Crash(formattedMessage.c_str());
93}
94
95void Fatal(char const* file, int line, char const* function, char const* message, ...)
96{
97 va_list args;
98 va_start(args, message);
99
100 std::string formattedMessage = StringFormat("\n{}:{} in {} FATAL ERROR:\n", file, line, function) + FormatAssertionMessage(message, args) + '\n';
101 va_end(args);
102
103 fprintf(stderr, "%s", formattedMessage.c_str());
104 fflush(stderr);
105
106 std::this_thread::sleep_for(std::chrono::seconds(10));
107 Crash(formattedMessage.c_str());
108}
109
110void Error(char const* file, int line, char const* function, char const* message)
111{
112 std::string formattedMessage = StringFormat("\n{}:{} in {} ERROR:\n {}\n", file, line, function, message);
113 fprintf(stderr, "%s", formattedMessage.c_str());
114 fflush(stderr);
115 Crash(formattedMessage.c_str());
116}
117
118void Warning(char const* file, int line, char const* function, char const* message)
119{
120 fprintf(stderr, "\n%s:%i in %s WARNING:\n %s\n",
121 file, line, function, message);
122}
123
124void Abort(char const* file, int line, char const* function)
125{
126 std::string formattedMessage = StringFormat("\n{}:{} in {} ABORTED.\n", file, line, function);
127 fprintf(stderr, "%s", formattedMessage.c_str());
128 fflush(stderr);
129 Crash(formattedMessage.c_str());
130}
131
132void Abort(char const* file, int line, char const* function, char const* message, ...)
133{
134 va_list args;
135 va_start(args, message);
136
137 std::string formattedMessage = StringFormat("\n{}:{} in {} ABORTED:\n", file, line, function) + FormatAssertionMessage(message, args) + '\n';
138 va_end(args);
139
140 fprintf(stderr, "%s", formattedMessage.c_str());
141 fflush(stderr);
142
143 Crash(formattedMessage.c_str());
144}
145
146void AbortHandler(int sigval)
147{
148 // nothing useful to log here, no way to pass args
149 std::string formattedMessage = StringFormat("Caught signal {}\n", sigval);
150 fprintf(stderr, "%s", formattedMessage.c_str());
151 fflush(stderr);
152 Crash(formattedMessage.c_str());
153}
154
155} // namespace Trinity
156
157std::string GetDebugInfo()
158{
159 return "";
160}
#define TC_COMMON_API
Definition: Define.h:99
int32_t int32
Definition: Define.h:138
std::string GetDebugInfo()
Definition: Errors.cpp:157
#define Crash(message)
Definition: Errors.cpp:39
void Assert(char const *file, int line, char const *function, std::string debugInfo, char const *message)
Definition: Errors.cpp:73
void AbortHandler(int sigval)
Definition: Errors.cpp:146
void Warning(char const *file, int line, char const *function, char const *message)
Definition: Errors.cpp:118
void Abort(char const *file, int line, char const *function)
Definition: Errors.cpp:124
void Fatal(char const *file, int line, char const *function, char const *message,...)
Definition: Errors.cpp:95
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
void Error(char const *file, int line, char const *function, char const *message)
Definition: Errors.cpp:110