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