// sc2k/debug/log.hpp — a categorized, leveled developer log. This is the "debug stuff that gets // removed for release" layer: the SC2K_LOG(...) macro expands to a real call only under SC2K_DEBUG // (set by `make test` and `make app DEBUG=1`); in a release build it is `((void)0)`, so nothing is // compiled in and there is zero runtime cost. // // Pure / header-only / no SDL, so the test suite links it freely and sim headers can sprinkle // SC2K_LOG at interesting seams (power, coverage, disasters) without dragging in the front end. // Accepted lines go to a fixed ring buffer (the on-screen log console reads this) and, optionally, // to stderr (off by default so `make test` stays quiet). Logging only ever touches this buffer — // never sim state — so it cannot perturb the validated RNG or the byte-exact equilibrium tests. #pragma once #include #include #include #include #include namespace sc2k::debug { // Subsystems a line can be tagged with. The runtime mask (one bit per category) filters them. enum class LogCat { Sim, Power, Water, Growth, Demand, Finance, Coverage, Disaster, Things, Render, IO, UI, Count }; // Severity. Lower = more important; the threshold keeps levels <= it. Trace is hidden by default. enum class LogLevel { Error, Warn, Info, Trace }; inline const char* log_cat_name(LogCat c) { switch (c) { case LogCat::Sim: return "Sim"; case LogCat::Power: return "Power"; case LogCat::Water: return "Water"; case LogCat::Growth: return "Growth"; case LogCat::Demand: return "Demand"; case LogCat::Finance: return "Finance"; case LogCat::Coverage: return "Coverage"; case LogCat::Disaster: return "Disaster"; case LogCat::Things: return "Things"; case LogCat::Render: return "Render"; case LogCat::IO: return "IO"; case LogCat::UI: return "UI"; default: return "?"; } } inline const char* log_level_name(LogLevel l) { switch (l) { case LogLevel::Error: return "ERR"; case LogLevel::Warn: return "WRN"; case LogLevel::Info: return "inf"; default: return "trc"; } } // Header-only singleton via a function-local static (one instance across all TUs, no ODR issue). class Logger { public: static Logger& instance() { static Logger g; return g; } // --- filtering knobs (driven by the debug UI) --- void set_threshold(LogLevel l) { threshold_ = l; } LogLevel threshold() const { return threshold_; } void cycle_threshold() { // F8: Error→Warn→Info→Trace→Error threshold_ = LogLevel((static_cast(threshold_) + 1) % (static_cast(LogLevel::Trace) + 1)); } void set_mask(std::uint32_t m) { catMask_ = m; } std::uint32_t mask() const { return catMask_; } void enable(LogCat c, bool on) { const std::uint32_t bit = 1u << static_cast(c); catMask_ = on ? (catMask_ | bit) : (catMask_ & ~bit); } bool enabled(LogCat c) const { return (catMask_ >> static_cast(c)) & 1u; } void set_mirror(bool on) { mirror_ = on; } // also echo accepted lines to stderr // Accept a line if its level passes the threshold and its category is unmasked; store + maybe echo. void log(LogCat c, LogLevel l, const std::string& msg) { if (static_cast(l) > static_cast(threshold_)) return; if (!enabled(c)) return; std::string line = std::string("[") + log_cat_name(c) + "/" + log_level_name(l) + "] " + msg; ring_[static_cast(head_)] = line; head_ = (head_ + 1) % kRing; if (count_ < kRing) ++count_; if (mirror_) std::fprintf(stderr, "%s\n", line.c_str()); } // --- ring-buffer readout (oldest-first: line(0) is the oldest retained line) --- static constexpr int kRing = 256; int size() const { return count_; } void clear() { count_ = 0; head_ = 0; } const std::string& line(int i) const { const int start = (count_ < kRing) ? 0 : head_; return ring_[static_cast((start + i) % kRing)]; } private: std::array ring_{}; int head_ = 0, count_ = 0; std::uint32_t catMask_ = 0xFFFFFFFFu; // every category on by default LogLevel threshold_ = LogLevel::Info; // Error/Warn/Info shown; Trace hidden until F8 bool mirror_ = false; // quiet by default (keeps `make test` output clean) }; // Fold the variadic args into one string via ostringstream (the codebase has no ; this // matches its string-concat style and accepts anything with an operator<<). template inline std::string log_format(Args&&... args) { std::ostringstream oss; (oss << ... << args); return oss.str(); } } // namespace sc2k::debug // The call site. Under SC2K_DEBUG it formats + records; otherwise it vanishes (args not evaluated). #ifdef SC2K_DEBUG #define SC2K_LOG(cat, lvl, ...) \ ::sc2k::debug::Logger::instance().log((cat), (lvl), ::sc2k::debug::log_format(__VA_ARGS__)) #else #define SC2K_LOG(cat, lvl, ...) ((void)0) #endif