Files
Library/tests/standalone/Log/Log.cpp
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

230 lines
6.5 KiB
C++

#include <Void/Log/Log.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <string>
#include <thread>
#include <vector>
// Reset the global facade state between tests via a scoped RAII guard.
struct SinkGuard {
~SinkGuard()
{
Void::Log::set_sink({});
Void::Log::set_min_level(Void::Log::Level::Trace);
Void::Log::clear_module_level("Filtered");
Void::Log::clear_module_level("Verbose");
}
};
Void::Testing::Test no_sink_is_silent()
{
SinkGuard guard;
bool called = false;
// No sink installed — this must not crash or invoke anything.
Void::Log::log(Void::Log::Level::Info, "test", "silent message");
Void::Testing::expect(!called);
}
Void::Testing::Test sink_receives_all_fields()
{
SinkGuard guard;
Void::Log::Level got_level{};
std::string got_module;
std::string got_message;
Void::Log::set_sink(
[&](Void::Log::Level l, std::string_view mod, std::string_view msg) {
got_level = l;
got_module = mod;
got_message = msg;
});
Void::Log::log(Void::Log::Level::Warning, "MyModule", "something happened");
Void::Testing::expect(got_level == Void::Log::Level::Warning);
Void::Testing::expect(got_module == "MyModule");
Void::Testing::expect(got_message == "something happened");
}
Void::Testing::Test set_sink_replaces_previous()
{
SinkGuard guard;
int first_calls = 0;
int second_calls = 0;
Void::Log::set_sink([&](auto, auto, auto) { ++first_calls; });
Void::Log::log(Void::Log::Level::Info, "m", "a");
Void::Log::set_sink([&](auto, auto, auto) { ++second_calls; });
Void::Log::log(Void::Log::Level::Info, "m", "b");
Void::Testing::expect_eq(first_calls, 1);
Void::Testing::expect_eq(second_calls, 1);
}
Void::Testing::Test clear_sink_silences_output()
{
SinkGuard guard;
int calls = 0;
Void::Log::set_sink([&](auto, auto, auto) { ++calls; });
Void::Log::log(Void::Log::Level::Info, "m", "before clear");
Void::Log::set_sink({});
Void::Log::log(Void::Log::Level::Info, "m", "after clear");
Void::Testing::expect_eq(calls, 1);
}
Void::Testing::Test concurrent_log_calls_are_safe()
{
SinkGuard guard;
std::atomic<int> count{0};
Void::Log::set_sink([&](auto, auto, auto) { ++count; });
constexpr int N = 64;
std::vector<std::thread> threads;
threads.reserve(N);
for (int i = 0; i < N; ++i) {
threads.emplace_back([] {
Void::Log::log(Void::Log::Level::Debug, "test", "concurrent");
});
}
for (auto& t : threads) {
t.join();
}
Void::Testing::expect_eq(count.load(), N);
}
Void::Testing::Test all_levels_are_forwarded()
{
SinkGuard guard;
int calls = 0;
Void::Log::set_sink([&](auto, auto, auto) { ++calls; });
Void::Log::log(Void::Log::Level::Trace, "m", "t");
Void::Log::log(Void::Log::Level::Debug, "m", "d");
Void::Log::log(Void::Log::Level::Info, "m", "i");
Void::Log::log(Void::Log::Level::Warning, "m", "w");
Void::Log::log(Void::Log::Level::Error, "m", "e");
Void::Log::log(Void::Log::Level::Fatal, "m", "f");
Void::Testing::expect_eq(calls, 6);
}
// ── leveled helpers & lazy formatting ─────────────────────────────────────────
Void::Testing::Test leveled_helpers_format_arguments()
{
SinkGuard guard;
std::string got;
Void::Log::Level got_level{};
Void::Log::set_sink([&](Void::Log::Level l, auto, std::string_view msg) {
got_level = l;
got = msg;
});
Void::Log::warn("m", "answer = {}", 42);
Void::Testing::expect(got_level == Void::Log::Level::Warning);
Void::Testing::expect_eq(got, std::string{"answer = 42"});
}
namespace
{
// A format argument whose formatting is observable — proves laziness.
struct FormatProbe {
int* format_count;
};
} // namespace
template <>
struct std::formatter<FormatProbe> : std::formatter<int> {
template <class Ctx>
auto format(const FormatProbe& p, Ctx& ctx) const
{
++*p.format_count;
return std::formatter<int>::format(0, ctx);
}
};
Void::Testing::Test disabled_calls_do_not_format()
{
SinkGuard guard;
int formatted = 0;
// No sink: nothing may be formatted.
Void::Log::debug("m", "{}", FormatProbe{&formatted});
Void::Testing::expect_eq(formatted, 0);
// Sink present but level below the global minimum: still nothing.
int calls = 0;
Void::Log::set_sink([&](auto, auto, auto) { ++calls; });
Void::Log::set_min_level(Void::Log::Level::Error);
Void::Log::debug("m", "{}", FormatProbe{&formatted});
Void::Testing::expect_eq(formatted, 0);
Void::Testing::expect_eq(calls, 0);
// Passing the gate formats exactly once.
Void::Log::error("m", "{}", FormatProbe{&formatted});
Void::Testing::expect_eq(formatted, 1);
Void::Testing::expect_eq(calls, 1);
}
// ── level filtering ───────────────────────────────────────────────────────────
Void::Testing::Test global_min_level_filters()
{
SinkGuard guard;
int calls = 0;
Void::Log::set_sink([&](auto, auto, auto) { ++calls; });
Void::Log::set_min_level(Void::Log::Level::Warning);
Void::Log::info("m", "dropped");
Void::Log::warn("m", "kept");
Void::Log::error("m", "kept");
Void::Testing::expect_eq(calls, 2);
}
Void::Testing::Test disabled_level_silences_everything()
{
SinkGuard guard;
int calls = 0;
Void::Log::set_sink([&](auto, auto, auto) { ++calls; });
Void::Log::set_min_level(Void::Log::Level::Disabled);
Void::Log::fatal("m", "still dropped");
Void::Testing::expect_eq(calls, 0);
}
Void::Testing::Test module_override_lowers_and_raises()
{
SinkGuard guard;
int calls = 0;
Void::Log::set_sink([&](auto, auto, auto) { ++calls; });
Void::Log::set_min_level(Void::Log::Level::Warning);
// Lower the threshold for one module only.
Void::Log::set_module_level("Verbose", Void::Log::Level::Trace);
Void::Log::debug("Verbose", "kept by override");
Void::Log::debug("Other", "dropped by global");
Void::Testing::expect_eq(calls, 1);
// Raise the threshold for one module above the global.
Void::Log::set_module_level("Filtered", Void::Log::Level::Fatal);
Void::Log::error("Filtered", "dropped by override");
Void::Log::error("Other", "kept by global");
Void::Testing::expect_eq(calls, 2);
}
Void::Testing::Test enabled_reflects_configuration()
{
SinkGuard guard;
Void::Testing::expect(
!Void::Log::enabled(Void::Log::Level::Fatal, "m")); // no sink
Void::Log::set_sink([](auto, auto, auto) {});
Void::Log::set_min_level(Void::Log::Level::Info);
Void::Testing::expect(Void::Log::enabled(Void::Log::Level::Info, "m"));
Void::Testing::expect(!Void::Log::enabled(Void::Log::Level::Debug, "m"));
}