Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
168 строк · 5.3 Кб
1
/*
2
* This file is part of the Pandaria 5.4.8 Project. See THANKS 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

25
/**
26
    @file Errors.cpp
27

28
    @brief This file contains definitions of functions used for reporting critical application errors
29

30
    It is very important that (std::)abort is NEVER called in place of *((volatile int*)NULL) = 0;
31
    Calling abort() on Windows does not invoke unhandled exception filters - a mechanism used by WheatyExceptionReport
32
    to log crashes. exit(1) calls here are for static analysis tools to indicate that calling functions defined in this file
33
    terminates the application.
34
 */
35

36
#ifdef _WIN32
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
43
extern "C" { char const* TrinityAssertionFailedMessage = nullptr; }
44
#define Crash(message) \
45
    TrinityAssertionFailedMessage = strdup(message); \
46
    *((volatile int*)nullptr) = 0; \
47
    exit(1);
48
#endif
49

50
namespace
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

68
namespace Trinity {
69

70
void Assert(char const* file, int line, char const* function, std::string debugInfo, char const* message)
71
{
72
    std::string formattedMessage = StringFormat("\n%s:%i in %s ASSERTION FAILED:\n  %s\n", file, line, function, message) + debugInfo + '\n';
73
    fprintf(stderr, "%s", formattedMessage.c_str());
74
    fflush(stderr);
75
    Crash(formattedMessage.c_str());
76
}
77

78
void Assert(char const* file, int line, char const* function, std::string debugInfo, char const* message, char const* format, ...)
79
{
80
    va_list args;
81
    va_start(args, format);
82

83
    std::string formattedMessage = StringFormat("\n%s:%i in %s ASSERTION FAILED:\n  %s\n", file, line, function, message) + FormatAssertionMessage(format, args) + '\n' + debugInfo + '\n';
84
    va_end(args);
85

86
    fprintf(stderr, "%s", formattedMessage.c_str());
87
    fflush(stderr);
88

89
    Crash(formattedMessage.c_str());
90
}
91

92
// void Fatal(char const* file, int line, char const* function, char const* message)
93
// {
94
//     fprintf(stderr, "\n%s:%i in %s FATAL ERROR:\n  %s\n",
95
//                    file, line, function, message);
96
//     std::abort();
97
// }
98

99
void Fatal(char const* file, int line, char const* function, char const* message, ...)
100
{
101
    va_list args;
102
    va_start(args, message);
103

104
    std::string formattedMessage = StringFormat("\n%s:%i in %s FATAL ERROR:\n", file, line, function) + FormatAssertionMessage(message, args) + '\n';
105
    va_end(args);
106

107
    fprintf(stderr, "%s", formattedMessage.c_str());
108
    fflush(stderr);
109

110
    std::this_thread::sleep_for(std::chrono::seconds(10));
111
    Crash(formattedMessage.c_str());
112
}
113

114
void Error(char const* file, int line, char const* function, char const* message)
115
{
116
    fprintf(stderr, "\n%s:%i in %s ERROR:\n  %s\n",
117
                   file, line, function, message);
118
    std::abort();
119
}
120

121
void Warning(char const* file, int line, char const* function, char const* message)
122
{
123
    fprintf(stderr, "\n%s:%i in %s WARNING:\n  %s\n",
124
                   file, line, function, message);
125
}
126

127
void Abort(char const* file, int line, char const* function)
128
{
129
    std::string formattedMessage = StringFormat("\n%s:%i in %s ABORTED.\n", file, line, function);
130
    fprintf(stderr, "%s", formattedMessage.c_str());
131
    fflush(stderr);
132
    Crash(formattedMessage.c_str());
133
}
134

135
void Abort(char const* file, int line, char const* function, char const* message, ...)
136
{
137
    va_list args;
138
    va_start(args, message);
139

140
    std::string formattedMessage = StringFormat("\n%s:%i in %s ABORTED:\n", file, line, function) + FormatAssertionMessage(message, args) + '\n';
141
    va_end(args);
142

143
    fprintf(stderr, "%s", formattedMessage.c_str());
144
    fflush(stderr);
145

146
    Crash(formattedMessage.c_str());
147
}
148

149
void AbortHandler(int sigval)
150
{
151
    // nothing useful to log here, no way to pass args
152
    std::string formattedMessage = StringFormat("Caught signal %i\n", sigval);
153
    fprintf(stderr, "%s", formattedMessage.c_str());
154
    fflush(stderr);
155
    Crash(formattedMessage.c_str());
156
}
157

158
} // namespace Trinity
159

160
void LogNotImplementedCall(char const* func)
161
{
162
    fprintf(stderr, "Call of not implemented function: %s", func);
163
}
164

165
std::string GetDebugInfo()
166
{
167
    return "";
168
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.