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