error_macros.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*************************************************************************/
  2. /* error_macros.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. bool _err_error_exists = false;
  35. static ErrorHandlerList *error_handler_list = NULL;
  36. void _err_set_last_error(const char *p_err) {
  37. OS::get_singleton()->set_last_error(p_err);
  38. }
  39. void _err_set_last_error(const String &p_err) {
  40. _err_set_last_error(p_err.utf8().get_data());
  41. }
  42. void _err_clear_last_error() {
  43. OS::get_singleton()->clear_last_error();
  44. }
  45. void add_error_handler(ErrorHandlerList *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(ErrorHandlerList *p_handler) {
  52. _global_lock();
  53. ErrorHandlerList *prev = NULL;
  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. break;
  62. }
  63. prev = l;
  64. l = l->next;
  65. }
  66. _global_unlock();
  67. }
  68. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type) {
  69. OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, _err_error_exists ? OS::get_singleton()->get_last_error() : "", (Logger::ErrorType)p_type);
  70. _global_lock();
  71. ErrorHandlerList *l = error_handler_list;
  72. while (l) {
  73. l->errfunc(l->userdata, p_function, p_file, p_line, p_error, _err_error_exists ? OS::get_singleton()->get_last_error() : "", p_type);
  74. l = l->next;
  75. }
  76. _global_unlock();
  77. if (_err_error_exists) {
  78. OS::get_singleton()->clear_last_error();
  79. _err_error_exists = false;
  80. }
  81. }
  82. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, ErrorHandlerType p_type) {
  83. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_type);
  84. }
  85. 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) {
  86. OS::get_singleton()->print_error(p_function, p_file, p_line, p_error, p_message, (Logger::ErrorType)p_type);
  87. _global_lock();
  88. ErrorHandlerList *l = error_handler_list;
  89. while (l) {
  90. l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_type);
  91. l = l->next;
  92. }
  93. _global_unlock();
  94. }
  95. 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) {
  96. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message, p_type);
  97. }
  98. 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) {
  99. _err_print_error(p_function, p_file, p_line, p_error, p_message.utf8().get_data(), p_type);
  100. }
  101. 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) {
  102. _err_print_error(p_function, p_file, p_line, p_error.utf8().get_data(), p_message.utf8().get_data(), p_type);
  103. }
  104. 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) {
  105. String fstr(fatal ? "FATAL: " : "");
  106. String err(fstr + "Index " + p_index_str + "=" + itos(p_index) + " out of size (" + p_size_str + "=" + itos(p_size) + ")");
  107. _err_print_error(p_function, p_file, p_line, err.utf8().get_data(), p_message);
  108. }
  109. 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) {
  110. _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);
  111. }