gd_mono_log.cpp 6.2 KB

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