error_macros.cpp 6.3 KB

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