logger.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*************************************************************************/
  2. /* logger.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "logger.h"
  31. #include "os/dir_access.h"
  32. #include "os/os.h"
  33. #include "print_string.h"
  34. bool Logger::should_log(bool p_err) {
  35. return (!p_err || _print_error_enabled) && (p_err || _print_line_enabled);
  36. }
  37. void Logger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
  38. if (!should_log(true)) {
  39. return;
  40. }
  41. const char *err_type = "**ERROR**";
  42. switch (p_type) {
  43. case ERR_ERROR: err_type = "**ERROR**"; break;
  44. case ERR_WARNING: err_type = "**WARNING**"; break;
  45. case ERR_SCRIPT: err_type = "**SCRIPT ERROR**"; break;
  46. case ERR_SHADER: err_type = "**SHADER ERROR**"; break;
  47. default: ERR_PRINT("Unknown error type"); break;
  48. }
  49. const char *err_details;
  50. if (p_rationale && *p_rationale)
  51. err_details = p_rationale;
  52. else
  53. err_details = p_code;
  54. logf_error("%s: %s\n", err_type, err_details);
  55. logf_error(" At: %s:%i:%s() - %s\n", p_file, p_line, p_function, p_code);
  56. }
  57. void Logger::logf(const char *p_format, ...) {
  58. if (!should_log(false)) {
  59. return;
  60. }
  61. va_list argp;
  62. va_start(argp, p_format);
  63. logv(p_format, argp, false);
  64. va_end(argp);
  65. }
  66. void Logger::logf_error(const char *p_format, ...) {
  67. if (!should_log(true)) {
  68. return;
  69. }
  70. va_list argp;
  71. va_start(argp, p_format);
  72. logv(p_format, argp, true);
  73. va_end(argp);
  74. }
  75. Logger::~Logger() {}
  76. void RotatedFileLogger::close_file() {
  77. if (file) {
  78. memdelete(file);
  79. file = NULL;
  80. }
  81. }
  82. void RotatedFileLogger::clear_old_backups() {
  83. int max_backups = max_files - 1; // -1 for the current file
  84. String basename = base_path.get_basename();
  85. String extension = "." + base_path.get_extension();
  86. DirAccess *da = DirAccess::open(base_path.get_base_dir());
  87. if (!da) {
  88. return;
  89. }
  90. da->list_dir_begin();
  91. String f = da->get_next();
  92. Set<String> backups;
  93. while (f != String()) {
  94. if (!da->current_is_dir() && f.begins_with(basename) && f.ends_with(extension) && f != base_path) {
  95. backups.insert(f);
  96. }
  97. f = da->get_next();
  98. }
  99. da->list_dir_end();
  100. if (backups.size() > max_backups) {
  101. // since backups are appended with timestamp and Set iterates them in sorted order,
  102. // first backups are the oldest
  103. int to_delete = backups.size() - max_backups;
  104. for (Set<String>::Element *E = backups.front(); E && to_delete > 0; E = E->next(), --to_delete) {
  105. da->remove(E->get());
  106. }
  107. }
  108. memdelete(da);
  109. }
  110. void RotatedFileLogger::rotate_file() {
  111. close_file();
  112. if (FileAccess::exists(base_path)) {
  113. if (max_files > 1) {
  114. char timestamp[21];
  115. OS::Date date = OS::get_singleton()->get_date();
  116. OS::Time time = OS::get_singleton()->get_time();
  117. sprintf(timestamp, "-%04d-%02d-%02d-%02d-%02d-%02d", date.year, date.month, date.day + 1, time.hour, time.min, time.sec);
  118. String backup_name = base_path.get_basename() + timestamp + "." + base_path.get_extension();
  119. DirAccess *da = DirAccess::open(base_path.get_base_dir());
  120. if (da) {
  121. da->copy(base_path, backup_name);
  122. memdelete(da);
  123. }
  124. clear_old_backups();
  125. }
  126. } else {
  127. DirAccess *da = DirAccess::create(DirAccess::ACCESS_USERDATA);
  128. if (da) {
  129. da->make_dir_recursive(base_path.get_base_dir());
  130. memdelete(da);
  131. }
  132. }
  133. file = FileAccess::open(base_path, FileAccess::WRITE);
  134. }
  135. RotatedFileLogger::RotatedFileLogger(const String &p_base_path, int p_max_files) {
  136. file = NULL;
  137. base_path = p_base_path.simplify_path();
  138. max_files = p_max_files > 0 ? p_max_files : 1;
  139. rotate_file();
  140. }
  141. void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err) {
  142. if (!should_log(p_err)) {
  143. return;
  144. }
  145. if (file) {
  146. const int static_buf_size = 512;
  147. char static_buf[static_buf_size];
  148. char *buf = static_buf;
  149. va_list list_copy;
  150. va_copy(list_copy, p_list);
  151. int len = vsnprintf(buf, static_buf_size, p_format, p_list);
  152. if (len >= static_buf_size) {
  153. buf = (char *)Memory::alloc_static(len + 1);
  154. vsnprintf(buf, len + 1, p_format, list_copy);
  155. }
  156. va_end(list_copy);
  157. file->store_buffer((uint8_t *)buf, len);
  158. if (len >= static_buf_size) {
  159. Memory::free_static(buf);
  160. }
  161. #ifdef DEBUG_ENABLED
  162. const bool need_flush = true;
  163. #else
  164. bool need_flush = p_err;
  165. #endif
  166. if (need_flush) {
  167. file->flush();
  168. }
  169. }
  170. }
  171. RotatedFileLogger::~RotatedFileLogger() {
  172. close_file();
  173. }
  174. void StdLogger::logv(const char *p_format, va_list p_list, bool p_err) {
  175. if (!should_log(p_err)) {
  176. return;
  177. }
  178. if (p_err) {
  179. vfprintf(stderr, p_format, p_list);
  180. } else {
  181. vprintf(p_format, p_list);
  182. #ifdef DEBUG_ENABLED
  183. fflush(stdout);
  184. #endif
  185. }
  186. }
  187. StdLogger::~StdLogger() {}
  188. CompositeLogger::CompositeLogger(Vector<Logger *> p_loggers) {
  189. loggers = p_loggers;
  190. }
  191. void CompositeLogger::logv(const char *p_format, va_list p_list, bool p_err) {
  192. if (!should_log(p_err)) {
  193. return;
  194. }
  195. for (int i = 0; i < loggers.size(); ++i) {
  196. va_list list_copy;
  197. va_copy(list_copy, p_list);
  198. loggers[i]->logv(p_format, list_copy, p_err);
  199. va_end(list_copy);
  200. }
  201. }
  202. void CompositeLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) {
  203. if (!should_log(true)) {
  204. return;
  205. }
  206. for (int i = 0; i < loggers.size(); ++i) {
  207. loggers[i]->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
  208. }
  209. }
  210. CompositeLogger::~CompositeLogger() {
  211. for (int i = 0; i < loggers.size(); ++i) {
  212. memdelete(loggers[i]);
  213. }
  214. }