Skip to content

Commit e00bfbe

Browse files
authored
test(file-logger): add rotation naming tests
2 parents e11cb75 + 26afe0f commit e00bfbe

4 files changed

Lines changed: 132 additions & 5 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <LogIt.hpp>
2+
#include <regex>
3+
#include <string>
4+
#include <fstream>
5+
#include <cstdlib>
6+
7+
int main() {
8+
std::system("rm -rf rotation_seq");
9+
logit::FileLogger::Config cfg;
10+
cfg.directory = "rotation_seq";
11+
cfg.max_file_size_bytes = 20;
12+
cfg.naming = logit::RotationNaming::Sequence;
13+
logit::Logger::get_instance().add_logger(
14+
std::unique_ptr<logit::FileLogger>(new logit::FileLogger(cfg)),
15+
std::unique_ptr<logit::SimpleLogFormatter>(new logit::SimpleLogFormatter("%v")));
16+
const std::string msg = "0123456789";
17+
LOGIT_INFO(msg);
18+
LOGIT_INFO(msg);
19+
LOGIT_WAIT();
20+
std::string current = LOGIT_GET_LAST_FILE_PATH(0);
21+
LOGIT_SHUTDOWN();
22+
size_t pos = current.rfind(".log");
23+
if (pos == std::string::npos) return 1;
24+
std::string rotated = current;
25+
rotated.insert(pos, ".001");
26+
std::string name = rotated.substr(rotated.find_last_of("/\\") + 1);
27+
std::regex re("\\d{4}-\\d{2}-\\d{2}\\.001\\.log");
28+
if (!std::regex_match(name, re)) return 1;
29+
std::ifstream f(rotated.c_str());
30+
return f.good() ? 0 : 1;
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <LogIt.hpp>
2+
#include <regex>
3+
#include <string>
4+
#include <vector>
5+
#if __cplusplus >= 201703L
6+
#include <filesystem>
7+
#else
8+
#include <cstdlib>
9+
#endif
10+
11+
int main() {
12+
#if __cplusplus >= 201703L
13+
std::filesystem::remove_all(logit::get_exec_dir() + "/rotation_tms");
14+
#else
15+
#ifdef _WIN32
16+
std::string clean = logit::get_exec_dir() + "\\rotation_tms";
17+
std::string cmd = "rmdir /s /q \"" + clean + "\"";
18+
#else
19+
std::string clean = logit::get_exec_dir() + "/rotation_tms";
20+
std::string cmd = "rm -rf \"" + clean + "\"";
21+
#endif
22+
std::system(cmd.c_str());
23+
#endif
24+
const std::string dir = logit::get_exec_dir() + "/rotation_tms";
25+
logit::FileLogger::Config cfg;
26+
cfg.directory = "rotation_tms";
27+
cfg.max_file_size_bytes = 20;
28+
cfg.naming = logit::RotationNaming::TimestampMs;
29+
logit::Logger::get_instance().add_logger(
30+
std::unique_ptr<logit::FileLogger>(new logit::FileLogger(cfg)),
31+
std::unique_ptr<logit::SimpleLogFormatter>(new logit::SimpleLogFormatter("%v")));
32+
const std::string msg = "0123456789";
33+
LOGIT_INFO(msg);
34+
LOGIT_INFO(msg);
35+
LOGIT_WAIT();
36+
LOGIT_SHUTDOWN();
37+
std::vector<std::string> files = logit::get_list_files(dir);
38+
std::regex re("\\d{4}-\\d{2}-\\d{2}_\\d{9}(?:\\.\\d+)?\\.log");
39+
for (const auto& path : files) {
40+
std::string name = path.substr(path.find_last_of("/\\") + 1);
41+
if (std::regex_match(name, re)) return 0;
42+
}
43+
return 1;
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <LogIt.hpp>
2+
#include <regex>
3+
#include <string>
4+
#include <vector>
5+
#if __cplusplus >= 201703L
6+
#include <filesystem>
7+
#else
8+
#include <cstdlib>
9+
#endif
10+
11+
int main() {
12+
#if __cplusplus >= 201703L
13+
std::filesystem::remove_all(logit::get_exec_dir() + "/rotation_ts");
14+
#else
15+
#ifdef _WIN32
16+
std::string clean = logit::get_exec_dir() + "\\rotation_ts";
17+
std::string cmd = "rmdir /s /q \"" + clean + "\"";
18+
#else
19+
std::string clean = logit::get_exec_dir() + "/rotation_ts";
20+
std::string cmd = "rm -rf \"" + clean + "\"";
21+
#endif
22+
std::system(cmd.c_str());
23+
#endif
24+
const std::string dir = logit::get_exec_dir() + "/rotation_ts";
25+
logit::FileLogger::Config cfg;
26+
cfg.directory = "rotation_ts";
27+
cfg.max_file_size_bytes = 20;
28+
cfg.naming = logit::RotationNaming::Timestamp;
29+
logit::Logger::get_instance().add_logger(
30+
std::unique_ptr<logit::FileLogger>(new logit::FileLogger(cfg)),
31+
std::unique_ptr<logit::SimpleLogFormatter>(new logit::SimpleLogFormatter("%v")));
32+
const std::string msg = "0123456789";
33+
LOGIT_INFO(msg);
34+
LOGIT_INFO(msg);
35+
LOGIT_WAIT();
36+
LOGIT_SHUTDOWN();
37+
std::vector<std::string> files = logit::get_list_files(dir);
38+
std::regex re("\\d{4}-\\d{2}-\\d{2}_\\d{6}(?:\\.\\d+)?\\.log");
39+
for (const auto& path : files) {
40+
std::string name = path.substr(path.find_last_of("/\\") + 1);
41+
if (std::regex_match(name, re)) return 0;
42+
}
43+
return 1;
44+
}

tests/file_logger_rotation_test.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,26 @@ int main() {
1212
LOGIT_INFO(msg);
1313
LOGIT_INFO(msg);
1414
LOGIT_WAIT();
15+
1516
#if __cplusplus >= 201703L
1617
std::filesystem::path current = LOGIT_GET_LAST_FILE_PATH(0);
1718
std::filesystem::path rotated = current;
1819
rotated.replace_filename(current.stem().string() + ".001.log");
19-
return std::filesystem::exists(rotated) ? 0 : 1;
20+
LOGIT_SHUTDOWN();
21+
bool exists = std::filesystem::exists(rotated);
2022
#else
2123
std::string current = LOGIT_GET_LAST_FILE_PATH(0);
2224
std::string rotated = current;
2325
size_t pos = rotated.rfind(".log");
24-
if (pos == std::string::npos) return 1;
25-
rotated.insert(pos, ".001");
26-
std::ifstream f(rotated.c_str());
27-
return f.good() ? 0 : 1;
26+
if (pos != std::string::npos) {
27+
rotated.insert(pos, ".001");
28+
}
29+
LOGIT_SHUTDOWN();
30+
bool exists = false;
31+
if (pos != std::string::npos) {
32+
std::ifstream f(rotated.c_str());
33+
exists = f.good();
34+
}
2835
#endif
36+
return exists ? 0 : 1;
2937
}

0 commit comments

Comments
 (0)