error_macros.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. static thread_local bool is_printing_error = false;
  43. 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) {
  44. if (p_reentrance) {
  45. fprintf(stderr, "While attempting to print an error, another error was printed:\n");
  46. }
  47. fprintf(stderr, "%s: %s\n", _error_handler_type_string(p_type), p_error_details);
  48. if (p_function && p_file) {
  49. fprintf(stderr, " at: %s (%s:%i)\n", p_function, p_file, p_line);
  50. }
  51. }
  52. void add_error_handler(ErrorHandlerList *p_handler) {
  53. // If p_handler is already in error_handler_list
  54. // we'd better remove it first then we can add it.
  55. // This prevent cyclic redundancy.
  56. remove_error_handler(p_handler);
  57. _global_lock();
  58. p_handler->next = error_handler_list;
  59. error_handler_list = p_handler;
  60. _global_unlock();
  61. }
  62. void remove_error_handler(const ErrorHandlerList *p_handler) {
  63. _global_lock();
  64. ErrorHandlerList *prev = nullptr;
  65. ErrorHandlerList *l = error_handler_list;
  66. while (l) {
  67. if (l == p_handler) {
  68. if (prev) {
  69. prev->next = l->next;
  70. } else {
  71. error_handler_list = l->next;
  72. }
  73. break;
  74. }
  75. prev = l;
  76. l = l->next;
  77. }
  78. _global_unlock();
  79. }
  80. // Errors without messages.
  81. 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) {
  82. _err_print_error(p_function, p_file, p_line, p_error, "", p_editor_notify, p_type);
  83. }
  84. 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) {
  85. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), "", p_editor_notify, p_type);
  86. }
  87. // Main error printing function.
  88. 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) {
  89. if (is_printing_error) {
  90. // Fallback if we're already printing an error, to prevent infinite recursion.
  91. const char *err_details = (p_message && *p_message) ? p_message : p_error;
  92. _err_print_fallback(p_function, p_file, p_line, err_details, p_type, true);
  93. return;
  94. }
  95. is_printing_error = true;
  96. if (OS::get_singleton()) {
  97. 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));
  98. } else {
  99. // Fallback if errors happen before OS init or after it's destroyed.
  100. const char *err_details = (p_message && *p_message) ? p_message : p_error;
  101. _err_print_fallback(p_function, p_file, p_line, err_details, p_type, false);
  102. }
  103. _global_lock();
  104. ErrorHandlerList *l = error_handler_list;
  105. while (l) {
  106. l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_editor_notify, p_type);
  107. l = l->next;
  108. }
  109. _global_unlock();
  110. is_printing_error = false;
  111. }
  112. // For printing errors when we may crash at any point, so we must flush ASAP a lot of lines
  113. // but we don't want to make it noisy by printing lots of file & line info (because it's already
  114. // been printing by a preceding _err_print_error).
  115. void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type) {
  116. const char *err_details = p_error.utf8().get_data();
  117. if (is_printing_error) {
  118. // Fallback if we're already printing an error, to prevent infinite recursion.
  119. _err_print_fallback(nullptr, nullptr, 0, err_details, p_type, true);
  120. return;
  121. }
  122. is_printing_error = true;
  123. if (OS::get_singleton()) {
  124. OS::get_singleton()->printerr("%s: %s\n", _error_handler_type_string(p_type), err_details);
  125. } else {
  126. // Fallback if errors happen before OS init or after it's destroyed.
  127. _err_print_fallback(nullptr, nullptr, 0, err_details, p_type, false);
  128. }
  129. _global_lock();
  130. ErrorHandlerList *l = error_handler_list;
  131. while (l) {
  132. l->errfunc(l->userdata, "", "", 0, err_details, "", false, p_type);
  133. l = l->next;
  134. }
  135. _global_unlock();
  136. is_printing_error = false;
  137. }
  138. // Errors with message. (All combinations of p_error and p_message as String or char*.)
  139. 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) {
  140. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message, p_editor_notify, p_type);
  141. }
  142. 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) {
  143. _err_print_error(p_function, p_file, p_line, p_error, p_message.utf8().get_data(), p_editor_notify, p_type);
  144. }
  145. 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) {
  146. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message.utf8().get_data(), p_editor_notify, p_type);
  147. }
  148. // Index errors. (All combinations of p_message as String or char*.)
  149. 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) {
  150. String fstr(p_fatal ? "FATAL: " : "");
  151. String err(fstr + "Index " + p_index_str + " = " + itos(p_index) + " is out of bounds (" + p_size_str + " = " + itos(p_size) + ").");
  152. _err_print_error(p_function, p_file, p_line, err.utf8().get_data(), p_message, p_editor_notify, ERR_HANDLER_ERROR);
  153. }
  154. 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) {
  155. _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);
  156. }
  157. void _err_flush_stdout() {
  158. fflush(stdout);
  159. }
  160. // Prevent error spam by limiting the warnings to a certain frequency.
  161. void _physics_interpolation_warning(const char *p_function, const char *p_file, int p_line, ObjectID p_id, const char *p_warn_string) {
  162. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  163. const uint32_t warn_max = 2048;
  164. const uint32_t warn_timeout_seconds = 15;
  165. static uint32_t warn_count = warn_max;
  166. static uint32_t warn_timeout = warn_timeout_seconds;
  167. uint32_t time_now = UINT32_MAX;
  168. if (warn_count) {
  169. warn_count--;
  170. }
  171. if (!warn_count) {
  172. time_now = OS::get_singleton()->get_ticks_msec() / 1000;
  173. }
  174. if ((warn_count == 0) && (time_now >= warn_timeout)) {
  175. warn_count = warn_max;
  176. warn_timeout = time_now + warn_timeout_seconds;
  177. if (GLOBAL_GET("debug/settings/physics_interpolation/enable_warnings")) {
  178. // UINT64_MAX means unused.
  179. if (p_id.operator uint64_t() == UINT64_MAX) {
  180. _err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + " (possibly benign).", false, ERR_HANDLER_WARNING);
  181. } else {
  182. String node_name;
  183. if (p_id.is_valid()) {
  184. Node *node = ObjectDB::get_instance<Node>(p_id);
  185. if (node && node->is_inside_tree()) {
  186. node_name = "\"" + String(node->get_path()) + "\"";
  187. } else {
  188. node_name = "\"unknown\"";
  189. }
  190. }
  191. _err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + ": " + node_name + " (possibly benign).", false, ERR_HANDLER_WARNING);
  192. }
  193. }
  194. }
  195. #endif
  196. }