| 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/os/os.h" |
| 35 | #include "core/string/ustring.h" |
| 36 | |
| 37 | static ErrorHandlerList *error_handler_list = nullptr; |
| 38 | |
| 39 | void add_error_handler(ErrorHandlerList *p_handler) { |
| 40 | // If p_handler is already in error_handler_list |
| 41 | // we'd better remove it first then we can add it. |
| 42 | // This prevent cyclic redundancy. |
| 43 | remove_error_handler(p_handler); |
| 44 | |
| 45 | _global_lock(); |
| 46 | |
| 47 | p_handler->next = error_handler_list; |
| 48 | error_handler_list = p_handler; |
| 49 | |
| 50 | _global_unlock(); |
| 51 | } |
| 52 | |
| 53 | void remove_error_handler(const ErrorHandlerList *p_handler) { |
| 54 | _global_lock(); |
| 55 | |
| 56 | ErrorHandlerList *prev = nullptr; |
| 57 | ErrorHandlerList *l = error_handler_list; |
| 58 | |
| 59 | while (l) { |
| 60 | if (l == p_handler) { |
| 61 | if (prev) { |
| 62 | prev->next = l->next; |
| 63 | } else { |
| 64 | error_handler_list = l->next; |
| 65 | } |
| 66 | break; |
| 67 | } |
| 68 | prev = l; |
| 69 | l = l->next; |
| 70 | } |
| 71 | |
| 72 | _global_unlock(); |
| 73 | } |
| 74 | |
| 75 | // Errors without messages. |
| 76 | 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) { |
| 77 | _err_print_error(p_function, p_file, p_line, p_error, "" , p_editor_notify, p_type); |
| 78 | } |
| 79 | |
| 80 | 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) { |
| 81 | _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), "" , p_editor_notify, p_type); |
| 82 | } |
| 83 | |
| 84 | // Main error printing function. |
| 85 | 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) { |
| 86 | if (OS::get_singleton()) { |
| 87 | OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, p_message, p_editor_notify, (Logger::ErrorType)p_type); |
| 88 | } else { |
| 89 | // Fallback if errors happen before OS init or after it's destroyed. |
| 90 | const char *err_details = (p_message && *p_message) ? p_message : p_error; |
| 91 | fprintf(stderr, "ERROR: %s\n at: %s (%s:%i)\n" , err_details, p_function, p_file, p_line); |
| 92 | } |
| 93 | |
| 94 | _global_lock(); |
| 95 | ErrorHandlerList *l = error_handler_list; |
| 96 | while (l) { |
| 97 | l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_editor_notify, p_type); |
| 98 | l = l->next; |
| 99 | } |
| 100 | |
| 101 | _global_unlock(); |
| 102 | } |
| 103 | |
| 104 | // Errors with message. (All combinations of p_error and p_message as String or char*.) |
| 105 | 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) { |
| 106 | _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message, p_editor_notify, p_type); |
| 107 | } |
| 108 | |
| 109 | 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) { |
| 110 | _err_print_error(p_function, p_file, p_line, p_error, p_message.utf8().get_data(), p_editor_notify, p_type); |
| 111 | } |
| 112 | |
| 113 | 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) { |
| 114 | _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message.utf8().get_data(), p_editor_notify, p_type); |
| 115 | } |
| 116 | |
| 117 | // Index errors. (All combinations of p_message as String or char*.) |
| 118 | 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) { |
| 119 | String fstr(p_fatal ? "FATAL: " : "" ); |
| 120 | String err(fstr + "Index " + p_index_str + " = " + itos(p_index) + " is out of bounds (" + p_size_str + " = " + itos(p_size) + ")." ); |
| 121 | _err_print_error(p_function, p_file, p_line, err.utf8().get_data(), p_message, p_editor_notify, ERR_HANDLER_ERROR); |
| 122 | } |
| 123 | |
| 124 | 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) { |
| 125 | _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); |
| 126 | } |
| 127 | |
| 128 | void _err_flush_stdout() { |
| 129 | fflush(stdout); |
| 130 | } |
| 131 | |