Files
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

113 lines
4.3 KiB
C++

#include <Void/Configuration/Config.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstdlib>
#include <string>
using Void::Configuration::Config;
// ── from_env_prefix ───────────────────────────────────────────────────────────
Void::Testing::Test env_prefix_strips_and_normalizes()
{
::setenv("VOIDTEST_LOG__LEVEL", "debug", 1);
::setenv("VOIDTEST_PORT", "9000", 1);
::setenv("UNRELATED_KEY", "x", 1);
const auto cfg = Config::from_env_prefix("VOIDTEST_");
Void::Testing::expect(cfg.get_or("log.level") ==
"debug");
Void::Testing::expect(cfg.get_or("port") ==
"9000");
Void::Testing::expect(!cfg.contains("unrelated_key"));
::unsetenv("VOIDTEST_LOG__LEVEL");
::unsetenv("VOIDTEST_PORT");
::unsetenv("UNRELATED_KEY");
}
// ── from_ini_text ─────────────────────────────────────────────────────────────
Void::Testing::Test ini_sections_become_prefixes()
{
const auto cfg = Config::from_ini_text(
"global_key = 1\n"
"[log]\n"
"level = warn\n"
"[net]\n"
"port = 4242\n");
Void::Testing::expect(cfg.has_value());
Void::Testing::expect(cfg->get_or("global_key") ==
"1");
Void::Testing::expect(cfg->get_or("log.level") ==
"warn");
Void::Testing::expect_eq(*cfg->get_as<int>("net.port"), 4242);
}
Void::Testing::Test ini_parse_error_surfaces()
{
const auto cfg = Config::from_ini_text("[unterminated\nkey=1\n");
Void::Testing::expect(!cfg.has_value());
}
// ── from_json_text ────────────────────────────────────────────────────────────
Void::Testing::Test json_objects_flatten_to_dotted_keys()
{
const auto cfg = Config::from_json_text(R"({
"log": {"level": "error", "async": true},
"servers": [{"host": "a"}, {"host": "b"}],
"ratio": 0.5,
"count": 3
})");
Void::Testing::expect(cfg.has_value());
Void::Testing::expect(cfg->get_or("log.level") ==
"error");
Void::Testing::expect(*cfg->get_as<bool>("log.async"));
Void::Testing::expect(cfg->get_or("servers.0.host") ==
"a");
Void::Testing::expect(cfg->get_or("servers.1.host") ==
"b");
Void::Testing::expect_eq(*cfg->get_as<double>("ratio"), 0.5);
Void::Testing::expect(cfg->get_or("count") ==
"3"); // integral form
}
Void::Testing::Test json_parse_error_surfaces()
{
Void::Testing::expect(!Config::from_json_text("{nope").has_value());
}
// ── from_args ─────────────────────────────────────────────────────────────────
Void::Testing::Test args_equals_space_and_flag_forms()
{
const char* argv[] = {"prog", "--log.level=debug",
"--net.port", "8080",
"--verbose", "positional",
"--trailing-flag"};
const auto cfg = Config::from_args(7, argv);
Void::Testing::expect(cfg.get_or("log.level") ==
"debug");
Void::Testing::expect(cfg.get_or("net.port") ==
"8080");
Void::Testing::expect(cfg.get_or("verbose") ==
"positional");
Void::Testing::expect(cfg.get_or("trailing-flag") ==
"true");
}
// ── overlay precedence ────────────────────────────────────────────────────────
Void::Testing::Test overlay_last_wins_across_sources()
{
auto base = Config::from_ini_text("[log]\nlevel = info\n");
Void::Testing::expect(base.has_value());
const char* argv[] = {"prog", "--log.level=trace"};
base->overlay(Config::from_args(2, argv));
Void::Testing::expect(base->get_or("log.level") ==
"trace");
}