logger.cpp 7.8 KB

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