From f0229eb42d592de214487a8ca3ac16a750fe0772 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sun, 14 Sep 2025 21:11:39 +0300 Subject: [PATCH 1/6] test(file-logger): add rotation naming tests --- ...e_logger_rotation_naming_sequence_test.cpp | 31 +++++++++++++++++++ ...gger_rotation_naming_timestamp_ms_test.cpp | 30 ++++++++++++++++++ ..._logger_rotation_naming_timestamp_test.cpp | 30 ++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 tests/file_logger_rotation_naming_sequence_test.cpp create mode 100644 tests/file_logger_rotation_naming_timestamp_ms_test.cpp create mode 100644 tests/file_logger_rotation_naming_timestamp_test.cpp diff --git a/tests/file_logger_rotation_naming_sequence_test.cpp b/tests/file_logger_rotation_naming_sequence_test.cpp new file mode 100644 index 0000000..34d9c7b --- /dev/null +++ b/tests/file_logger_rotation_naming_sequence_test.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +int main() { + std::system("rm -rf rotation_seq"); + logit::FileLogger::Config cfg; + cfg.directory = "rotation_seq"; + cfg.max_file_size_bytes = 20; + cfg.naming = logit::RotationNaming::Sequence; + logit::Logger::get_instance().add_logger( + std::unique_ptr(new logit::FileLogger(cfg)), + std::unique_ptr(new logit::SimpleLogFormatter("%v"))); + const std::string msg = "0123456789"; + LOGIT_INFO(msg); + LOGIT_INFO(msg); + LOGIT_WAIT(); + std::string current = LOGIT_GET_LAST_FILE_PATH(0); + LOGIT_SHUTDOWN(); + size_t pos = current.rfind(".log"); + if (pos == std::string::npos) return 1; + std::string rotated = current; + rotated.insert(pos, ".001"); + std::string name = rotated.substr(rotated.find_last_of("/\\") + 1); + std::regex re("\\d{4}-\\d{2}-\\d{2}\\.001\\.log"); + if (!std::regex_match(name, re)) return 1; + std::ifstream f(rotated.c_str()); + return f.good() ? 0 : 1; +} diff --git a/tests/file_logger_rotation_naming_timestamp_ms_test.cpp b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp new file mode 100644 index 0000000..5808151 --- /dev/null +++ b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +int main() { + std::system("rm -rf rotation_tms"); + logit::FileLogger::Config cfg; + cfg.directory = "rotation_tms"; + cfg.max_file_size_bytes = 20; + cfg.naming = logit::RotationNaming::TimestampMs; + logit::Logger::get_instance().add_logger( + std::unique_ptr(new logit::FileLogger(cfg)), + std::unique_ptr(new logit::SimpleLogFormatter("%v"))); + const std::string msg = "0123456789"; + LOGIT_INFO(msg); + LOGIT_INFO(msg); + LOGIT_WAIT(); + std::string current = LOGIT_GET_LAST_FILE_PATH(0); + std::string base_dir = current.substr(0, current.find_last_of("/\\")); + std::vector files = logit::get_list_files(base_dir); + LOGIT_SHUTDOWN(); + std::regex re("\\d{4}-\\d{2}-\\d{2}_\\d{9}(?:\\.\\d+)?\\.log"); + for (const auto& path : files) { + std::string name = path.substr(path.find_last_of("/\\") + 1); + if (std::regex_match(name, re)) return 0; + } + return 1; +} diff --git a/tests/file_logger_rotation_naming_timestamp_test.cpp b/tests/file_logger_rotation_naming_timestamp_test.cpp new file mode 100644 index 0000000..5ca32ea --- /dev/null +++ b/tests/file_logger_rotation_naming_timestamp_test.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +int main() { + std::system("rm -rf rotation_ts"); + logit::FileLogger::Config cfg; + cfg.directory = "rotation_ts"; + cfg.max_file_size_bytes = 20; + cfg.naming = logit::RotationNaming::Timestamp; + logit::Logger::get_instance().add_logger( + std::unique_ptr(new logit::FileLogger(cfg)), + std::unique_ptr(new logit::SimpleLogFormatter("%v"))); + const std::string msg = "0123456789"; + LOGIT_INFO(msg); + LOGIT_INFO(msg); + LOGIT_WAIT(); + std::string current = LOGIT_GET_LAST_FILE_PATH(0); + std::string base_dir = current.substr(0, current.find_last_of("/\\")); + std::vector files = logit::get_list_files(base_dir); + LOGIT_SHUTDOWN(); + std::regex re("\\d{4}-\\d{2}-\\d{2}_\\d{6}(?:\\.\\d+)?\\.log"); + for (const auto& path : files) { + std::string name = path.substr(path.find_last_of("/\\") + 1); + if (std::regex_match(name, re)) return 0; + } + return 1; +} From a8f5ef1e7c68e37e3fbe412d83b5f369271470c9 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sun, 14 Sep 2025 21:36:50 +0300 Subject: [PATCH 2/6] test(file-logger): shut down rotation test Store rotated file existence before shutting down the async logger to avoid macOS segfault. --- tests/file_logger_rotation_test.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/file_logger_rotation_test.cpp b/tests/file_logger_rotation_test.cpp index ad2c9e5..3b8e675 100644 --- a/tests/file_logger_rotation_test.cpp +++ b/tests/file_logger_rotation_test.cpp @@ -16,14 +16,18 @@ int main() { std::filesystem::path current = LOGIT_GET_LAST_FILE_PATH(0); std::filesystem::path rotated = current; rotated.replace_filename(current.stem().string() + ".001.log"); - return std::filesystem::exists(rotated) ? 0 : 1; + bool exists = std::filesystem::exists(rotated); #else std::string current = LOGIT_GET_LAST_FILE_PATH(0); std::string rotated = current; size_t pos = rotated.rfind(".log"); - if (pos == std::string::npos) return 1; - rotated.insert(pos, ".001"); - std::ifstream f(rotated.c_str()); - return f.good() ? 0 : 1; + bool exists = false; + if (pos != std::string::npos) { + rotated.insert(pos, ".001"); + std::ifstream f(rotated.c_str()); + exists = f.good(); + } #endif + LOGIT_SHUTDOWN(); + return exists ? 0 : 1; } From e9493a1a1c116723c4fd49827dc13b88cb4a7205 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sun, 14 Sep 2025 21:52:51 +0300 Subject: [PATCH 3/6] test(file-logger): ensure rotation checks after shutdown --- tests/file_logger_rotation_naming_timestamp_ms_test.cpp | 5 ++--- tests/file_logger_rotation_test.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/file_logger_rotation_naming_timestamp_ms_test.cpp b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp index 5808151..01b3bbd 100644 --- a/tests/file_logger_rotation_naming_timestamp_ms_test.cpp +++ b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp @@ -10,6 +10,7 @@ int main() { cfg.directory = "rotation_tms"; cfg.max_file_size_bytes = 20; cfg.naming = logit::RotationNaming::TimestampMs; + const std::string dir = cfg.directory; logit::Logger::get_instance().add_logger( std::unique_ptr(new logit::FileLogger(cfg)), std::unique_ptr(new logit::SimpleLogFormatter("%v"))); @@ -17,10 +18,8 @@ int main() { LOGIT_INFO(msg); LOGIT_INFO(msg); LOGIT_WAIT(); - std::string current = LOGIT_GET_LAST_FILE_PATH(0); - std::string base_dir = current.substr(0, current.find_last_of("/\\")); - std::vector files = logit::get_list_files(base_dir); LOGIT_SHUTDOWN(); + std::vector files = logit::get_list_files(dir); std::regex re("\\d{4}-\\d{2}-\\d{2}_\\d{9}(?:\\.\\d+)?\\.log"); for (const auto& path : files) { std::string name = path.substr(path.find_last_of("/\\") + 1); diff --git a/tests/file_logger_rotation_test.cpp b/tests/file_logger_rotation_test.cpp index 3b8e675..6725797 100644 --- a/tests/file_logger_rotation_test.cpp +++ b/tests/file_logger_rotation_test.cpp @@ -12,22 +12,26 @@ int main() { LOGIT_INFO(msg); LOGIT_INFO(msg); LOGIT_WAIT(); + #if __cplusplus >= 201703L std::filesystem::path current = LOGIT_GET_LAST_FILE_PATH(0); std::filesystem::path rotated = current; rotated.replace_filename(current.stem().string() + ".001.log"); + LOGIT_SHUTDOWN(); bool exists = std::filesystem::exists(rotated); #else std::string current = LOGIT_GET_LAST_FILE_PATH(0); std::string rotated = current; size_t pos = rotated.rfind(".log"); - bool exists = false; if (pos != std::string::npos) { rotated.insert(pos, ".001"); + } + LOGIT_SHUTDOWN(); + bool exists = false; + if (pos != std::string::npos) { std::ifstream f(rotated.c_str()); exists = f.good(); } #endif - LOGIT_SHUTDOWN(); return exists ? 0 : 1; } From 5389f715faba9880b2a773830c7f5d24081262d4 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sun, 14 Sep 2025 22:02:07 +0300 Subject: [PATCH 4/6] test(file-logger): flush timestamp rotation before check --- tests/file_logger_rotation_naming_timestamp_test.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/file_logger_rotation_naming_timestamp_test.cpp b/tests/file_logger_rotation_naming_timestamp_test.cpp index 5ca32ea..8cfe07d 100644 --- a/tests/file_logger_rotation_naming_timestamp_test.cpp +++ b/tests/file_logger_rotation_naming_timestamp_test.cpp @@ -10,6 +10,7 @@ int main() { cfg.directory = "rotation_ts"; cfg.max_file_size_bytes = 20; cfg.naming = logit::RotationNaming::Timestamp; + const std::string dir = cfg.directory; logit::Logger::get_instance().add_logger( std::unique_ptr(new logit::FileLogger(cfg)), std::unique_ptr(new logit::SimpleLogFormatter("%v"))); @@ -17,10 +18,8 @@ int main() { LOGIT_INFO(msg); LOGIT_INFO(msg); LOGIT_WAIT(); - std::string current = LOGIT_GET_LAST_FILE_PATH(0); - std::string base_dir = current.substr(0, current.find_last_of("/\\")); - std::vector files = logit::get_list_files(base_dir); LOGIT_SHUTDOWN(); + std::vector files = logit::get_list_files(dir); std::regex re("\\d{4}-\\d{2}-\\d{2}_\\d{6}(?:\\.\\d+)?\\.log"); for (const auto& path : files) { std::string name = path.substr(path.find_last_of("/\\") + 1); From 189835fa334c7398941763b439cc9a8694346048 Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sun, 14 Sep 2025 22:27:15 +0300 Subject: [PATCH 5/6] test(file-logger): fix timestamp rotation paths --- tests/file_logger_rotation_naming_timestamp_ms_test.cpp | 6 +++--- tests/file_logger_rotation_naming_timestamp_test.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/file_logger_rotation_naming_timestamp_ms_test.cpp b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp index 01b3bbd..bac2ddf 100644 --- a/tests/file_logger_rotation_naming_timestamp_ms_test.cpp +++ b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp @@ -2,15 +2,15 @@ #include #include #include -#include +#include int main() { - std::system("rm -rf rotation_tms"); + std::filesystem::remove_all(logit::get_exec_dir() + "/rotation_tms"); + const std::string dir = logit::get_exec_dir() + "/rotation_tms"; logit::FileLogger::Config cfg; cfg.directory = "rotation_tms"; cfg.max_file_size_bytes = 20; cfg.naming = logit::RotationNaming::TimestampMs; - const std::string dir = cfg.directory; logit::Logger::get_instance().add_logger( std::unique_ptr(new logit::FileLogger(cfg)), std::unique_ptr(new logit::SimpleLogFormatter("%v"))); diff --git a/tests/file_logger_rotation_naming_timestamp_test.cpp b/tests/file_logger_rotation_naming_timestamp_test.cpp index 8cfe07d..7092b22 100644 --- a/tests/file_logger_rotation_naming_timestamp_test.cpp +++ b/tests/file_logger_rotation_naming_timestamp_test.cpp @@ -2,15 +2,15 @@ #include #include #include -#include +#include int main() { - std::system("rm -rf rotation_ts"); + std::filesystem::remove_all(logit::get_exec_dir() + "/rotation_ts"); + const std::string dir = logit::get_exec_dir() + "/rotation_ts"; logit::FileLogger::Config cfg; cfg.directory = "rotation_ts"; cfg.max_file_size_bytes = 20; cfg.naming = logit::RotationNaming::Timestamp; - const std::string dir = cfg.directory; logit::Logger::get_instance().add_logger( std::unique_ptr(new logit::FileLogger(cfg)), std::unique_ptr(new logit::SimpleLogFormatter("%v"))); From 26afe0fe23d09ad3fb228e13b088d8d8c50dce4d Mon Sep 17 00:00:00 2001 From: Aster Seker Date: Sun, 14 Sep 2025 22:40:08 +0300 Subject: [PATCH 6/6] test(file-logger): guard timestamp tests for c++11 --- ...e_logger_rotation_naming_timestamp_ms_test.cpp | 15 +++++++++++++++ ...file_logger_rotation_naming_timestamp_test.cpp | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/tests/file_logger_rotation_naming_timestamp_ms_test.cpp b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp index bac2ddf..0b68f76 100644 --- a/tests/file_logger_rotation_naming_timestamp_ms_test.cpp +++ b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp @@ -2,10 +2,25 @@ #include #include #include +#if __cplusplus >= 201703L #include +#else +#include +#endif int main() { +#if __cplusplus >= 201703L std::filesystem::remove_all(logit::get_exec_dir() + "/rotation_tms"); +#else +#ifdef _WIN32 + std::string clean = logit::get_exec_dir() + "\\rotation_tms"; + std::string cmd = "rmdir /s /q \"" + clean + "\""; +#else + std::string clean = logit::get_exec_dir() + "/rotation_tms"; + std::string cmd = "rm -rf \"" + clean + "\""; +#endif + std::system(cmd.c_str()); +#endif const std::string dir = logit::get_exec_dir() + "/rotation_tms"; logit::FileLogger::Config cfg; cfg.directory = "rotation_tms"; diff --git a/tests/file_logger_rotation_naming_timestamp_test.cpp b/tests/file_logger_rotation_naming_timestamp_test.cpp index 7092b22..ed6e442 100644 --- a/tests/file_logger_rotation_naming_timestamp_test.cpp +++ b/tests/file_logger_rotation_naming_timestamp_test.cpp @@ -2,10 +2,25 @@ #include #include #include +#if __cplusplus >= 201703L #include +#else +#include +#endif int main() { +#if __cplusplus >= 201703L std::filesystem::remove_all(logit::get_exec_dir() + "/rotation_ts"); +#else +#ifdef _WIN32 + std::string clean = logit::get_exec_dir() + "\\rotation_ts"; + std::string cmd = "rmdir /s /q \"" + clean + "\""; +#else + std::string clean = logit::get_exec_dir() + "/rotation_ts"; + std::string cmd = "rm -rf \"" + clean + "\""; +#endif + std::system(cmd.c_str()); +#endif const std::string dir = logit::get_exec_dir() + "/rotation_ts"; logit::FileLogger::Config cfg; cfg.directory = "rotation_ts";