gd_mono_log.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/dir_access.h"
  34. #include "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: FALTAL ERROR, ABORTING! Logfile: " + GDMonoLog::get_singleton()->get_log_file_path() + "\n");
  62. abort();
  63. }
  64. }
  65. GDMonoLog *GDMonoLog::singleton = NULL;
  66. bool GDMonoLog::_try_create_logs_dir(const String &p_logs_dir) {
  67. if (!DirAccess::exists(p_logs_dir)) {
  68. DirAccessRef diraccess = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  69. ERR_FAIL_COND_V(!diraccess, false);
  70. Error logs_mkdir_err = diraccess->make_dir_recursive(p_logs_dir);
  71. ERR_EXPLAIN("Failed to create mono logs directory");
  72. ERR_FAIL_COND_V(logs_mkdir_err != OK, false);
  73. }
  74. return true;
  75. }
  76. void GDMonoLog::_open_log_file(const String &p_file_path) {
  77. log_file = FileAccess::open(p_file_path, FileAccess::WRITE);
  78. ERR_EXPLAIN("Failed to create log file");
  79. ERR_FAIL_COND(!log_file);
  80. }
  81. void GDMonoLog::_delete_old_log_files(const String &p_logs_dir) {
  82. static const uint64_t MAX_SECS = 5 * 86400;
  83. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  84. ERR_FAIL_COND(!da);
  85. Error err = da->change_dir(p_logs_dir);
  86. ERR_FAIL_COND(err != OK);
  87. ERR_FAIL_COND(da->list_dir_begin() != OK);
  88. String current;
  89. while ((current = da->get_next()).length()) {
  90. if (da->current_is_dir())
  91. continue;
  92. if (!current.ends_with(".txt"))
  93. continue;
  94. String name = current.get_basename();
  95. uint64_t unixtime = (uint64_t)name.to_int64();
  96. if (OS::get_singleton()->get_unix_time() - unixtime > MAX_SECS) {
  97. da->remove(current);
  98. }
  99. }
  100. da->list_dir_end();
  101. }
  102. void GDMonoLog::initialize() {
  103. #ifdef DEBUG_ENABLED
  104. const char *log_level = "debug";
  105. #else
  106. const char *log_level = "warning";
  107. #endif
  108. String logs_dir = GodotSharpDirs::get_mono_logs_dir();
  109. if (_try_create_logs_dir(logs_dir)) {
  110. _delete_old_log_files(logs_dir);
  111. log_file_path = logs_dir.plus_file(String::num_int64(OS::get_singleton()->get_unix_time()) + ".txt");
  112. _open_log_file(log_file_path);
  113. }
  114. mono_trace_set_level_string(log_level);
  115. log_level_id = log_level_get_id(log_level);
  116. if (log_file) {
  117. if (OS::get_singleton()->is_stdout_verbose())
  118. OS::get_singleton()->print(String("Mono: Logfile is " + log_file_path + "\n").utf8());
  119. mono_trace_set_log_handler(gdmono_MonoLogCallback, this);
  120. } else {
  121. OS::get_singleton()->printerr("Mono: No log file, using default log handler\n");
  122. }
  123. }
  124. GDMonoLog::GDMonoLog() {
  125. singleton = this;
  126. log_level_id = -1;
  127. }
  128. GDMonoLog::~GDMonoLog() {
  129. singleton = NULL;
  130. if (log_file) {
  131. log_file->close();
  132. memdelete(log_file);
  133. }
  134. }