Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/error/error_macros.cpp
9903 views
1
/**************************************************************************/
2
/* error_macros.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "error_macros.h"
32
33
#include "core/io/logger.h"
34
#include "core/object/object_id.h"
35
#include "core/object/script_language.h"
36
#include "core/os/os.h"
37
#include "core/string/ustring.h"
38
39
// Optional physics interpolation warnings try to include the path to the relevant node.
40
#if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
41
#include "core/config/project_settings.h"
42
#include "scene/main/node.h"
43
#endif
44
45
static ErrorHandlerList *error_handler_list = nullptr;
46
static thread_local bool is_printing_error = false;
47
48
static void _err_print_fallback(const char *p_function, const char *p_file, int p_line, const char *p_error_details, ErrorHandlerType p_type, bool p_reentrance) {
49
if (p_reentrance) {
50
fprintf(stderr, "While attempting to print an error, another error was printed:\n");
51
}
52
53
fprintf(stderr, "%s: %s\n", _error_handler_type_string(p_type), p_error_details);
54
55
if (p_function && p_file) {
56
fprintf(stderr, " at: %s (%s:%i)\n", p_function, p_file, p_line);
57
}
58
}
59
60
void add_error_handler(ErrorHandlerList *p_handler) {
61
// If p_handler is already in error_handler_list
62
// we'd better remove it first then we can add it.
63
// This prevent cyclic redundancy.
64
remove_error_handler(p_handler);
65
66
_global_lock();
67
68
p_handler->next = error_handler_list;
69
error_handler_list = p_handler;
70
71
_global_unlock();
72
}
73
74
void remove_error_handler(const ErrorHandlerList *p_handler) {
75
_global_lock();
76
77
ErrorHandlerList *prev = nullptr;
78
ErrorHandlerList *l = error_handler_list;
79
80
while (l) {
81
if (l == p_handler) {
82
if (prev) {
83
prev->next = l->next;
84
} else {
85
error_handler_list = l->next;
86
}
87
break;
88
}
89
prev = l;
90
l = l->next;
91
}
92
93
_global_unlock();
94
}
95
96
// Errors without messages.
97
void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, bool p_editor_notify, ErrorHandlerType p_type) {
98
_err_print_error(p_function, p_file, p_line, p_error, "", p_editor_notify, p_type);
99
}
100
101
void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, bool p_editor_notify, ErrorHandlerType p_type) {
102
_err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), "", p_editor_notify, p_type);
103
}
104
105
// Main error printing function.
106
void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, bool p_editor_notify, ErrorHandlerType p_type) {
107
if (is_printing_error) {
108
// Fallback if we're already printing an error, to prevent infinite recursion.
109
const char *err_details = (p_message && *p_message) ? p_message : p_error;
110
_err_print_fallback(p_function, p_file, p_line, err_details, p_type, true);
111
return;
112
}
113
114
is_printing_error = true;
115
116
if (OS::get_singleton()) {
117
OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, p_message, p_editor_notify, (Logger::ErrorType)p_type, ScriptServer::capture_script_backtraces(false));
118
} else {
119
// Fallback if errors happen before OS init or after it's destroyed.
120
const char *err_details = (p_message && *p_message) ? p_message : p_error;
121
_err_print_fallback(p_function, p_file, p_line, err_details, p_type, false);
122
}
123
124
_global_lock();
125
126
ErrorHandlerList *l = error_handler_list;
127
while (l) {
128
l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_editor_notify, p_type);
129
l = l->next;
130
}
131
132
_global_unlock();
133
134
is_printing_error = false;
135
}
136
137
// For printing errors when we may crash at any point, so we must flush ASAP a lot of lines
138
// but we don't want to make it noisy by printing lots of file & line info (because it's already
139
// been printing by a preceding _err_print_error).
140
void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type) {
141
const char *err_details = p_error.utf8().get_data();
142
143
if (is_printing_error) {
144
// Fallback if we're already printing an error, to prevent infinite recursion.
145
_err_print_fallback(nullptr, nullptr, 0, err_details, p_type, true);
146
return;
147
}
148
149
is_printing_error = true;
150
151
if (OS::get_singleton()) {
152
OS::get_singleton()->printerr("%s: %s\n", _error_handler_type_string(p_type), err_details);
153
} else {
154
// Fallback if errors happen before OS init or after it's destroyed.
155
_err_print_fallback(nullptr, nullptr, 0, err_details, p_type, false);
156
}
157
158
_global_lock();
159
160
ErrorHandlerList *l = error_handler_list;
161
while (l) {
162
l->errfunc(l->userdata, "", "", 0, err_details, "", false, p_type);
163
l = l->next;
164
}
165
166
_global_unlock();
167
168
is_printing_error = false;
169
}
170
171
// Errors with message. (All combinations of p_error and p_message as String or char*.)
172
void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const char *p_message, bool p_editor_notify, ErrorHandlerType p_type) {
173
_err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message, p_editor_notify, p_type);
174
}
175
176
void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const String &p_message, bool p_editor_notify, ErrorHandlerType p_type) {
177
_err_print_error(p_function, p_file, p_line, p_error, p_message.utf8().get_data(), p_editor_notify, p_type);
178
}
179
180
void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const String &p_message, bool p_editor_notify, ErrorHandlerType p_type) {
181
_err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message.utf8().get_data(), p_editor_notify, p_type);
182
}
183
184
// Index errors. (All combinations of p_message as String or char*.)
185
void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const char *p_message, bool p_editor_notify, bool p_fatal) {
186
String fstr(p_fatal ? "FATAL: " : "");
187
String err(fstr + "Index " + p_index_str + " = " + itos(p_index) + " is out of bounds (" + p_size_str + " = " + itos(p_size) + ").");
188
_err_print_error(p_function, p_file, p_line, err.utf8().get_data(), p_message, p_editor_notify, ERR_HANDLER_ERROR);
189
}
190
191
void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const String &p_message, bool p_editor_notify, bool p_fatal) {
192
_err_print_index_error(p_function, p_file, p_line, p_index, p_size, p_index_str, p_size_str, p_message.utf8().get_data(), p_editor_notify, p_fatal);
193
}
194
195
void _err_flush_stdout() {
196
fflush(stdout);
197
}
198
199
// Prevent error spam by limiting the warnings to a certain frequency.
200
void _physics_interpolation_warning(const char *p_function, const char *p_file, int p_line, ObjectID p_id, const char *p_warn_string) {
201
#if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
202
const uint32_t warn_max = 2048;
203
const uint32_t warn_timeout_seconds = 15;
204
205
static uint32_t warn_count = warn_max;
206
static uint32_t warn_timeout = warn_timeout_seconds;
207
208
uint32_t time_now = UINT32_MAX;
209
210
if (warn_count) {
211
warn_count--;
212
}
213
214
if (!warn_count) {
215
time_now = OS::get_singleton()->get_ticks_msec() / 1000;
216
}
217
218
if ((warn_count == 0) && (time_now >= warn_timeout)) {
219
warn_count = warn_max;
220
warn_timeout = time_now + warn_timeout_seconds;
221
222
if (GLOBAL_GET("debug/settings/physics_interpolation/enable_warnings")) {
223
// UINT64_MAX means unused.
224
if (p_id.operator uint64_t() == UINT64_MAX) {
225
_err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + " (possibly benign).", false, ERR_HANDLER_WARNING);
226
} else {
227
String node_name;
228
if (p_id.is_valid()) {
229
Node *node = ObjectDB::get_instance<Node>(p_id);
230
if (node && node->is_inside_tree()) {
231
node_name = "\"" + String(node->get_path()) + "\"";
232
} else {
233
node_name = "\"unknown\"";
234
}
235
}
236
237
_err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + ": " + node_name + " (possibly benign).", false, ERR_HANDLER_WARNING);
238
}
239
}
240
}
241
#endif
242
}
243
244