test_logger.h 6.8 KB

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