TrinityCore
Loading...
Searching...
No Matches
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 <thread>
21#include <cstdarg>
22#include <cstdio>
23#include <cstring>
24
36#if TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT
37#define Unreachable() (__assume(false))
38#else
39#define Unreachable() (__builtin_unreachable())
40#endif
41
42#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
43#include <Windows.h>
44#include <intrin.h>
45#define Crash(message) \
46 ULONG_PTR execeptionArgs[] = { reinterpret_cast<ULONG_PTR>(strdup(message)), reinterpret_cast<ULONG_PTR>(_ReturnAddress()) }; \
47 RaiseException(EXCEPTION_ASSERTION_FAILURE, 0, 2, execeptionArgs); \
48 Unreachable()
49#else
50// should be easily accessible in gdb
51extern "C" { TC_COMMON_API char const* TrinityAssertionFailedMessage = nullptr; }
52#define Crash(message) \
53 TrinityAssertionFailedMessage = strdup(message); \
54 *((volatile int*)nullptr) = 0; \
55 Unreachable()
56#endif
57
58namespace
59{
60 void FormatAssertionMessageTo(std::string& formatted, char const* format, va_list args) noexcept
61 {
62 va_list len;
63
64 va_copy(len, args);
65 int32 length = vsnprintf(nullptr, 0, format, len);
66 va_end(len);
67
68 std::size_t offset = formatted.length();
69 formatted.resize(offset + length);
70 vsnprintf(&formatted[offset], length + 1, format, args);
71 }
72}
73
74namespace Trinity
75{
76void Assert(char const* file, int line, char const* function, char const* message, std::string debugInfo) noexcept
77{
78 std::string formattedMessage = StringFormat("\n{}:{} in {} ASSERTION FAILED:\n {}\n{}\n", file, line, function, message, debugInfo);
79 fprintf(stderr, "%s", formattedMessage.c_str());
80 fflush(stderr);
81 Crash(formattedMessage.c_str());
82}
83
84void Assert(char const* file, int line, char const* function, char const* message, std::string debugInfo, char const* format, ...) noexcept
85{
86 va_list args;
87 va_start(args, format);
88
89 std::string formattedMessage = StringFormat("\n{}:{} in {} ASSERTION FAILED:\n {}\n", file, line, function, message);
90 FormatAssertionMessageTo(formattedMessage, format, args);
91 va_end(args);
92
93 formattedMessage.append(1, '\n');
94 formattedMessage.append(debugInfo);
95 formattedMessage.append(1, '\n');
96
97 fprintf(stderr, "%s", formattedMessage.c_str());
98 fflush(stderr);
99
100 Crash(formattedMessage.c_str());
101}
102
103void Fatal(char const* file, int line, char const* function, char const* message, ...) noexcept
104{
105 va_list args;
106 va_start(args, message);
107
108 std::string formattedMessage = StringFormat("\n{}:{} in {} FATAL ERROR:\n", file, line, function);
109 FormatAssertionMessageTo(formattedMessage, message, args);
110 va_end(args);
111
112 formattedMessage.append(1, '\n');
113
114 fprintf(stderr, "%s", formattedMessage.c_str());
115 fflush(stderr);
116
117 std::this_thread::sleep_for(std::chrono::seconds(10));
118 Crash(formattedMessage.c_str());
119}
120
121void Error(char const* file, int line, char const* function, char const* message) noexcept
122{
123 std::string formattedMessage = StringFormat("\n{}:{} in {} ERROR:\n {}\n", file, line, function, message);
124 fprintf(stderr, "%s", formattedMessage.c_str());
125 fflush(stderr);
126 Crash(formattedMessage.c_str());
127}
128
129void Warning(char const* file, int line, char const* function, char const* message) noexcept
130{
131 fprintf(stderr, "\n%s:%i in %s WARNING:\n %s\n",
132 file, line, function, message);
133}
134
135void Abort(char const* file, int line, char const* function) noexcept
136{
137 std::string formattedMessage = StringFormat("\n{}:{} in {} ABORTED.\n", file, line, function);
138 fprintf(stderr, "%s", formattedMessage.c_str());
139 fflush(stderr);
140 Crash(formattedMessage.c_str());
141}
142
143void Abort(char const* file, int line, char const* function, char const* message, ...) noexcept
144{
145 va_list args;
146 va_start(args, message);
147
148 std::string formattedMessage = StringFormat("\n{}:{} in {} ABORTED:\n", file, line, function);
149 FormatAssertionMessageTo(formattedMessage, message, args);
150 va_end(args);
151
152 formattedMessage.append(1, '\n');
153
154 fprintf(stderr, "%s", formattedMessage.c_str());
155 fflush(stderr);
156
157 Crash(formattedMessage.c_str());
158}
159
160void AbortHandler(int sigval) noexcept
161{
162 // nothing useful to log here, no way to pass args
163 std::string formattedMessage = StringFormat("Caught signal {}\n", sigval);
164 fprintf(stderr, "%s", formattedMessage.c_str());
165 fflush(stderr);
166 Crash(formattedMessage.c_str());
167}
168} // namespace Trinity
169
170std::string GetDebugInfo()
171{
172 return "";
173}
#define TC_COMMON_API
Definition Define.h:99
int32_t int32
Definition Define.h:150
std::string GetDebugInfo()
Definition Errors.cpp:170
#define Crash(message)
Definition Errors.cpp:45
void Assert(char const *file, int line, char const *function, char const *message, std::string debugInfo) noexcept
Definition Errors.cpp:76
void Error(char const *file, int line, char const *function, char const *message) noexcept
Definition Errors.cpp:121
std::string StringFormat(FormatString< Args... > fmt, Args &&... args) noexcept
Default TC string format function.
void Abort(char const *file, int line, char const *function) noexcept
Definition Errors.cpp:135
void Fatal(char const *file, int line, char const *function, char const *message,...) noexcept
Definition Errors.cpp:103
void AbortHandler(int sigval) noexcept
Definition Errors.cpp:160