logger.cpp 8.0 KB

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