Core: Unify display of error type prefixes

This commit is contained in:
Danil Alexeev
2025-05-05 19:08:19 +03:00
parent 8f87e60307
commit 24494d840e
13 changed files with 102 additions and 152 deletions

View File

@@ -362,7 +362,7 @@ void LocalDebugger::send_message(const String &p_message, const Array &p_args) {
}
void LocalDebugger::send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, bool p_editor_notify, ErrorHandlerType p_type) {
print_line("ERROR: '" + (p_descr.is_empty() ? p_err : p_descr) + "'");
_err_print_error(p_func.utf8().get_data(), p_file.utf8().get_data(), p_line, p_err, p_descr, p_editor_notify, p_type);
}
LocalDebugger::LocalDebugger() {

View File

@@ -96,10 +96,11 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
} else {
// Fallback if errors happen before OS init or after it's destroyed.
const char *err_details = (p_message && *p_message) ? p_message : p_error;
fprintf(stderr, "ERROR: %s\n at: %s (%s:%i)\n", err_details, p_function, p_file, p_line);
fprintf(stderr, "%s: %s\n at: %s (%s:%i)\n", _error_handler_type_string(p_type), err_details, p_function, p_file, p_line);
}
_global_lock();
ErrorHandlerList *l = error_handler_list;
while (l) {
l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_editor_notify, p_type);
@@ -113,18 +114,20 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
// but we don't want to make it noisy by printing lots of file & line info (because it's already
// been printing by a preceding _err_print_error).
void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type) {
const char *err_details = p_error.utf8().get_data();
if (OS::get_singleton()) {
OS::get_singleton()->printerr("ERROR: %s\n", p_error.utf8().get_data());
OS::get_singleton()->printerr("%s: %s\n", _error_handler_type_string(p_type), err_details);
} else {
// Fallback if errors happen before OS init or after it's destroyed.
const char *err_details = p_error.utf8().get_data();
fprintf(stderr, "ERROR: %s\n", err_details);
fprintf(stderr, "%s: %s\n", _error_handler_type_string(p_type), err_details);
}
_global_lock();
ErrorHandlerList *l = error_handler_list;
while (l) {
l->errfunc(l->userdata, "", "", 0, p_error.utf8().get_data(), "", false, p_type);
l->errfunc(l->userdata, "", "", 0, err_details, "", false, p_type);
l = l->next;
}

View File

@@ -46,6 +46,20 @@ enum ErrorHandlerType {
ERR_HANDLER_SHADER,
};
constexpr const char *_error_handler_type_string(ErrorHandlerType p_type) {
switch (p_type) {
case ERR_HANDLER_ERROR:
return "ERROR";
case ERR_HANDLER_WARNING:
return "WARNING";
case ERR_HANDLER_SCRIPT:
return "SCRIPT ERROR";
case ERR_HANDLER_SHADER:
return "SHADER ERROR";
}
return "UNKNOWN ERROR";
}
// Pointer to the error handler printing function. Reassign to any function to have errors printed.
// Parameters: userdata, function, file, line, error, explanation, type.
typedef void (*ErrorHandlerFunc)(void *, const char *, const char *, int p_line, const char *, const char *, bool p_editor_notify, ErrorHandlerType p_type);

View File

@@ -58,24 +58,7 @@ void Logger::log_error(const char *p_function, const char *p_file, int p_line, c
return;
}
const char *err_type = "ERROR";
switch (p_type) {
case ERR_ERROR:
err_type = "ERROR";
break;
case ERR_WARNING:
err_type = "WARNING";
break;
case ERR_SCRIPT:
err_type = "SCRIPT ERROR";
break;
case ERR_SHADER:
err_type = "SHADER ERROR";
break;
default:
ERR_PRINT("Unknown error type");
break;
}
const char *err_type = error_type_string(p_type);
const char *err_details;
if (p_rationale && *p_rationale) {

View File

@@ -53,6 +53,34 @@ public:
ERR_SHADER
};
static constexpr const char *error_type_string(ErrorType p_type) {
switch (p_type) {
case ERR_ERROR:
return "ERROR";
case ERR_WARNING:
return "WARNING";
case ERR_SCRIPT:
return "SCRIPT ERROR";
case ERR_SHADER:
return "SHADER ERROR";
}
return "UNKNOWN ERROR";
}
static constexpr const char *error_type_indent(ErrorType p_type) {
switch (p_type) {
case ERR_ERROR:
return " ";
case ERR_WARNING:
return " ";
case ERR_SCRIPT:
return " ";
case ERR_SHADER:
return " ";
}
return " ";
}
static void set_flush_stdout_on_print(bool value);
virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0 = 0;