Repository navigation
Expand file tree
/
Copy pathtest_logger.cpp
More file actions
280 lines (257 loc) · 10.1 KB
/
Copy pathtest_logger.cpp
File metadata and controls
280 lines (257 loc) · 10.1 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Logger class tests
*
* Tests the new RAII logger class: construction, level filtering,
* file output, timestamps, and destructor cleanup.
* Uses a temp file that is removed after each test group.
*/
#include "Logger.h"
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <print>
#include <sstream>
namespace fs = std::filesystem;
static const std::string LOG_FILE = "/tmp/ragger_test_logger.log";
static void cleanup() {
fs::remove(LOG_FILE);
}
static std::string read_file(const std::string& path) {
std::ifstream f(path);
std::ostringstream ss;
ss << f.rdbuf();
return ss.str();
}
int main() {
std::println("Running logger class tests:");
// 1. Constructor creates the log file
std::println(" test_file_created...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "info");
Diskerror::Logger::info("startup message");
}
assert(fs::exists(LOG_FILE));
auto content = read_file(LOG_FILE);
assert(content.find("startup message") != std::string::npos);
std::println(" OK");
// 2. Destructor closes the file (second logger can reopen it)
std::println(" test_destructor_closes_file...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "info");
Diskerror::Logger::info("first run");
}
{
Diskerror::Logger log(LOG_FILE, "info");
Diskerror::Logger::info("second run");
}
content = read_file(LOG_FILE);
assert(content.find("first run") != std::string::npos);
assert(content.find("second run") != std::string::npos);
std::println(" OK");
// 3. Level filtering — "warn" should suppress info and debug
std::println(" test_level_filtering_warn...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "warn");
Diskerror::Logger::debug("should not appear");
Diskerror::Logger::info("should not appear either");
Diskerror::Logger::warn("visible warning");
Diskerror::Logger::error("visible error");
}
content = read_file(LOG_FILE);
assert(content.find("should not appear") == std::string::npos);
assert(content.find("should not appear either") == std::string::npos);
assert(content.find("visible warning") != std::string::npos);
assert(content.find("visible error") != std::string::npos);
std::println(" OK");
// 4. Level filtering — "trace" enables everything
std::println(" test_level_filtering_trace...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "trace");
Diskerror::Logger::trace("t msg");
Diskerror::Logger::debug("d msg");
Diskerror::Logger::info("i msg");
Diskerror::Logger::warn("w msg");
Diskerror::Logger::error("e msg");
Diskerror::Logger::critical("c msg");
}
content = read_file(LOG_FILE);
assert(content.find("[TRACE]") != std::string::npos);
assert(content.find("[DEBUG]") != std::string::npos);
assert(content.find("[INFO ]") != std::string::npos);
assert(content.find("[WARN ]") != std::string::npos);
assert(content.find("[ERROR]") != std::string::npos);
assert(content.find("[CRITICAL]") != std::string::npos);
std::println(" OK");
// 5. Level filtering — "critical" suppresses everything below
std::println(" test_level_filtering_critical...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "critical");
Diskerror::Logger::trace("no");
Diskerror::Logger::debug("no");
Diskerror::Logger::info("no");
Diskerror::Logger::warn("no");
Diskerror::Logger::error("no");
Diskerror::Logger::critical("yes critical");
}
content = read_file(LOG_FILE);
assert(content.find("yes critical") != std::string::npos);
// Only one line should be present (the critical one)
// Count newlines — should be exactly 1
auto lines = std::count(content.begin(), content.end(), '\n');
assert(lines == 1);
std::println(" OK");
// 6. Timestamp format (YYYY-MM-DD HH:MM:SS.mmmm)
std::println(" test_timestamp_format...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "info");
Diskerror::Logger::info("timestamp check");
}
content = read_file(LOG_FILE);
assert(content.find("202") != std::string::npos);
auto dot_pos = content.find('.');
assert(dot_pos != std::string::npos);
// 4 digits after the dot (microseconds mod 10000), then space
assert(content[dot_pos + 5] == ' ');
std::println(" OK");
// 7. Log tags present in output
std::println(" test_log_tags...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "info");
Diskerror::Logger::info("tag test");
}
content = read_file(LOG_FILE);
assert(content.find("[INFO ]") != std::string::npos);
std::println(" OK");
// 8. Invalid level defaults to warn
std::println(" test_invalid_level_defaults_to_warn...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "nonsense");
Diskerror::Logger::info("should be hidden");
Diskerror::Logger::warn("should be visible");
}
content = read_file(LOG_FILE);
assert(content.find("should be hidden") == std::string::npos);
assert(content.find("should be visible") != std::string::npos);
std::println(" OK");
// 9. Multiple entries accumulate
std::println(" test_accumulation...");
cleanup();
{
Diskerror::Logger log(LOG_FILE, "info");
Diskerror::Logger::info("line one");
Diskerror::Logger::info("line two");
Diskerror::Logger::info("line three");
}
content = read_file(LOG_FILE);
assert(content.find("line one") != std::string::npos);
assert(content.find("line two") != std::string::npos);
assert(content.find("line three") != std::string::npos);
std::println(" OK");
// 10. Bad file path throws
std::println(" test_bad_path_throws...");
{
bool threw = false;
try {
Diskerror::Logger log("/no/such/directory/bad.log", "info");
} catch (const std::runtime_error& e) {
threw = true;
assert(std::string(e.what()).find("failed to open") != std::string::npos);
}
assert(threw);
}
std::println(" OK");
// 11. Constructor creates a missing containing directory (e.g. ~/.ragger/logs)
std::println(" test_creates_missing_directory...");
{
fs::remove_all("/tmp/ragger_test_logdir");
std::string nested = "/tmp/ragger_test_logdir/nested/activity.log";
assert(!fs::exists("/tmp/ragger_test_logdir"));
{
Diskerror::Logger log(nested, "info");
Diskerror::Logger::info("dir autocreate check");
}
assert(fs::exists(nested));
content = read_file(nested);
assert(content.find("dir autocreate check") != std::string::npos);
fs::remove_all("/tmp/ragger_test_logdir");
}
std::println(" OK");
// 12. Size-based rotation: once the file crosses max_size, it's renamed
// to a timestamped backup and a fresh empty file continues at the same path.
std::println(" test_size_based_rotation...");
{
std::string dir = "/tmp/ragger_test_rotate";
std::string path = dir + "/activity.log";
fs::remove_all(dir);
fs::create_directories(dir);
// Seed the file past the 1MB threshold so the logger's very first
// append (which checks size after writing) triggers rotation.
{
std::ofstream seed(path);
seed << std::string(1024 * 1024 + 10, 'x'); // > 1MB
}
{
Diskerror::Logger log(path, "info", /*maxSizeMb=*/1, /*maxAgeDays=*/0);
Diskerror::Logger::info("triggers rotation");
// Second write lands in the fresh post-rotation file.
Diskerror::Logger::info("post rotation");
}
int backups = 0;
std::string backup_path;
for (const auto& e : fs::directory_iterator(dir)) {
if (e.path().filename().string().rfind("activity.log.", 0) == 0) {
++backups;
backup_path = e.path().string();
}
}
assert(backups == 1);
// The pre-rotation content (seeded data + "triggers rotation") is in
// the backup; the fresh file has only "post rotation".
auto backup_content = read_file(backup_path);
assert(backup_content.find("triggers rotation") != std::string::npos);
content = read_file(path);
assert(content.find("post rotation") != std::string::npos);
assert(content.find("triggers rotation") == std::string::npos);
assert(content.size() < 1024 * 1024); // fresh file, not the seeded giant one
fs::remove_all(dir);
}
std::println(" OK");
// 13. Age-based cleanup: a backup older than max_age_days is deleted on
// the next rotation; one younger than the cutoff survives.
std::println(" test_age_based_cleanup...");
{
std::string dir = "/tmp/ragger_test_agesweep";
std::string path = dir + "/activity.log";
fs::remove_all(dir);
fs::create_directories(dir);
std::string old_backup = dir + "/activity.log.20200101-000000";
std::string young_backup = dir + "/activity.log.20990101-000000"; // far future, never "old"
{ std::ofstream f(old_backup); f << "old"; }
{ std::ofstream f(young_backup); f << "young"; }
// Backdate old_backup's mtime well past the 1-day cutoff used below.
auto ancient = std::chrono::file_clock::now() - std::chrono::hours(24 * 30);
fs::last_write_time(old_backup, ancient);
{
std::ofstream seed(path);
seed << std::string(1024 * 1024 + 10, 'x');
}
Diskerror::Logger log(path, "info", /*maxSizeMb=*/1, /*maxAgeDays=*/1);
Diskerror::Logger::info("triggers rotation + sweep");
assert(!fs::exists(old_backup)); // swept (30 days old, cutoff is 1 day)
assert(fs::exists(young_backup)); // untouched (2099 mtime, not old)
fs::remove_all(dir);
}
std::println(" OK");
cleanup();
std::println("test_logger: all passed");
return 0;
}