logger.cpp 7.6 KB

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