error_macros.cpp 8.8 KB

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