#pragma once #include #include #include #include #include #include #include #include #include #include #include #include // ── Void::Log — the emission facade // ─────────────────────────────────────────── Header-only apart from its // state, which Void::LogState (a small library with no dependencies) holds // once per process, so every module (including INTERFACE libraries) can log by // linking only that. The full sink framework lives in Void::Logging // (Logger.hpp); bridge with Void/Log/LoggingAdapter.hpp. // // Integration contract: // * No sink installed (the default) — every call is a single relaxed // atomic load; format arguments are NOT formatted. This is the // "no logging at all" path. // * set_sink(...) routes everything to any callable — your own logging // library plugs in here. // * set_min_level(Level::Disabled) silences without removing the sink. // * Per-module overrides: set_module_level("Fsm", Level::Trace). // // The API is deliberately macro-free: leveled function templates take a // std::format_string and only format after the level/sink gate passes. namespace Void::Log { enum class Level : unsigned char { Trace = 0, Debug, Info, Warning, Error, Fatal, Disabled }; /// Signature for any log consumer. Four arguments: /// level — severity of the event /// module — short library-internal label (e.g. "System.Process") /// message — human-readable description /// location — the call site that emitted the event (file/line/function) using Sink = std::function; namespace Detail { // std::source_location cannot follow a variadic pack in a parameter // list, so this wrapper captures the format string together with the // caller's source location in its (consteval) constructor. // std::type_identity_t... keeps Args non-deduced here, so they // are deduced solely from the trailing argument pack. template struct FmtLoc { std::format_string fmt; std::source_location location; template T> consteval FmtLoc(T&& f, std::source_location loc = std::source_location::current()) : fmt(std::forward(f)), location(loc) { } }; struct State { // Fast-path gates (checked before any locking or formatting). std::atomic has_sink{false}; std::atomic min_level{Level::Trace}; std::atomic has_module_levels{false}; // Slow path. Sink sink; std::unordered_map module_levels; std::shared_mutex mutex; }; // The process's one State, defined in Void::LogState (a static in an // inline function would be one per DLL on Windows). State& state(); } // namespace Detail /// Install the global sink. Any previously installed sink is replaced. /// Pass a default-constructed (empty) Sink to silence all output. /// Thread-safe: the new sink is visible to all threads after this returns. inline void set_sink(Sink sink) { auto& s = Detail::state(); std::unique_lock lock{s.mutex}; s.sink = std::move(sink); s.has_sink.store(static_cast(s.sink), std::memory_order_release); } /// Global minimum level. Events below it are discarded before /// formatting. Default: Trace (everything passes; the sink filters). inline void set_min_level(Level level) noexcept { Detail::state().min_level.store(level, std::memory_order_relaxed); } [[nodiscard]] inline Level min_level() noexcept { return Detail::state().min_level.load(std::memory_order_relaxed); } /// Per-module minimum level override (exact label match). Overrides take /// precedence over the global minimum — they can both raise and lower it. inline void set_module_level(std::string_view module, Level level) { auto& s = Detail::state(); std::unique_lock lock{s.mutex}; s.module_levels.insert_or_assign(std::string{module}, level); s.has_module_levels.store(true, std::memory_order_release); } /// Remove a per-module override. inline void clear_module_level(std::string_view module) { auto& s = Detail::state(); std::unique_lock lock{s.mutex}; s.module_levels.erase(std::string{module}); s.has_module_levels.store(!s.module_levels.empty(), std::memory_order_release); } /// Would an event at `level` from `module` currently be emitted? [[nodiscard]] inline bool enabled(Level level, std::string_view module) { auto& s = Detail::state(); if (!s.has_sink.load(std::memory_order_relaxed)) { return false; } if (level == Level::Disabled) { return false; } if (s.has_module_levels.load(std::memory_order_acquire)) { std::shared_lock lock{s.mutex}; if (auto it = s.module_levels.find(std::string{module}); it != s.module_levels.end()) { return level >= it->second; } } return level >= s.min_level.load(std::memory_order_relaxed); } /// Emit one pre-formatted log event through the global sink. /// No-op (zero allocation) when disabled. namespace Detail { // Single, non-overloaded dispatch to the sink. The formatted paths // must forward through this rather than through log() itself: a // log(level, module, std::string, source_location) call would also // match the variadic log() overload (deducing source_location as a // format argument) and fail in FmtLoc's consteval constructor. inline void emit(Level level, std::string_view module, std::string_view message, const std::source_location& location) { auto& s = state(); if (!::Void::Log::enabled(level, module)) { return; } std::shared_lock lock{s.mutex}; if (s.sink) { s.sink(level, module, message, location); } } } // namespace Detail inline void log(Level level, std::string_view module, std::string_view message, const std::source_location& location = std::source_location::current()) { Detail::emit(level, module, message, location); } /// Leveled, lazily-formatted emission: arguments are formatted only /// after the level/sink gate passes. The call-site source location is /// captured transparently via FmtLoc. template void log(Level level, std::string_view module, Detail::FmtLoc...> fmt, Args&&... args) { if (!enabled(level, module)) { return; } Detail::emit(level, module, std::format(fmt.fmt, std::forward(args)...), fmt.location); } template void trace(std::string_view module, Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Trace, module, fmt, std::forward(args)...); } template void debug(std::string_view module, Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Debug, module, fmt, std::forward(args)...); } template void info(std::string_view module, Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Info, module, fmt, std::forward(args)...); } template void warn(std::string_view module, Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Warning, module, fmt, std::forward(args)...); } template void error(std::string_view module, Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Error, module, fmt, std::forward(args)...); } template void fatal(std::string_view module, Detail::FmtLoc...> fmt, Args&&... args) { log(Level::Fatal, module, fmt, std::forward(args)...); } } // namespace Void::Log