Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/file_logger_rotation_naming_sequence_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <LogIt.hpp>
#include <regex>
#include <string>
#include <fstream>
#include <cstdlib>

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<logit::FileLogger>(new logit::FileLogger(cfg)),
std::unique_ptr<logit::SimpleLogFormatter>(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;
}
44 changes: 44 additions & 0 deletions tests/file_logger_rotation_naming_timestamp_ms_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <LogIt.hpp>
#include <regex>
#include <string>
#include <vector>
#if __cplusplus >= 201703L
#include <filesystem>
#else
#include <cstdlib>
#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<logit::FileLogger>(new logit::FileLogger(cfg)),
std::unique_ptr<logit::SimpleLogFormatter>(new logit::SimpleLogFormatter("%v")));
const std::string msg = "0123456789";
LOGIT_INFO(msg);
LOGIT_INFO(msg);
LOGIT_WAIT();
LOGIT_SHUTDOWN();
std::vector<std::string> 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;
}
44 changes: 44 additions & 0 deletions tests/file_logger_rotation_naming_timestamp_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <LogIt.hpp>
#include <regex>
#include <string>
#include <vector>
#if __cplusplus >= 201703L
#include <filesystem>
#else
#include <cstdlib>
#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<logit::FileLogger>(new logit::FileLogger(cfg)),
std::unique_ptr<logit::SimpleLogFormatter>(new logit::SimpleLogFormatter("%v")));
const std::string msg = "0123456789";
LOGIT_INFO(msg);
LOGIT_INFO(msg);
LOGIT_WAIT();
LOGIT_SHUTDOWN();
std::vector<std::string> 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;
}
18 changes: 13 additions & 5 deletions tests/file_logger_rotation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading