logger.cpp 8.0 KB

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