gd_mono_log.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. static int log_level_get_id(const char *p_log_level) {
  37. const char *valid_log_levels[] = { "error", "critical", "warning", "message", "info", "debug", NULL };
  38. int i = 0;
  39. while (valid_log_levels[i]) {
  40. if (!strcmp(valid_log_levels[i], p_log_level))
  41. return i;
  42. i++;
  43. }
  44. return -1;
  45. }
  46. void gdmono_MonoLogCallback(const char *log_domain, const char *log_level, const char *message, mono_bool fatal, void *user_data) {
  47. FileAccess *f = GDMonoLog::get_singleton()->get_log_file();
  48. if (GDMonoLog::get_singleton()->get_log_level_id() >= log_level_get_id(log_level)) {
  49. String text(message);
  50. text += " (in domain ";
  51. text += log_domain;
  52. if (log_level) {
  53. text += ", ";
  54. text += log_level;
  55. }
  56. text += ")\n";
  57. f->seek_end();
  58. f->store_string(text);
  59. }
  60. if (fatal) {
  61. ERR_PRINTS("Mono: FATAL ERROR, ABORTING! Logfile: " + GDMonoLog::get_singleton()->get_log_file_path() + "\n");
  62. // If we were to abort without flushing, the log wouldn't get written.
  63. f->flush();
  64. abort();
  65. }
  66. }
  67. GDMonoLog *GDMonoLog::singleton = NULL;
  68. bool GDMonoLog::_try_create_logs_dir(const String &p_logs_dir) {
  69. if (!DirAccess::exists(p_logs_dir)) {
  70. DirAccessRef diraccess = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  71. ERR_FAIL_COND_V(!diraccess, false);
  72. Error logs_mkdir_err = diraccess->make_dir_recursive(p_logs_dir);
  73. ERR_EXPLAIN("Failed to create mono logs directory");
  74. ERR_FAIL_COND_V(logs_mkdir_err != OK, false);
  75. }
  76. return true;
  77. }
  78. void GDMonoLog::_open_log_file(const String &p_file_path) {
  79. log_file = FileAccess::open(p_file_path, FileAccess::WRITE);
  80. ERR_EXPLAIN("Failed to create log file");
  81. ERR_FAIL_COND(!log_file);
  82. }
  83. void GDMonoLog::_delete_old_log_files(const String &p_logs_dir) {
  84. static const uint64_t MAX_SECS = 5 * 86400;
  85. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  86. ERR_FAIL_COND(!da);
  87. Error err = da->change_dir(p_logs_dir);
  88. ERR_FAIL_COND(err != OK);
  89. ERR_FAIL_COND(da->list_dir_begin() != OK);
  90. String current;
  91. while ((current = da->get_next()).length()) {
  92. if (da->current_is_dir())
  93. continue;
  94. if (!current.ends_with(".txt"))
  95. continue;
  96. String name = current.get_basename();
  97. uint64_t unixtime = (uint64_t)name.to_int64();
  98. if (OS::get_singleton()->get_unix_time() - unixtime > MAX_SECS) {
  99. da->remove(current);
  100. }
  101. }
  102. da->list_dir_end();
  103. }
  104. void GDMonoLog::initialize() {
  105. #ifdef DEBUG_ENABLED
  106. const char *log_level = "debug";
  107. #else
  108. const char *log_level = "warning";
  109. #endif
  110. String logs_dir = GodotSharpDirs::get_mono_logs_dir();
  111. if (_try_create_logs_dir(logs_dir)) {
  112. _delete_old_log_files(logs_dir);
  113. log_file_path = logs_dir.plus_file(String::num_int64(OS::get_singleton()->get_unix_time()) + ".txt");
  114. _open_log_file(log_file_path);
  115. }
  116. mono_trace_set_level_string(log_level);
  117. log_level_id = log_level_get_id(log_level);
  118. if (log_file) {
  119. print_verbose("Mono: Logfile is " + log_file_path);
  120. mono_trace_set_log_handler(gdmono_MonoLogCallback, this);
  121. } else {
  122. OS::get_singleton()->printerr("Mono: No log file, using default log handler\n");
  123. }
  124. }
  125. GDMonoLog::GDMonoLog() {
  126. singleton = this;
  127. log_level_id = -1;
  128. }
  129. GDMonoLog::~GDMonoLog() {
  130. singleton = NULL;
  131. if (log_file) {
  132. log_file->close();
  133. memdelete(log_file);
  134. }
  135. }