1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 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 Affero 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/>.
*/
#include "Errors.h"
#include "Duration.h"
#include <cstdio>
#include <cstdlib>
#include <thread>
/**
@file Errors.cpp
@brief This file contains definitions of functions used for reporting critical application errors
It is very important that (std::)abort is NEVER called in place of *((volatile int*)nullptr) = 0;
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.
*/
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
#include <Windows.h>
#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
extern "C" { char const* AcoreAssertionFailedMessage = nullptr; }
#define Crash(message) \
AcoreAssertionFailedMessage = strdup(message); \
*((volatile int*)nullptr) = 0; \
exit(1);
#endif
namespace
{
/**
* @name MakeMessage
* @brief Make message for display erros
* @param messageType Message type (ASSERTION FAILED, FATAL ERROR, ERROR) end etc
* @param file Path to file
* @param line Line number in file
* @param function Functionn name
* @param message Condition to string format
* @param fmtMessage [optional] Display format message after condition
* @param debugInfo [optional] Display debug info
*/
inline std::string MakeMessage(std::string_view messageType, std::string_view file, uint32 line, std::string_view function,
std::string_view message, std::string_view fmtMessage = {}, std::string_view debugInfo = {})
{
std::string msg = Acore::StringFormat("\n>> {}\n\n# Location: {}:{}\n# Function: {}\n# Condition: {}\n", messageType, file, line, function, message);
if (!fmtMessage.empty())
{
msg.append(Acore::StringFormat("# Message: {}\n", fmtMessage));
}
if (!debugInfo.empty())
{
msg.append(Acore::StringFormat("\n# Debug info: {}\n", debugInfo));
}
return Acore::StringFormat(
"#{0:-^{2}}#\n"
" {1: ^{2}} \n"
"#{0:-^{2}}#\n", "", msg, 70);
}
/**
* @name MakeAbortMessage
* @brief Make message for display erros
* @param file Path to file
* @param line Line number in file
* @param function Functionn name
* @param fmtMessage [optional] Display format message after condition
*/
inline std::string MakeAbortMessage(std::string_view file, uint32 line, std::string_view function, std::string_view fmtMessage = {})
{
std::string msg = Acore::StringFormat("\n>> ABORTED\n\n# Location '{}:{}'\n# Function '{}'\n", file, line, function);
if (!fmtMessage.empty())
{
msg.append(Acore::StringFormat("# Message '{}'\n", fmtMessage));
}
return Acore::StringFormat(
"\n#{0:-^{2}}#\n"
" {1: ^{2}} \n"
"#{0:-^{2}}#\n", "", msg, 70);
}
}
void Acore::Assert(std::string_view file, uint32 line, std::string_view function, std::string_view debugInfo, std::string_view message, std::string_view fmtMessage /*= {}*/)
{
std::string formattedMessage = MakeMessage("ASSERTION FAILED", file, line, function, message, fmtMessage, debugInfo);
fmt::print(stderr, "{}", formattedMessage);
fflush(stderr);
Crash(formattedMessage.c_str());
}
void Acore::Fatal(std::string_view file, uint32 line, std::string_view function, std::string_view message, std::string_view fmtMessage /*= {}*/)
{
std::string formattedMessage = MakeMessage("FATAL ERROR", file, line, function, message, fmtMessage);
fmt::print(stderr, "{}", formattedMessage);
fflush(stderr);
std::this_thread::sleep_for(10s);
Crash(formattedMessage.c_str());
}
void Acore::Error(std::string_view file, uint32 line, std::string_view function, std::string_view message)
{
std::string formattedMessage = MakeMessage("ERROR", file, line, function, message);
fmt::print(stderr, "{}", formattedMessage);
fflush(stderr);
std::this_thread::sleep_for(10s);
Crash(formattedMessage.c_str());
}
void Acore::Warning(std::string_view file, uint32 line, std::string_view function, std::string_view message)
{
std::string formattedMessage = MakeMessage("WARNING", file, line, function, message);
fmt::print(stderr, "{}", formattedMessage);
}
void Acore::Abort(std::string_view file, uint32 line, std::string_view function, std::string_view fmtMessage /*= {}*/)
{
std::string formattedMessage = MakeAbortMessage(file, line, function, fmtMessage);
fmt::print(stderr, "{}", formattedMessage);
fflush(stderr);
std::this_thread::sleep_for(10s);
Crash(formattedMessage.c_str());
}
void Acore::AbortHandler(int sigval)
{
// nothing useful to log here, no way to pass args
std::string formattedMessage = StringFormat("Caught signal {}\n", sigval);
fmt::print(stderr, "{}", formattedMessage);
fflush(stderr);
Crash(formattedMessage.c_str());
}
std::string GetDebugInfo()
{
return "";
}
|