| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/dir_access.h" |
| 34 |
#include "core/io/logger.h" |
| 35 |
#include "modules/regex/regex.h" |
| 36 |
#include "tests/test_macros.h" |
| 37 |
|
| 38 |
namespace TestLogger { |
| 39 |
|
| 40 |
constexpr int sleep_duration = 1200000; |
| 41 |
|
| 42 |
void initialize_logs() { |
| 43 |
ProjectSettings::get_singleton()->set_setting("application/config/name", "godot_tests"); |
| 44 |
DirAccess::make_dir_recursive_absolute(OS::get_singleton()->get_user_data_dir().path_join("logs")); |
| 45 |
} |
| 46 |
|
| 47 |
void cleanup_logs() { |
| 48 |
ProjectSettings::get_singleton()->set_setting("application/config/name", "godot_tests"); |
| 49 |
Ref<DirAccess> dir = DirAccess::open("user://logs"); |
| 50 |
dir->list_dir_begin(); |
| 51 |
String file = dir->get_next(); |
| 52 |
while (file != "") { |
| 53 |
if (file.match("*.log")) { |
| 54 |
dir->remove(file); |
| 55 |
} |
| 56 |
file = dir->get_next(); |
| 57 |
} |
| 58 |
DirAccess::remove_absolute(OS::get_singleton()->get_user_data_dir().path_join("logs")); |
| 59 |
DirAccess::remove_absolute(OS::get_singleton()->get_user_data_dir()); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_CASE("[Logger][RotatedFileLogger] Creates the first log file and logs on it") { |
| 63 |
initialize_logs(); |
| 64 |
|
| 65 |
String waiting_for_godot = "Waiting for Godot"; |
| 66 |
RotatedFileLogger logger("user: |
| 67 |
logger.logf("%s", "Waiting for Godot"); |
| 68 |
|
| 69 |
Error err = Error::OK; |
| 70 |
Ref<FileAccess> log = FileAccess::open("user://logs/godot.log", FileAccess::READ, &err); |
| 71 |
CHECK_EQ(err, Error::OK); |
| 72 |
CHECK_EQ(log->get_as_text(), waiting_for_godot); |
| 73 |
|
| 74 |
cleanup_logs(); |
| 75 |
} |
| 76 |
|
| 77 |
void get_log_files(Vector<String> &log_files) { |
| 78 |
Ref<DirAccess> dir = DirAccess::open("user://logs"); |
| 79 |
dir->list_dir_begin(); |
| 80 |
String file = dir->get_next(); |
| 81 |
while (file != "") { |
| 82 |
|
| 83 |
if (file.match("*.log") && file != "godot.log") { |
| 84 |
log_files.ordered_insert(file); |
| 85 |
} |
| 86 |
file = dir->get_next(); |
| 87 |
} |
| 88 |
if (FileAccess::exists("user://logs/godot.log")) { |
| 89 |
log_files.push_back("godot.log"); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
TEST_CASE("[Logger][RotatedFileLogger] Rotates logs files") { |
| 95 |
initialize_logs(); |
| 96 |
|
| 97 |
Vector<String> all_waiting_for_godot; |
| 98 |
|
| 99 |
const int number_of_files = 3; |
| 100 |
for (int i = 0; i < number_of_files; i++) { |
| 101 |
String waiting_for_godot = "Waiting for Godot " + itos(i); |
| 102 |
RotatedFileLogger logger("user: |
| 103 |
logger.logf("%s", waiting_for_godot.ascii().get_data()); |
| 104 |
all_waiting_for_godot.push_back(waiting_for_godot); |
| 105 |
|
| 106 |
|
| 107 |
OS::get_singleton()->delay_usec(sleep_duration); |
| 108 |
} |
| 109 |
|
| 110 |
Vector<String> log_files; |
| 111 |
get_log_files(log_files); |
| 112 |
CHECK_MESSAGE(log_files.size() == number_of_files, "Did not rotate all files"); |
| 113 |
|
| 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 |
|
| 121 |
|
| 122 |
OS::get_singleton()->delay_usec(sleep_duration); |
| 123 |
|
| 124 |
|
| 125 |
String new_waiting_for_godot = "Waiting for Godot " + itos(number_of_files); |
| 126 |
all_waiting_for_godot = all_waiting_for_godot.slice(1, all_waiting_for_godot.size()); |
| 127 |
all_waiting_for_godot.push_back(new_waiting_for_godot); |
| 128 |
RotatedFileLogger logger("user: |
| 129 |
logger.logf("%s", new_waiting_for_godot.ascii().get_data()); |
| 130 |
|
| 131 |
log_files.clear(); |
| 132 |
get_log_files(log_files); |
| 133 |
CHECK_MESSAGE(log_files.size() == number_of_files, "Did not remove old log file"); |
| 134 |
|
| 135 |
for (int i = 0; i < log_files.size(); i++) { |
| 136 |
Error err = Error::OK; |
| 137 |
Ref<FileAccess> log_file = FileAccess::open("user://logs/" + log_files[i], FileAccess::READ, &err); |
| 138 |
REQUIRE_EQ(err, Error::OK); |
| 139 |
CHECK_EQ(log_file->get_as_text(), all_waiting_for_godot[i]); |
| 140 |
} |
| 141 |
|
| 142 |
cleanup_logs(); |
| 143 |
} |
| 144 |
|
| 145 |
TEST_CASE("[Logger][CompositeLogger] Logs the same into multiple loggers") { |
| 146 |
initialize_logs(); |
| 147 |
|
| 148 |
Vector<Logger *> all_loggers; |
| 149 |
all_loggers.push_back(memnew(RotatedFileLogger("user://logs/godot_logger_1.log", 1))); |
| 150 |
all_loggers.push_back(memnew(RotatedFileLogger("user://logs/godot_logger_2.log", 1))); |
| 151 |
|
| 152 |
String waiting_for_godot = "Waiting for Godot"; |
| 153 |
CompositeLogger logger(all_loggers); |
| 154 |
logger.logf("%s", "Waiting for Godot"); |
| 155 |
|
| 156 |
Error err = Error::OK; |
| 157 |
Ref<FileAccess> log = FileAccess::open("user://logs/godot_logger_1.log", FileAccess::READ, &err); |
| 158 |
CHECK_EQ(err, Error::OK); |
| 159 |
CHECK_EQ(log->get_as_text(), waiting_for_godot); |
| 160 |
log = FileAccess::open("user://logs/godot_logger_2.log", FileAccess::READ, &err); |
| 161 |
CHECK_EQ(err, Error::OK); |
| 162 |
CHECK_EQ(log->get_as_text(), waiting_for_godot); |
| 163 |
|
| 164 |
cleanup_logs(); |
| 165 |
} |
| 166 |
|
| 167 |
} |