gd_mono_log.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*************************************************************************/
  2. /* gd_mono_log.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 "gd_mono_log.h"
  31. #include <stdlib.h> // abort
  32. #include "core/os/dir_access.h"
  33. #include "core/os/os.h"
  34. #include "../godotsharp_dirs.h"
  35. #include "../utils/string_utils.h"
  36. static CharString get_default_log_level() {
  37. #ifdef DEBUG_ENABLED
  38. return String("info").utf8();
  39. #else
  40. return String("warning").utf8();
  41. #endif
  42. }
  43. GDMonoLog *GDMonoLog::singleton = nullptr;
  44. #ifdef GD_MONO_LOG_ENABLED
  45. static int get_log_level_id(const char *p_log_level) {
  46. const char *valid_log_levels[] = { "error", "critical", "warning", "message", "info", "debug", nullptr };
  47. int i = 0;
  48. while (valid_log_levels[i]) {
  49. if (!strcmp(valid_log_levels[i], p_log_level)) {
  50. return i;
  51. }
  52. i++;
  53. }
  54. return -1;
  55. }
  56. static String make_text(const char *log_domain, const char *log_level, const char *message) {
  57. String text(message);
  58. text += " (in domain ";
  59. text += log_domain;
  60. if (log_level) {
  61. text += ", ";
  62. text += log_level;
  63. }
  64. text += ")";
  65. return text;
  66. }
  67. void GDMonoLog::mono_log_callback(const char *log_domain, const char *log_level, const char *message, mono_bool fatal, void *) {
  68. FileAccess *f = GDMonoLog::get_singleton()->log_file;
  69. if (GDMonoLog::get_singleton()->log_level_id >= get_log_level_id(log_level)) {
  70. String text = make_text(log_domain, log_level, message);
  71. text += "\n";
  72. f->seek_end();
  73. f->store_string(text);
  74. }
  75. if (fatal) {
  76. String text = make_text(log_domain, log_level, message);
  77. ERR_PRINT("Mono: FATAL ERROR '" + text + "', ABORTING! Logfile: '" + GDMonoLog::get_singleton()->log_file_path + "'.");
  78. // Make sure to flush before aborting
  79. f->flush();
  80. f->close();
  81. memdelete(f);
  82. abort();
  83. }
  84. }
  85. bool GDMonoLog::_try_create_logs_dir(const String &p_logs_dir) {
  86. if (!DirAccess::exists(p_logs_dir)) {
  87. DirAccessRef diraccess = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  88. ERR_FAIL_COND_V(!diraccess, false);
  89. Error logs_mkdir_err = diraccess->make_dir_recursive(p_logs_dir);
  90. ERR_FAIL_COND_V_MSG(logs_mkdir_err != OK, false, "Failed to create mono logs directory.");
  91. }
  92. return true;
  93. }
  94. void GDMonoLog::_delete_old_log_files(const String &p_logs_dir) {
  95. static const uint64_t MAX_SECS = 5 * 86400; // 5 days
  96. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  97. ERR_FAIL_COND(!da);
  98. Error err = da->change_dir(p_logs_dir);
  99. ERR_FAIL_COND_MSG(err != OK, "Cannot change directory to '" + p_logs_dir + "'.");
  100. ERR_FAIL_COND(da->list_dir_begin() != OK);
  101. String current;
  102. while ((current = da->get_next()).length()) {
  103. if (da->current_is_dir()) {
  104. continue;
  105. }
  106. if (!current.ends_with(".txt")) {
  107. continue;
  108. }
  109. uint64_t modified_time = FileAccess::get_modified_time(da->get_current_dir().plus_file(current));
  110. if (OS::get_singleton()->get_unix_time() - modified_time > MAX_SECS) {
  111. da->remove(current);
  112. }
  113. }
  114. da->list_dir_end();
  115. }
  116. void GDMonoLog::initialize() {
  117. CharString log_level = OS::get_singleton()->get_environment("GODOT_MONO_LOG_LEVEL").utf8();
  118. if (log_level.length() != 0 && get_log_level_id(log_level.get_data()) == -1) {
  119. ERR_PRINT(String() + "Mono: Ignoring invalid log level (GODOT_MONO_LOG_LEVEL): '" + log_level.get_data() + "'.");
  120. log_level = CharString();
  121. }
  122. if (log_level.length() == 0) {
  123. log_level = get_default_log_level();
  124. }
  125. String logs_dir = GodotSharpDirs::get_mono_logs_dir();
  126. if (_try_create_logs_dir(logs_dir)) {
  127. _delete_old_log_files(logs_dir);
  128. OS::Date date_now = OS::get_singleton()->get_date();
  129. OS::Time time_now = OS::get_singleton()->get_time();
  130. String log_file_name = str_format("%04d-%02d-%02d_%02d.%02d.%02d",
  131. date_now.year, date_now.month, date_now.day,
  132. time_now.hour, time_now.min, time_now.sec);
  133. log_file_name += str_format("_%d", OS::get_singleton()->get_process_id());
  134. log_file_name += ".log";
  135. log_file_path = logs_dir.plus_file(log_file_name);
  136. log_file = FileAccess::open(log_file_path, FileAccess::WRITE);
  137. if (!log_file) {
  138. ERR_PRINT("Mono: Cannot create log file at: " + log_file_path);
  139. }
  140. }
  141. mono_trace_set_level_string(log_level.get_data());
  142. log_level_id = get_log_level_id(log_level.get_data());
  143. if (log_file) {
  144. OS::get_singleton()->print("Mono: Log file is: '%s'\n", log_file_path.utf8().get_data());
  145. mono_trace_set_log_handler(mono_log_callback, this);
  146. } else {
  147. OS::get_singleton()->printerr("Mono: No log file, using default log handler\n");
  148. }
  149. }
  150. GDMonoLog::GDMonoLog() {
  151. singleton = this;
  152. }
  153. GDMonoLog::~GDMonoLog() {
  154. singleton = nullptr;
  155. if (log_file) {
  156. log_file->close();
  157. memdelete(log_file);
  158. }
  159. }
  160. #else
  161. void GDMonoLog::initialize() {
  162. CharString log_level = get_default_log_level();
  163. mono_trace_set_level_string(log_level.get_data());
  164. }
  165. GDMonoLog::GDMonoLog() {
  166. singleton = this;
  167. }
  168. GDMonoLog::~GDMonoLog() {
  169. singleton = nullptr;
  170. }
  171. #endif // !defined(JAVASCRIPT_ENABLED)