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..0b68f76 --- /dev/null +++ b/tests/file_logger_rotation_naming_timestamp_ms_test.cpp @@ -0,0 +1,44 @@ +#include +#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"; + 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(); + 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); + 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..ed6e442 --- /dev/null +++ b/tests/file_logger_rotation_naming_timestamp_test.cpp @@ -0,0 +1,44 @@ +#include +#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"; + 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(); + 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); + if (std::regex_match(name, re)) return 0; + } + return 1; +} diff --git a/tests/file_logger_rotation_test.cpp b/tests/file_logger_rotation_test.cpp index ad2c9e5..6725797 100644 --- a/tests/file_logger_rotation_test.cpp +++ b/tests/file_logger_rotation_test.cpp @@ -12,18 +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"); - return std::filesystem::exists(rotated) ? 0 : 1; + 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"); - if (pos == std::string::npos) return 1; - rotated.insert(pos, ".001"); - std::ifstream f(rotated.c_str()); - return f.good() ? 0 : 1; + 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 + return exists ? 0 : 1; }