logger.cpp 8.0 KB

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