logger.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*************************************************************************/
  2. /* logger.h */
  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. #ifndef LOGGER_H
  31. #define LOGGER_H
  32. #include "core/os/file_access.h"
  33. #include "core/ustring.h"
  34. #include "core/vector.h"
  35. #include <stdarg.h>
  36. class Logger {
  37. protected:
  38. bool should_log(bool p_err);
  39. public:
  40. enum ErrorType {
  41. ERR_ERROR,
  42. ERR_WARNING,
  43. ERR_SCRIPT,
  44. ERR_SHADER
  45. };
  46. virtual void logv(const char *p_format, va_list p_list, bool p_err) = 0;
  47. virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
  48. void logf(const char *p_format, ...);
  49. void logf_error(const char *p_format, ...);
  50. virtual ~Logger();
  51. };
  52. /**
  53. * Writes messages to stdout/stderr.
  54. */
  55. class StdLogger : public Logger {
  56. public:
  57. virtual void logv(const char *p_format, va_list p_list, bool p_err);
  58. virtual ~StdLogger();
  59. };
  60. /**
  61. * Writes messages to the specified file. If the file already exists, creates a copy (backup)
  62. * of it with timestamp appended to the file name. Maximum number of backups is configurable.
  63. * When maximum is reached, the oldest backups are erased. With the maximum being equal to 1,
  64. * it acts as a simple file logger.
  65. */
  66. class RotatedFileLogger : public Logger {
  67. String base_path;
  68. int max_files;
  69. FileAccess *file;
  70. void rotate_file_without_closing();
  71. void close_file();
  72. void clear_old_backups();
  73. void rotate_file();
  74. public:
  75. RotatedFileLogger(const String &p_base_path, int p_max_files = 10);
  76. virtual void logv(const char *p_format, va_list p_list, bool p_err);
  77. virtual ~RotatedFileLogger();
  78. };
  79. class CompositeLogger : public Logger {
  80. Vector<Logger *> loggers;
  81. public:
  82. CompositeLogger(Vector<Logger *> p_loggers);
  83. virtual void logv(const char *p_format, va_list p_list, bool p_err);
  84. virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
  85. void add_logger(Logger *p_logger);
  86. virtual ~CompositeLogger();
  87. };
  88. #endif