Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_logger.h
10278 views
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
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://logs/godot.log");
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
// Filtering godot.log because ordered_insert will put it first and should be the last.
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
// All things related to log file rotation are in the same test because testing it require some sleeps.
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://logs/godot.log", number_of_files);
103
logger.logf("%s", waiting_for_godot.ascii().get_data());
104
all_waiting_for_godot.push_back(waiting_for_godot);
105
106
// Required to ensure the rotation of the log file.
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
// Required to ensure the rotation of the log file.
122
OS::get_singleton()->delay_usec(sleep_duration);
123
124
// This time the oldest log must be removed and godot.log updated.
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://logs/godot.log", number_of_files);
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
} // namespace TestLogger
168
169