Files
Library/include/Void/Log/Sinks.hpp
T
faumarayandClaude Fable 5 c74a02a5a5 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>
2026-06-13 00:11:13 +03:00

115 lines
2.4 KiB
C++

#pragma once
#include <Void/Log/Logger.hpp>
#include <functional>
#include <memory>
#include <span>
#include <vector>
namespace Void::Logging
{
/// Discards everything. Useful as a placeholder or for benchmarks.
class NullLogger final : public Logger
{
public:
explicit NullLogger(Mode mode = Mode::Immediate) : Logger(mode) {}
~NullLogger() noexcept override { flush(); }
protected:
void write(const Entry&) override {}
};
/// Forwards every entry to a user callback. The simplest way to splice
/// an external logging library into the Logger framework.
class CallbackLogger final : public Logger
{
public:
using Callback = std::function<void(const Entry&)>;
explicit CallbackLogger(
Callback callback,
Mode mode = Mode::Async,
std::size_t capacity = 512,
OverflowPolicy policy = OverflowPolicy::Block)
: Logger(mode, capacity, policy),
m_callback(std::move(callback))
{
}
~CallbackLogger() noexcept override { flush(); }
protected:
void write(const Entry& entry) override
{
if (m_callback) {
m_callback(entry);
}
}
private:
Callback m_callback;
};
/// Tee: relays every entry to each child logger. Children keep their
/// own modes, levels, and formatters. The MultiLogger itself defaults
/// to Immediate dispatch — the children already decide whether they
/// queue asynchronously, so double-queueing only adds latency.
class MultiLogger final : public Logger
{
public:
explicit MultiLogger(
std::vector<std::shared_ptr<Logger>> children = {},
Mode mode = Mode::Immediate)
: Logger(mode), m_children(std::move(children))
{
}
~MultiLogger() noexcept override { flush(); }
/// Add a child sink. Not thread-safe with concurrent logging —
/// assemble the tree before use.
void add(std::shared_ptr<Logger> child)
{
if (child) {
m_children.push_back(std::move(child));
}
}
[[nodiscard]] std::size_t child_count() const noexcept
{
return m_children.size();
}
/// Flush own pending entries, then every child.
void flush() override
{
Logger::flush();
for (const auto& child : m_children) {
child->flush();
}
}
protected:
void write(const Entry& entry) override
{
for (const auto& child : m_children) {
child->relay(entry);
}
}
private:
std::vector<std::shared_ptr<Logger>> m_children;
};
} // namespace Void::Logging