error_macros.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include "error_macros.h"
  31. #include "core/io/logger.h"
  32. #include "core/object/object_id.h"
  33. #include "core/object/script_language.h"
  34. #include "core/os/os.h"
  35. #include "core/string/ustring.h"
  36. // Optional physics interpolation warnings try to include the path to the relevant node.
  37. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  38. #include "core/config/project_settings.h"
  39. #include "scene/main/node.h"
  40. #endif
  41. static ErrorHandlerList *error_handler_list = nullptr;
  42. void add_error_handler(ErrorHandlerList *p_handler) {
  43. // If p_handler is already in error_handler_list
  44. // we'd better remove it first then we can add it.
  45. // This prevent cyclic redundancy.
  46. remove_error_handler(p_handler);
  47. _global_lock();
  48. p_handler->next = error_handler_list;
  49. error_handler_list = p_handler;
  50. _global_unlock();
  51. }
  52. void remove_error_handler(const ErrorHandlerList *p_handler) {
  53. _global_lock();
  54. ErrorHandlerList *prev = nullptr;
  55. ErrorHandlerList *l = error_handler_list;
  56. while (l) {
  57. if (l == p_handler) {
  58. if (prev) {
  59. prev->next = l->next;
  60. } else {
  61. error_handler_list = l->next;
  62. }
  63. break;
  64. }
  65. prev = l;
  66. l = l->next;
  67. }
  68. _global_unlock();
  69. }
  70. // Errors without messages.
  71. 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) {
  72. _err_print_error(p_function, p_file, p_line, p_error, "", p_editor_notify, p_type);
  73. }
  74. 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) {
  75. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), "", p_editor_notify, p_type);
  76. }
  77. // Main error printing function.
  78. 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) {
  79. if (OS::get_singleton()) {
  80. Vector<Ref<ScriptBacktrace>> script_backtraces;
  81. // If script languages aren't initialized, we could be in the process of shutting down, in which case we don't want to allocate any objects, as we could be
  82. // logging ObjectDB leaks, where ObjectDB would be locked, thus causing a deadlock.
  83. if (ScriptServer::are_languages_initialized()) {
  84. script_backtraces = ScriptServer::capture_script_backtraces(false);
  85. }
  86. OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, p_message, p_editor_notify, (Logger::ErrorType)p_type, script_backtraces);
  87. } else {
  88. // Fallback if errors happen before OS init or after it's destroyed.
  89. const char *err_details = (p_message && *p_message) ? p_message : p_error;
  90. fprintf(stderr, "ERROR: %s\n at: %s (%s:%i)\n", err_details, p_function, p_file, p_line);
  91. }
  92. _global_lock();
  93. ErrorHandlerList *l = error_handler_list;
  94. while (l) {
  95. l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_editor_notify, p_type);
  96. l = l->next;
  97. }
  98. _global_unlock();
  99. }
  100. // For printing errors when we may crash at any point, so we must flush ASAP a lot of lines
  101. // but we don't want to make it noisy by printing lots of file & line info (because it's already
  102. // been printing by a preceding _err_print_error).
  103. void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type) {
  104. if (OS::get_singleton()) {
  105. OS::get_singleton()->printerr("ERROR: %s\n", p_error.utf8().get_data());
  106. } else {
  107. // Fallback if errors happen before OS init or after it's destroyed.
  108. const char *err_details = p_error.utf8().get_data();
  109. fprintf(stderr, "ERROR: %s\n", err_details);
  110. }
  111. _global_lock();
  112. ErrorHandlerList *l = error_handler_list;
  113. while (l) {
  114. l->errfunc(l->userdata, "", "", 0, p_error.utf8().get_data(), "", false, p_type);
  115. l = l->next;
  116. }
  117. _global_unlock();
  118. }
  119. // Errors with message. (All combinations of p_error and p_message as String or char*.)
  120. 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) {
  121. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message, p_editor_notify, p_type);
  122. }
  123. 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) {
  124. _err_print_error(p_function, p_file, p_line, p_error, p_message.utf8().get_data(), p_editor_notify, p_type);
  125. }
  126. 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) {
  127. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message.utf8().get_data(), p_editor_notify, p_type);
  128. }
  129. // Index errors. (All combinations of p_message as String or char*.)
  130. 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) {
  131. String fstr(p_fatal ? "FATAL: " : "");
  132. String err(fstr + "Index " + p_index_str + " = " + itos(p_index) + " is out of bounds (" + p_size_str + " = " + itos(p_size) + ").");
  133. _err_print_error(p_function, p_file, p_line, err.utf8().get_data(), p_message, p_editor_notify, ERR_HANDLER_ERROR);
  134. }
  135. 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) {
  136. _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);
  137. }
  138. void _err_flush_stdout() {
  139. fflush(stdout);
  140. }
  141. // Prevent error spam by limiting the warnings to a certain frequency.
  142. void _physics_interpolation_warning(const char *p_function, const char *p_file, int p_line, ObjectID p_id, const char *p_warn_string) {
  143. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  144. const uint32_t warn_max = 2048;
  145. const uint32_t warn_timeout_seconds = 15;
  146. static uint32_t warn_count = warn_max;
  147. static uint32_t warn_timeout = warn_timeout_seconds;
  148. uint32_t time_now = UINT32_MAX;
  149. if (warn_count) {
  150. warn_count--;
  151. }
  152. if (!warn_count) {
  153. time_now = OS::get_singleton()->get_ticks_msec() / 1000;
  154. }
  155. if ((warn_count == 0) && (time_now >= warn_timeout)) {
  156. warn_count = warn_max;
  157. warn_timeout = time_now + warn_timeout_seconds;
  158. if (GLOBAL_GET("debug/settings/physics_interpolation/enable_warnings")) {
  159. // UINT64_MAX means unused.
  160. if (p_id.operator uint64_t() == UINT64_MAX) {
  161. _err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + " (possibly benign).", false, ERR_HANDLER_WARNING);
  162. } else {
  163. String node_name;
  164. if (p_id.is_valid()) {
  165. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(p_id));
  166. if (node && node->is_inside_tree()) {
  167. node_name = "\"" + String(node->get_path()) + "\"";
  168. } else {
  169. node_name = "\"unknown\"";
  170. }
  171. }
  172. _err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + ": " + node_name + " (possibly benign).", false, ERR_HANDLER_WARNING);
  173. }
  174. }
  175. }
  176. #endif
  177. }