os_log_logger.cpp 5.1 KB

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