logger.cpp 7.6 KB

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