#include #include #include #include #include #include #include #include #include #include #include #include using namespace Void::Logging; namespace { std::size_t archive_count(const std::filesystem::path& path) { const auto directory = path.parent_path() / "history"; if (!std::filesystem::exists(directory)) { return 0; } const auto prefix = path.stem().string() + "-"; std::size_t count = 0; for (const auto& entry : std::filesystem::directory_iterator(directory)) { const auto filename = entry.path().filename().string(); if (entry.is_regular_file() && filename.starts_with(prefix) && entry.path().extension() == path.extension()) { ++count; } } return count; } } // namespace // ── NullLogger / CallbackLogger // ─────────────────────────────────────────────── Void::Testing::Test null_logger_swallows_everything() { NullLogger log; log.fatal("into the void"); log.flush(); Void::Testing::expect(true); // reached without crashing } Void::Testing::Test callback_logger_receives_entry_fields() { Entry got; CallbackLogger log{[&](const Entry& e) { got = e; }, Mode::Immediate}; log.warning("payload"); Void::Testing::expect(got.level == Level::Warning); Void::Testing::expect_eq(got.message, std::string{"payload"}); } // ── MultiLogger // ─────────────────────────────────────────────────────────────── Void::Testing::Test multi_logger_fans_out_to_all_children() { int a = 0; int b = 0; MultiLogger multi; multi.add(std::make_shared([&](const Entry&) { ++a; }, Mode::Immediate)); multi.add(std::make_shared([&](const Entry&) { ++b; }, Mode::Immediate)); multi.info("to both"); Void::Testing::expect_eq(a, 1); Void::Testing::expect_eq(b, 1); Void::Testing::expect_eq(multi.child_count(), std::size_t{2}); } Void::Testing::Test multi_logger_children_keep_own_levels() { int verbose_count = 0; int errors_only = 0; auto verbose = std::make_shared( [&](const Entry&) { ++verbose_count; }, Mode::Immediate); auto strict = std::make_shared( [&](const Entry&) { ++errors_only; }, Mode::Immediate); strict->set_level(Level::Error); MultiLogger multi; multi.add(verbose); multi.add(strict); multi.info("info"); multi.error("error"); Void::Testing::expect_eq(verbose_count, 2); Void::Testing::expect_eq(errors_only, 1); } Void::Testing::Test multi_logger_relays_async_children_with_flush() { int delivered = 0; auto async_child = std::make_shared( [&](const Entry&) { ++delivered; }); // Async by default MultiLogger multi; multi.add(async_child); multi.info("queued in child"); multi.flush(); // flushes children too Void::Testing::expect_eq(delivered, 1); } // ── ConsoleLogger + Formatter // ───────────────────────────────────────────────── Void::Testing::Test console_logger_uses_installed_formatter() { std::ostringstream out; { ConsoleLogger log{out, Mode::Immediate}; log.set_formatter( std::make_shared("{level}>{message}")); log.info("custom format"); } Void::Testing::expect_eq(out.str(), std::string{"Info>custom format\n"}); } // ── RotatingFileLogger // ──────────────────────────────────────────────────────── class RotatingFileLoggerTests : public Void::Testing::Fixture { public: std::filesystem::path dir; std::filesystem::path file; void setup() override { dir = std::filesystem::temp_directory_path() / "void_rotating_test"; std::filesystem::remove_all(dir); std::filesystem::create_directories(dir); file = dir / "app.log"; } void teardown() override { std::filesystem::remove_all(dir); } static std::string slurp(const std::filesystem::path& p) { std::ifstream f(p); return {std::istreambuf_iterator(f), {}}; } Void::Testing::Test writes_and_flushes() { { RotatingFileLogger log{file}; log.info("rotating hello"); } Void::Testing::expect(slurp(file).find("rotating hello") != std::string::npos); } Void::Testing::Test rotates_when_size_exceeded() { { RotatingFileLogger::Options opt; opt.max_bytes = 256; opt.max_files = 3; opt.mode = Mode::Immediate; RotatingFileLogger log{file, opt}; for (int i = 0; i < 40; ++i) { log.info("a reasonably long line to fill the file " + std::to_string(i)); } } Void::Testing::expect(std::filesystem::exists(file)); const auto archives = archive_count(file); Void::Testing::expect(archives >= 1); Void::Testing::expect(archives <= 3); } Void::Testing::Test open_factory_rejects_bad_path() { const auto blocking_parent = dir / "not-a-directory"; { std::ofstream blocker{blocking_parent}; blocker << "file blocks directory creation"; } const auto result = RotatingFileLogger::open(blocking_parent / "app.log"); Void::Testing::expect(!result.has_value()); } Void::Testing::Test async_rotation_under_load() { { RotatingFileLogger::Options opt; opt.max_bytes = 512; opt.max_files = 2; RotatingFileLogger log{file, opt}; // Async default for (int i = 0; i < 200; ++i) { log.info("async rotation line " + std::to_string(i)); } log.flush(); } Void::Testing::expect(std::filesystem::exists(file)); } };