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

+86
View File
@@ -0,0 +1,86 @@
#include <Void/Configuration/Bind.hpp>
#include <Void/Configuration/Config.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <optional>
#include <string>
#include <vector>
#ifdef VOID_HAS_CONFIG_REFLECTION
using Void::Configuration::Config;
namespace
{
enum class Sink { Console, File };
struct FileSettings {
std::string path = "default.log";
int max_files = 3;
};
struct LogSettings {
std::string level = "info";
bool async = true;
Sink sink = Sink::Console;
std::chrono::milliseconds flush_every{250};
FileSettings file;
std::optional<int> sample_rate;
std::vector<std::string> tags;
};
} // namespace
Void::Testing::Test bind_fills_present_keys_keeps_defaults()
{
Config cfg;
cfg.set("log.level", "debug");
cfg.set("log.file.path", "/var/log/app.log");
cfg.set("log.flush_every", "1s");
const auto s = Void::Configuration::bind<LogSettings>(cfg, "log");
Void::Testing::expect(s.has_value());
Void::Testing::expect_eq(s->level, std::string{"debug"});
Void::Testing::expect(s->async); // default kept
Void::Testing::expect_eq(s->file.path, std::string{"/var/log/app.log"});
Void::Testing::expect_eq(s->file.max_files, 3); // nested default kept
Void::Testing::expect(s->flush_every ==
std::chrono::milliseconds{1000});
}
Void::Testing::Test bind_enum_optional_vector()
{
Config cfg;
cfg.set("log.sink", "File");
cfg.set("log.sample_rate", "10");
cfg.set("log.tags.0", "alpha");
cfg.set("log.tags.1", "beta");
const auto s = Void::Configuration::bind<LogSettings>(cfg, "log");
Void::Testing::expect(s.has_value());
Void::Testing::expect(s->sink == Sink::File);
Void::Testing::expect(s->sample_rate.has_value());
Void::Testing::expect_eq(*s->sample_rate, 10);
Void::Testing::expect_eq(s->tags.size(), std::size_t{2});
Void::Testing::expect_eq(s->tags[1], std::string{"beta"});
}
Void::Testing::Test bind_reports_conversion_errors()
{
Config cfg;
cfg.set("log.async", "not-a-bool");
const auto s = Void::Configuration::bind<LogSettings>(cfg, "log");
Void::Testing::expect(!s.has_value());
}
Void::Testing::Test bind_empty_prefix_binds_root_keys()
{
Config cfg;
cfg.set("path", "root.log");
const auto s = Void::Configuration::bind<FileSettings>(cfg);
Void::Testing::expect(s.has_value());
Void::Testing::expect_eq(s->path, std::string{"root.log"});
}
#endif // VOID_HAS_CONFIG_REFLECTION
+112
View File
@@ -0,0 +1,112 @@
#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");
}
+104
View File
@@ -0,0 +1,104 @@
#include <Void/Configuration/Config.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <string>
using Void::Configuration::Config;
namespace
{
enum class Color { Red, Green, Blue };
Config sample()
{
Config cfg;
cfg.set("port", "8080");
cfg.set("ratio", "0.25");
cfg.set("verbose", "TRUE");
cfg.set("quiet", "off");
cfg.set("color", "Green");
cfg.set("timeout", "250ms");
cfg.set("interval", "2m");
cfg.set("negative", "-42");
cfg.set("junk", "12abc");
return cfg;
}
} // namespace
Void::Testing::Test typed_int_and_double()
{
const auto cfg = sample();
Void::Testing::expect_eq(*cfg.get_as<int>("port"), 8080);
Void::Testing::expect_eq(*cfg.get_as<double>("ratio"), 0.25);
Void::Testing::expect_eq(*cfg.get_as<long>("negative"), -42L);
}
Void::Testing::Test typed_bool_spellings()
{
const auto cfg = sample();
Void::Testing::expect(*cfg.get_as<bool>("verbose"));
Void::Testing::expect(!*cfg.get_as<bool>("quiet"));
}
Void::Testing::Test typed_enum_by_name()
{
const auto cfg = sample();
const auto color = cfg.get_as<Color>("color");
Void::Testing::expect(color.has_value());
Void::Testing::expect(*color == Color::Green);
}
Void::Testing::Test typed_durations()
{
using namespace std::chrono;
const auto cfg = sample();
Void::Testing::expect(*cfg.get_as<milliseconds>("timeout") ==
milliseconds{250});
Void::Testing::expect(*cfg.get_as<seconds>("interval") ==
seconds{120});
}
Void::Testing::Test typed_errors_are_reported()
{
const auto cfg = sample();
Void::Testing::expect(!cfg.get_as<int>("junk").has_value());
Void::Testing::expect(!cfg.get_as<int>("missing").has_value());
Void::Testing::expect(!cfg.get_as<bool>("port").has_value());
Void::Testing::expect(!cfg.get_as<Color>("verbose").has_value());
}
Void::Testing::Test typed_get_or_as_fallback()
{
const auto cfg = sample();
Void::Testing::expect_eq(cfg.get_or_as<int>("port", 1), 8080);
Void::Testing::expect_eq(cfg.get_or_as<int>("missing", 7), 7);
}
Void::Testing::Test subtree_strips_prefix()
{
Config cfg;
cfg.set("log.level", "debug");
cfg.set("log.file.path", "/tmp/a.log");
cfg.set("net.port", "1");
const auto log = cfg.subtree("log");
Void::Testing::expect_eq(log.size(), std::size_t{2});
Void::Testing::expect(log.get_or("level") ==
"debug");
Void::Testing::expect(log.get_or("file.path") ==
"/tmp/a.log");
Void::Testing::expect(!log.contains("net.port"));
}
Void::Testing::Test keys_are_sorted()
{
Config cfg;
cfg.set("b", "2");
cfg.set("a", "1");
const auto keys = cfg.keys();
Void::Testing::expect_eq(keys.size(), std::size_t{2});
Void::Testing::expect_eq(keys[0], std::string{"a"});
Void::Testing::expect_eq(keys[1], std::string{"b"});
}
+116
View File
@@ -0,0 +1,116 @@
#include <Void/Configuration/Watch.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <chrono>
#include <filesystem>
#include <mutex>
#include <string>
#include <vector>
using Void::Configuration::Config;
using Void::Configuration::ConfigWatch;
namespace
{
Void::FileSystem::FileWatcherOptions manual_poll()
{
return {.poll_interval = std::chrono::milliseconds{60'000},
.debounce_polls = 2};
}
int bump = 0;
void write(const std::filesystem::path& p, std::string_view text)
{
auto r = Void::FileSystem::write_file(p, text);
Void::Testing::expect(r.has_value());
const auto now = std::filesystem::file_time_type::clock::now();
std::filesystem::last_write_time(
p, now + std::chrono::milliseconds{(++bump) * 10});
}
} // namespace
Void::Testing::Test config_watch_reloads_and_diffs()
{
Void::FileSystem::TempDir tmp{"void_cfgwatch"};
const auto file = tmp.path() / "app.ini";
write(file, "[log]\nlevel = info\nkeep = 1\n");
std::mutex mu;
std::vector<std::string> last_changed;
std::string last_level;
auto watch = ConfigWatch::ini(
file,
[&](const Config& cfg, const std::vector<std::string>& changed) {
std::lock_guard lock(mu);
last_changed = changed;
last_level = cfg.get_or("log.level");
},
manual_poll());
Void::Testing::expect(watch.current().get_or("log.level") ==
"info");
write(file, "[log]\nlevel = debug\nkeep = 1\nadded = x\n");
watch.poll_now();
watch.poll_now(); // debounce satisfied -> reload + callback
std::lock_guard lock(mu);
Void::Testing::expect_eq(last_level, std::string{"debug"});
Void::Testing::expect_eq(last_changed.size(), std::size_t{2});
Void::Testing::expect_eq(last_changed[0], std::string{"log.added"});
Void::Testing::expect_eq(last_changed[1], std::string{"log.level"});
Void::Testing::expect(watch.current().get_or("log.level") ==
"debug");
}
Void::Testing::Test config_watch_keeps_old_config_on_parse_error()
{
Void::FileSystem::TempDir tmp{"void_cfgwatch"};
const auto file = tmp.path() / "app.ini";
write(file, "[log]\nlevel = info\n");
std::atomic<int> reloads{0};
auto watch = ConfigWatch::ini(
file,
[&](const Config&, const std::vector<std::string>&) {
++reloads;
},
manual_poll());
write(file, "[broken\nlevel=zzz\n"); // malformed section
watch.poll_now();
watch.poll_now();
Void::Testing::expect_eq(reloads.load(), 0);
Void::Testing::expect(watch.current().get_or("log.level") ==
"info");
}
Void::Testing::Test config_watch_deletion_keeps_last_config()
{
Void::FileSystem::TempDir tmp{"void_cfgwatch"};
const auto file = tmp.path() / "app.ini";
write(file, "[log]\nlevel = info\n");
std::atomic<int> reloads{0};
auto watch = ConfigWatch::ini(
file,
[&](const Config&, const std::vector<std::string>&) {
++reloads;
},
manual_poll());
std::filesystem::remove(file);
watch.poll_now();
watch.poll_now();
Void::Testing::expect_eq(reloads.load(), 0);
Void::Testing::expect(watch.current().get_or("log.level") ==
"info");
}