Overhaul Logging and Configuration; integrate the log facade library-wide

Void::Log facade (header-only, macro-free):
- Leveled function templates trace/debug/info/warn/error/fatal(module,
  fmt, args...) with lazy formatting - a disabled call is one relaxed
  atomic load, no allocation, no formatting.
- Global min level + per-module overrides (can raise or lower); empty
  sink/Disabled level = logging fully off; set_sink remains the splice
  point for external logging libraries.

Void::Logging::Logger - async by default, one class for both modes:
- Mode::Async (default): lock-free MPSC queue + logger-owned worker
  delivering batches via on_flush outside any lock; Immediate/Buffered
  give synchronous behavior via constructor arg or set_mode at runtime.
- Bounded queue with OverflowPolicy{Block, DropNewest, DropOldest};
  flush() (now virtual) blocks until everything submitted is delivered;
  base destructor only stops/joins the worker, never touches virtuals.
- Fixed a wakeup race: stale park token + fresh release overflowed the
  binary semaphore; wakeup is a counting semaphore (spurious wakes are
  absorbed by the worker loop).

New sinks and formatters:
- RotatingFileLogger (size-based rotation, max-files cap), NullLogger,
  CallbackLogger, MultiLogger (tee via new Logger::relay), SyslogLogger.
- Formatter interface: PatternFormatter ({time}/{level}/{scopes}/...),
  JsonLinesFormatter, LogfmtFormatter; installable on Console/File/
  Rotating sinks. Console/File sinks now default to Async.
- Logging::from_config builds the whole sink tree from Config keys;
  apply_facade_config drives facade levels.

Configuration:
- Typed get_as<T>/get_or_as<T> (numbers, bool spellings, enums by name,
  chrono durations), dotted keys, subtree(), sorted keys().
- New sources: from_env_prefix (APP_LOG__LEVEL -> log.level), from_ini
  (sections as prefixes), from_json (objects/arrays flattened),
  from_args (--key=value / --key value / --flag).
- Bind.hpp: reflection bind<T>(cfg, prefix) fills aggregates; defaults
  in the struct are the schema; nested aggregates, optional, vector.
- Watch.hpp: ConfigWatch reloads on change with per-key diff; parse
  failures keep the previous config.

System: new FileSystem::FileWatcher - debounced polling watcher (stable
mtime+size across N polls), callbacks off-thread, deletion events;
shared groundwork for plugin hot-reload.

Sweep: all internal Void::Log::log call sites migrated to the leveled
API ("Umbrella.Feature" labels); Plugin loader now logs load/reload.

918 tests green (28 new), stable across 30 consecutive runs, ASan+UBSan
clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-13 00:11:13 +03:00
1 parent 8a6065539a
commit c74a02a5a5
43 files changed
+3889 -236

No files matched your search

+160 -21
View File
@@ -1,10 +1,32 @@
#pragma once
#include <atomic>
#include <format>
#include <functional>
#include <mutex>
#include <shared_mutex>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
// ── Void::Log — the emission facade
// ─────────────────────────────────────────── Header-only so every module
// (including INTERFACE libraries) can log with zero link dependencies. 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
{
@@ -14,27 +36,35 @@ namespace Void::Log
Info,
Warning,
Error,
Fatal
Fatal,
Disabled
};
/// Signature for any log consumer. Three arguments:
/// level — severity of the event
/// module — short library-internal label (e.g. "FileSystem",
/// "Concurrency") message — human-readable description
using Sink = std::function<void(
Level level, std::string_view module, std::string_view message)>;
/// module — short library-internal label (e.g. "System.Process")
/// message — human-readable description
using Sink = std::function<void(Level level, std::string_view module,
std::string_view message)>;
namespace Detail
{
struct SinkState {
Sink sink;
std::shared_mutex mutex;
struct State {
// Fast-path gates (checked before any locking or formatting).
std::atomic<bool> has_sink{false};
std::atomic<Level> min_level{Level::Trace};
std::atomic<bool> has_module_levels{false};
// Slow path.
Sink sink;
std::unordered_map<std::string, Level> module_levels;
std::shared_mutex mutex;
};
inline SinkState& sink_state()
inline State& state()
{
static SinkState state;
return state;
static State s;
return s;
}
} // namespace Detail
@@ -43,22 +73,131 @@ namespace Void::Log
/// Thread-safe: the new sink is visible to all threads after this returns.
inline void set_sink(Sink sink)
{
auto& state = Detail::sink_state();
std::unique_lock lock{state.mutex};
state.sink = std::move(sink);
auto& s = Detail::state();
std::unique_lock lock{s.mutex};
s.sink = std::move(sink);
s.has_sink.store(static_cast<bool>(s.sink), std::memory_order_release);
}
/// Emit one log event through the global sink.
/// No-op (zero allocation) if no sink is installed.
/// Thread-safe with respect to set_sink() and concurrent log() calls.
/// 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.
inline void log(Level level, std::string_view module,
std::string_view message)
{
auto& state = Detail::sink_state();
std::shared_lock lock{state.mutex};
if (state.sink) {
state.sink(level, module, message);
auto& s = Detail::state();
if (!enabled(level, module)) {
return;
}
std::shared_lock lock{s.mutex};
if (s.sink) {
s.sink(level, module, message);
}
}
/// Leveled, lazily-formatted emission: arguments are formatted only
/// after the level/sink gate passes.
template <typename... Args>
void log(Level level, std::string_view module,
std::format_string<Args...> fmt, Args&&... args)
{
if (!enabled(level, module)) {
return;
}
log(level, module, std::format(fmt, std::forward<Args>(args)...));
}
template <typename... Args>
void trace(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
{
log(Level::Trace, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void debug(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
{
log(Level::Debug, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void info(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
{
log(Level::Info, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void warn(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
{
log(Level::Warning, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void error(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
{
log(Level::Error, module, fmt, std::forward<Args>(args)...);
}
template <typename... Args>
void fatal(std::string_view module, std::format_string<Args...> fmt,
Args&&... args)
{
log(Level::Fatal, module, fmt, std::forward<Args>(args)...);
}
} // namespace Void::Log