Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879// 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 <cstdio>#include <string>
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;}