test_logger.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**************************************************************************/
  2. /* test_logger.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef TEST_LOGGER_H
  31. #define TEST_LOGGER_H
  32. #include "core/io/dir_access.h"
  33. #include "core/io/logger.h"
  34. #include "modules/regex/regex.h"
  35. #include "tests/test_macros.h"
  36. namespace TestLogger {
  37. constexpr int sleep_duration = 1200000;
  38. void initialize_logs() {
  39. ProjectSettings::get_singleton()->set_setting("application/config/name", "godot_tests");
  40. DirAccess::make_dir_recursive_absolute(OS::get_singleton()->get_user_data_dir().path_join("logs"));
  41. }
  42. void cleanup_logs() {
  43. ProjectSettings::get_singleton()->set_setting("application/config/name", "godot_tests");
  44. Ref<DirAccess> dir = DirAccess::open("user://logs");
  45. dir->list_dir_begin();
  46. String file = dir->get_next();
  47. while (file != "") {
  48. if (file.match("*.log")) {
  49. dir->remove(file);
  50. }
  51. file = dir->get_next();
  52. }
  53. DirAccess::remove_absolute(OS::get_singleton()->get_user_data_dir().path_join("logs"));
  54. DirAccess::remove_absolute(OS::get_singleton()->get_user_data_dir());
  55. }
  56. TEST_CASE("[Logger][RotatedFileLogger] Creates the first log file and logs on it") {
  57. initialize_logs();
  58. String waiting_for_godot = "Waiting for Godot";
  59. RotatedFileLogger logger("user://logs/godot.log");
  60. logger.logf("%s", "Waiting for Godot");
  61. Error err = Error::OK;
  62. Ref<FileAccess> log = FileAccess::open("user://logs/godot.log", FileAccess::READ, &err);
  63. CHECK_EQ(err, Error::OK);
  64. CHECK_EQ(log->get_as_text(), waiting_for_godot);
  65. cleanup_logs();
  66. }
  67. void get_log_files(Vector<String> &log_files) {
  68. Ref<DirAccess> dir = DirAccess::open("user://logs");
  69. dir->list_dir_begin();
  70. String file = dir->get_next();
  71. while (file != "") {
  72. // Filtering godot.log because ordered_insert will put it first and should be the last.
  73. if (file.match("*.log") && file != "godot.log") {
  74. log_files.ordered_insert(file);
  75. }
  76. file = dir->get_next();
  77. }
  78. if (FileAccess::exists("user://logs/godot.log")) {
  79. log_files.push_back("godot.log");
  80. }
  81. }
  82. // All things related to log file rotation are in the same test because testing it require some sleeps.
  83. TEST_CASE("[Logger][RotatedFileLogger] Rotates logs files") {
  84. initialize_logs();
  85. Vector<String> all_waiting_for_godot;
  86. const int number_of_files = 3;
  87. for (int i = 0; i < number_of_files; i++) {
  88. String waiting_for_godot = "Waiting for Godot " + itos(i);
  89. RotatedFileLogger logger("user://logs/godot.log", number_of_files);
  90. logger.logf("%s", waiting_for_godot.ascii().get_data());
  91. all_waiting_for_godot.push_back(waiting_for_godot);
  92. // Required to ensure the rotation of the log file.
  93. OS::get_singleton()->delay_usec(sleep_duration);
  94. }
  95. Vector<String> log_files;
  96. get_log_files(log_files);
  97. CHECK_MESSAGE(log_files.size() == number_of_files, "Did not rotate all files");
  98. for (int i = 0; i < log_files.size(); i++) {
  99. Error err = Error::OK;
  100. Ref<FileAccess> log_file = FileAccess::open("user://logs/" + log_files[i], FileAccess::READ, &err);
  101. REQUIRE_EQ(err, Error::OK);
  102. CHECK_EQ(log_file->get_as_text(), all_waiting_for_godot[i]);
  103. }
  104. // Required to ensure the rotation of the log file.
  105. OS::get_singleton()->delay_usec(sleep_duration);
  106. // This time the oldest log must be removed and godot.log updated.
  107. String new_waiting_for_godot = "Waiting for Godot " + itos(number_of_files);
  108. all_waiting_for_godot = all_waiting_for_godot.slice(1, all_waiting_for_godot.size());
  109. all_waiting_for_godot.push_back(new_waiting_for_godot);
  110. RotatedFileLogger logger("user://logs/godot.log", number_of_files);
  111. logger.logf("%s", new_waiting_for_godot.ascii().get_data());
  112. log_files.clear();
  113. get_log_files(log_files);
  114. CHECK_MESSAGE(log_files.size() == number_of_files, "Did not remove old log file");
  115. for (int i = 0; i < log_files.size(); i++) {
  116. Error err = Error::OK;
  117. Ref<FileAccess> log_file = FileAccess::open("user://logs/" + log_files[i], FileAccess::READ, &err);
  118. REQUIRE_EQ(err, Error::OK);
  119. CHECK_EQ(log_file->get_as_text(), all_waiting_for_godot[i]);
  120. }
  121. cleanup_logs();
  122. }
  123. TEST_CASE("[Logger][CompositeLogger] Logs the same into multiple loggers") {
  124. initialize_logs();
  125. Vector<Logger *> all_loggers;
  126. all_loggers.push_back(memnew(RotatedFileLogger("user://logs/godot_logger_1.log", 1)));
  127. all_loggers.push_back(memnew(RotatedFileLogger("user://logs/godot_logger_2.log", 1)));
  128. String waiting_for_godot = "Waiting for Godot";
  129. CompositeLogger logger(all_loggers);
  130. logger.logf("%s", "Waiting for Godot");
  131. Error err = Error::OK;
  132. Ref<FileAccess> log = FileAccess::open("user://logs/godot_logger_1.log", FileAccess::READ, &err);
  133. CHECK_EQ(err, Error::OK);
  134. CHECK_EQ(log->get_as_text(), waiting_for_godot);
  135. log = FileAccess::open("user://logs/godot_logger_2.log", FileAccess::READ, &err);
  136. CHECK_EQ(err, Error::OK);
  137. CHECK_EQ(log->get_as_text(), waiting_for_godot);
  138. cleanup_logs();
  139. }
  140. } // namespace TestLogger
  141. #endif // TEST_LOGGER_H