2
0

logger.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**************************************************************************/
  2. /* logger.h */
  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. #pragma once
  31. #include "core/io/file_access.h"
  32. #include "core/object/script_backtrace.h"
  33. #include "core/string/ustring.h"
  34. #include "core/templates/vector.h"
  35. #include <cstdarg>
  36. class RegEx;
  37. class Logger {
  38. protected:
  39. bool should_log(bool p_err);
  40. static inline bool _flush_stdout_on_print = true;
  41. public:
  42. enum ErrorType {
  43. ERR_ERROR,
  44. ERR_WARNING,
  45. ERR_SCRIPT,
  46. ERR_SHADER
  47. };
  48. static constexpr const char *error_type_string(ErrorType p_type) {
  49. switch (p_type) {
  50. case ERR_ERROR:
  51. return "ERROR";
  52. case ERR_WARNING:
  53. return "WARNING";
  54. case ERR_SCRIPT:
  55. return "SCRIPT ERROR";
  56. case ERR_SHADER:
  57. return "SHADER ERROR";
  58. }
  59. return "UNKNOWN ERROR";
  60. }
  61. static constexpr const char *error_type_indent(ErrorType p_type) {
  62. switch (p_type) {
  63. case ERR_ERROR:
  64. return " ";
  65. case ERR_WARNING:
  66. return " ";
  67. case ERR_SCRIPT:
  68. return " ";
  69. case ERR_SHADER:
  70. return " ";
  71. }
  72. return " ";
  73. }
  74. static void set_flush_stdout_on_print(bool value);
  75. virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0 = 0;
  76. virtual void 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 = false, ErrorType p_type = ERR_ERROR, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces = {});
  77. void logf(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  78. void logf_error(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
  79. virtual ~Logger() {}
  80. };
  81. /**
  82. * Writes messages to stdout/stderr.
  83. */
  84. class StdLogger : public Logger {
  85. public:
  86. virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;
  87. virtual ~StdLogger() {}
  88. };
  89. /**
  90. * Writes messages to the specified file. If the file already exists, creates a copy (backup)
  91. * of it with timestamp appended to the file name. Maximum number of backups is configurable.
  92. * When maximum is reached, the oldest backups are erased. With the maximum being equal to 1,
  93. * it acts as a simple file logger.
  94. */
  95. class RotatedFileLogger : public Logger {
  96. String base_path;
  97. int max_files;
  98. Ref<FileAccess> file;
  99. void clear_old_backups();
  100. void rotate_file();
  101. Ref<RegEx> strip_ansi_regex;
  102. public:
  103. explicit RotatedFileLogger(const String &p_base_path, int p_max_files = 10);
  104. virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;
  105. };
  106. class CompositeLogger : public Logger {
  107. Vector<Logger *> loggers;
  108. public:
  109. explicit CompositeLogger(const Vector<Logger *> &p_loggers);
  110. virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;
  111. virtual void 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 = ERR_ERROR, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces = {}) override;
  112. void add_logger(Logger *p_logger);
  113. virtual ~CompositeLogger();
  114. };