#include #include #include #include #include #include #include #include #include #include using namespace Void::Logging; namespace { std::string slurp(const std::filesystem::path& p) { std::ifstream f(p); return {std::istreambuf_iterator(f), {}}; } 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 class SplitFileLoggerTests : public Void::Testing::Fixture { public: std::filesystem::path dir; void setup() override { dir = std::filesystem::temp_directory_path() / "void_split_test"; std::filesystem::remove_all(dir); } void teardown() override { std::filesystem::remove_all(dir); } Void::Testing::Test routes_by_level() { { SplitFileLogger log{dir, {.mode = Void::Logging::Mode::Immediate}}; log.info("an info line"); log.error("an error line"); } Void::Testing::expect( slurp(dir / "info.log").find("an info line") != std::string::npos); Void::Testing::expect( slurp(dir / "error.log").find("an error line") != std::string::npos); // error.log must NOT contain the info line. Void::Testing::expect( slurp(dir / "error.log").find("an info line") == std::string::npos); } Void::Testing::Test combined_has_all_levels() { { SplitFileLogger log{dir, {.mode = Void::Logging::Mode::Immediate}}; log.info("info one"); log.error("error one"); } const auto all = slurp(dir / "trace.log"); Void::Testing::expect(all.find("info one") != std::string::npos); Void::Testing::expect(all.find("error one") != std::string::npos); } Void::Testing::Test combined_can_be_disabled() { { SplitFileLogger log{dir, {.combined = false, .mode = Void::Logging::Mode::Immediate}}; log.info("solo"); } Void::Testing::expect(!std::filesystem::exists(dir / "trace.log")); Void::Testing::expect(std::filesystem::exists(dir / "info.log")); } }; class HistoryLoggerTests : public Void::Testing::Fixture { public: std::filesystem::path dir; std::filesystem::path path; void setup() override { dir = std::filesystem::temp_directory_path() / "void_hist_test"; std::filesystem::remove_all(dir); std::filesystem::create_directories(dir); path = dir / "history.jsonl"; } void teardown() override { std::filesystem::remove_all(dir); } Void::Testing::Test writes_jsonlines() { { HistoryLogger log{path, {.mode = Void::Logging::Mode::Immediate}}; log.info("audit me"); } std::ifstream f(path); std::string line; std::getline(f, line); const auto parsed = Void::Json::parse(line); Void::Testing::expect(parsed.has_value()); Void::Testing::expect_eq((*parsed)["message"].as_string(), std::string{"audit me"}); } Void::Testing::Test captures_below_default_min_level() { { HistoryLogger log{path, {.mode = Void::Logging::Mode::Immediate}}; log.trace("low level but kept"); } Void::Testing::expect(slurp(path).find("low level but kept") != std::string::npos); } Void::Testing::Test rotates_on_size() { { HistoryLogger log{path, {.max_bytes = 256, .max_files = 3, .mode = Void::Logging::Mode::Immediate}}; for (int i = 0; i < 60; ++i) { log.info("a fairly long history line number " + std::to_string(i)); } } const auto archives = archive_count(path); Void::Testing::expect(archives >= 1); Void::Testing::expect(archives <= 3); } };