os_log_logger.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**************************************************************************/
  2. /* os_log_logger.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 "os_log_logger.h"
  31. #include "core/object/script_backtrace.h"
  32. #include "core/string/print_string.h"
  33. #include <cstdlib> // For malloc/free
  34. OsLogLogger::OsLogLogger(const char *p_subsystem) {
  35. const char *subsystem = p_subsystem;
  36. if (!subsystem) {
  37. subsystem = "org.godotengine.godot";
  38. os_log_info(OS_LOG_DEFAULT, "Missing subsystem for os_log logging; using %{public}s", subsystem);
  39. }
  40. log = os_log_create(subsystem, "engine");
  41. error_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_ERROR));
  42. warning_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_WARNING));
  43. script_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_SCRIPT));
  44. shader_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_SHADER));
  45. }
  46. void OsLogLogger::logv(const char *p_format, va_list p_list, bool p_err) {
  47. constexpr int static_buf_size = 1024;
  48. char static_buf[static_buf_size] = { '\0' };
  49. char *buf = static_buf;
  50. va_list list_copy;
  51. va_copy(list_copy, p_list);
  52. int len = vsnprintf(buf, static_buf_size, p_format, p_list);
  53. if (len >= static_buf_size) {
  54. buf = (char *)Memory::alloc_static(len + 1);
  55. vsnprintf(buf, len + 1, p_format, list_copy);
  56. }
  57. va_end(list_copy);
  58. // Choose appropriate log type based on error flag.
  59. os_log_type_t log_type = p_err ? OS_LOG_TYPE_ERROR : OS_LOG_TYPE_INFO;
  60. os_log_with_type(log, log_type, "%{public}s", buf);
  61. if (len >= static_buf_size) {
  62. Memory::free_static(buf);
  63. }
  64. }
  65. void OsLogLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, ErrorType p_type, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces) {
  66. os_log_t selected_log;
  67. switch (p_type) {
  68. case ERR_WARNING:
  69. selected_log = warning_log;
  70. break;
  71. case ERR_SCRIPT:
  72. selected_log = script_log;
  73. break;
  74. case ERR_SHADER:
  75. selected_log = shader_log;
  76. break;
  77. case ERR_ERROR:
  78. default:
  79. selected_log = error_log;
  80. break;
  81. }
  82. const char *err_details;
  83. if (p_rationale && *p_rationale) {
  84. err_details = p_rationale;
  85. } else {
  86. err_details = p_code;
  87. }
  88. // Choose log level based on error type.
  89. os_log_type_t log_type;
  90. switch (p_type) {
  91. case ERR_WARNING:
  92. log_type = OS_LOG_TYPE_DEFAULT;
  93. break;
  94. case ERR_ERROR:
  95. case ERR_SCRIPT:
  96. case ERR_SHADER:
  97. default:
  98. log_type = OS_LOG_TYPE_ERROR;
  99. break;
  100. }
  101. // Append script backtraces, if any.
  102. String back_trace;
  103. for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
  104. if (backtrace.is_valid() && !backtrace->is_empty()) {
  105. back_trace += "\n";
  106. back_trace += backtrace->format(strlen(error_type_indent(p_type)));
  107. }
  108. }
  109. if (back_trace.is_empty()) {
  110. os_log_with_type(selected_log, log_type, "%{public}s:%d:%{public}s(): %{public}s %{public}s", p_file, p_line, p_function, err_details, p_code);
  111. } else {
  112. os_log_with_type(selected_log, log_type, "%{public}s:%d:%{public}s(): %{public}s %{public}s%{public}s", p_file, p_line, p_function, err_details, p_code, back_trace.utf8().ptr());
  113. }
  114. }