// Pure test of the developer log (debug/log.hpp): level + category filtering, ring-buffer wrap, and the // SC2K_LOG macro. The Logger is a process-wide singleton, so each section clears it first. #include "sc2k/debug/log.hpp" #include #include using namespace sc2k::debug; #include "test_harness.hpp" static bool has(const std::string& s, const std::string& sub) { return s.find(sub) != std::string::npos; } int main() { Logger& lg = Logger::instance(); lg.set_mirror(false); // keep the test quiet // Basic record: line carries the category + level + message. lg.clear(); lg.set_threshold(LogLevel::Trace); lg.set_mask(0xFFFFFFFFu); lg.log(LogCat::Sim, LogLevel::Info, "one"); lg.log(LogCat::Power, LogLevel::Trace, "two"); CHECK(lg.size() == 2); CHECK(has(lg.line(0), "Sim") && has(lg.line(0), "one")); // oldest-first CHECK(has(lg.line(1), "Power") && has(lg.line(1), "two")); // Level threshold: at Warn, Error+Warn pass, Info+Trace are dropped. lg.clear(); lg.set_threshold(LogLevel::Warn); lg.log(LogCat::Sim, LogLevel::Error, "e"); lg.log(LogCat::Sim, LogLevel::Warn, "w"); lg.log(LogCat::Sim, LogLevel::Info, "i"); lg.log(LogCat::Sim, LogLevel::Trace, "t"); CHECK(lg.size() == 2); // Category mask: only enabled categories are recorded. lg.clear(); lg.set_threshold(LogLevel::Trace); lg.set_mask(0); lg.enable(LogCat::Coverage, true); CHECK(lg.enabled(LogCat::Coverage) && !lg.enabled(LogCat::Sim)); lg.log(LogCat::Sim, LogLevel::Info, "drop"); lg.log(LogCat::Coverage, LogLevel::Info, "keep"); CHECK(lg.size() == 1 && has(lg.line(0), "keep")); // cycle_threshold: Error -> Warn -> Info -> Trace -> Error. lg.set_threshold(LogLevel::Error); lg.cycle_threshold(); CHECK(lg.threshold() == LogLevel::Warn); lg.cycle_threshold(); CHECK(lg.threshold() == LogLevel::Info); lg.cycle_threshold(); CHECK(lg.threshold() == LogLevel::Trace); lg.cycle_threshold(); CHECK(lg.threshold() == LogLevel::Error); // Ring wrap: pushing more than kRing keeps only the newest kRing, oldest dropped. lg.clear(); lg.set_mask(0xFFFFFFFFu); lg.set_threshold(LogLevel::Trace); const int extra = 50; for (int i = 0; i < Logger::kRing + extra; ++i) lg.log(LogCat::Sim, LogLevel::Info, std::to_string(i)); CHECK(lg.size() == Logger::kRing); CHECK(has(lg.line(0), "] " + std::to_string(extra))); // oldest retained = #50 CHECK(has(lg.line(Logger::kRing - 1), std::to_string(Logger::kRing + extra - 1))); // newest = #305 // The SC2K_LOG macro (tests always build with SC2K_DEBUG) records like a direct call. lg.clear(); SC2K_LOG(LogCat::UI, LogLevel::Info, "macro ", 42); CHECK(lg.size() == 1 && has(lg.line(0), "macro 42") && has(lg.line(0), "UI")); if (failures == 0) { std::printf("debug_log: all checks passed\n"); return 0; } std::printf("debug_log: %d failure(s)\n", failures); return 1; }