error_macros.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/ustring.h"
  33. #include "os/os.h"
  34. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  35. #include "scene/main/node.h"
  36. #endif
  37. static ErrorHandlerList *error_handler_list = nullptr;
  38. void add_error_handler(ErrorHandlerList *p_handler) {
  39. _global_lock();
  40. p_handler->next = error_handler_list;
  41. error_handler_list = p_handler;
  42. _global_unlock();
  43. }
  44. void remove_error_handler(ErrorHandlerList *p_handler) {
  45. _global_lock();
  46. ErrorHandlerList *prev = nullptr;
  47. ErrorHandlerList *l = error_handler_list;
  48. while (l) {
  49. if (l == p_handler) {
  50. if (prev) {
  51. prev->next = l->next;
  52. } else {
  53. error_handler_list = l->next;
  54. }
  55. break;
  56. }
  57. prev = l;
  58. l = l->next;
  59. }
  60. _global_unlock();
  61. }
  62. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type) {
  63. _err_print_error(p_function, p_file, p_line, p_error, "", p_type);
  64. }
  65. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, ErrorHandlerType p_type) {
  66. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), "", p_type);
  67. }
  68. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, ErrorHandlerType p_type) {
  69. if (OS::get_singleton()) {
  70. OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, p_message, (Logger::ErrorType)p_type);
  71. } else {
  72. // Fallback if errors happen before OS init or after it's destroyed.
  73. const char *err_details = (p_message && *p_message) ? p_message : p_error;
  74. fprintf(stderr, "ERROR: %s\n at: %s (%s:%i)\n", err_details, p_function, p_file, p_line);
  75. }
  76. _global_lock();
  77. ErrorHandlerList *l = error_handler_list;
  78. while (l) {
  79. l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_type);
  80. l = l->next;
  81. }
  82. _global_unlock();
  83. }
  84. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const char *p_message, ErrorHandlerType p_type) {
  85. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message, p_type);
  86. }
  87. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const String &p_message, ErrorHandlerType p_type) {
  88. _err_print_error(p_function, p_file, p_line, p_error, p_message.utf8().get_data(), p_type);
  89. }
  90. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const String &p_message, ErrorHandlerType p_type) {
  91. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message.utf8().get_data(), p_type);
  92. }
  93. 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 fatal) {
  94. String fstr(fatal ? "FATAL: " : "");
  95. String err(fstr + "Index " + p_index_str + " = " + itos(p_index) + " is out of bounds (" + p_size_str + " = " + itos(p_size) + ").");
  96. _err_print_error(p_function, p_file, p_line, err.utf8().get_data(), p_message);
  97. }
  98. 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 fatal) {
  99. _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(), fatal);
  100. }
  101. void _err_flush_stdout() {
  102. fflush(stdout);
  103. }
  104. // Prevent error spam by limiting the warnings to a certain frequency.
  105. void _physics_interpolation_warning(const char *p_function, const char *p_file, int p_line, ObjectID p_id, const char *p_warn_string) {
  106. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  107. const uint32_t warn_max = 2048;
  108. const uint32_t warn_timeout_seconds = 15;
  109. static uint32_t warn_count = warn_max;
  110. static uint32_t warn_timeout = warn_timeout_seconds;
  111. uint32_t time_now = UINT32_MAX;
  112. if (warn_count) {
  113. warn_count--;
  114. }
  115. if (!warn_count) {
  116. time_now = OS::get_singleton()->get_ticks_msec() / 1000;
  117. }
  118. if ((warn_count == 0) && (time_now >= warn_timeout)) {
  119. warn_count = warn_max;
  120. warn_timeout = time_now + warn_timeout_seconds;
  121. if (GLOBAL_GET("debug/settings/physics_interpolation/enable_warnings")) {
  122. // UINT64_MAX means unused.
  123. if (p_id == UINT64_MAX) {
  124. _err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + " (possibly benign).", ERR_HANDLER_WARNING);
  125. } else {
  126. String node_name;
  127. if (p_id != 0) {
  128. if (ObjectDB::get_instance(p_id)) {
  129. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(p_id));
  130. if (node && node->is_inside_tree()) {
  131. node_name = "\"" + String(node->get_path()) + "\"";
  132. } else {
  133. node_name = "\"unknown\"";
  134. }
  135. }
  136. }
  137. _err_print_error(p_function, p_file, p_line, "[Physics interpolation] " + String(p_warn_string) + ": " + node_name + " (possibly benign).", ERR_HANDLER_WARNING);
  138. }
  139. }
  140. }
  141. #endif
  142. }