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:
1 parent
8a6065539a
commit
c74a02a5a5
43 files changed
+3889
-236
No files matched your search
@@ -0,0 +1,198 @@
|
||||
#include <Void/Log/Logger.hpp>
|
||||
#include <Void/Log/Sinks.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using Void::Logging::CallbackLogger;
|
||||
using Void::Logging::Entry;
|
||||
using Void::Logging::Level;
|
||||
using Void::Logging::Mode;
|
||||
using Void::Logging::OverflowPolicy;
|
||||
|
||||
// ── basics ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test async_is_the_default_mode()
|
||||
{
|
||||
CallbackLogger log{[](const Entry&) {}};
|
||||
Void::Testing::expect(log.mode() == Mode::Async);
|
||||
}
|
||||
|
||||
Void::Testing::Test async_flush_delivers_everything_submitted()
|
||||
{
|
||||
std::atomic<int> delivered{0};
|
||||
CallbackLogger log{[&](const Entry&) { ++delivered; }};
|
||||
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
log.info("entry");
|
||||
}
|
||||
log.flush();
|
||||
Void::Testing::expect_eq(delivered.load(), 100);
|
||||
}
|
||||
|
||||
Void::Testing::Test async_entries_arrive_in_order()
|
||||
{
|
||||
std::vector<std::string> messages;
|
||||
CallbackLogger log{
|
||||
[&](const Entry& e) { messages.push_back(e.message); }};
|
||||
|
||||
log.info("one");
|
||||
log.info("two");
|
||||
log.info("three");
|
||||
log.flush();
|
||||
|
||||
Void::Testing::expect_eq(messages.size(), std::size_t{3});
|
||||
Void::Testing::expect_eq(messages[0], std::string{"one"});
|
||||
Void::Testing::expect_eq(messages[1], std::string{"two"});
|
||||
Void::Testing::expect_eq(messages[2], std::string{"three"});
|
||||
}
|
||||
|
||||
Void::Testing::Test async_level_filter_applies()
|
||||
{
|
||||
std::atomic<int> delivered{0};
|
||||
CallbackLogger log{[&](const Entry&) { ++delivered; }};
|
||||
log.set_level(Level::Warning);
|
||||
|
||||
log.debug("dropped");
|
||||
log.warning("kept");
|
||||
log.flush();
|
||||
Void::Testing::expect_eq(delivered.load(), 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test destructor_flushes_via_derived_contract()
|
||||
{
|
||||
std::atomic<int> delivered{0};
|
||||
{
|
||||
CallbackLogger log{[&](const Entry&) { ++delivered; }};
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
log.info("entry");
|
||||
}
|
||||
// No explicit flush — CallbackLogger's destructor must flush.
|
||||
}
|
||||
Void::Testing::expect_eq(delivered.load(), 32);
|
||||
}
|
||||
|
||||
// ── mode switching (same class, no decorators) ────────────────────────────────
|
||||
|
||||
Void::Testing::Test switch_async_to_immediate_makes_calls_synchronous()
|
||||
{
|
||||
std::atomic<int> delivered{0};
|
||||
std::thread::id write_thread{};
|
||||
CallbackLogger log{[&](const Entry&) {
|
||||
++delivered;
|
||||
write_thread = std::this_thread::get_id();
|
||||
}};
|
||||
|
||||
log.info("async one");
|
||||
log.set_mode(Mode::Immediate); // drains pending first
|
||||
Void::Testing::expect_eq(delivered.load(), 1);
|
||||
|
||||
log.info("sync two");
|
||||
// Immediate: delivered inline on this thread, no flush needed.
|
||||
Void::Testing::expect_eq(delivered.load(), 2);
|
||||
Void::Testing::expect(write_thread == std::this_thread::get_id());
|
||||
}
|
||||
|
||||
Void::Testing::Test switch_immediate_to_async_round_trip()
|
||||
{
|
||||
std::atomic<int> delivered{0};
|
||||
CallbackLogger log{[&](const Entry&) { ++delivered; },
|
||||
Mode::Immediate};
|
||||
|
||||
log.info("sync");
|
||||
Void::Testing::expect_eq(delivered.load(), 1);
|
||||
|
||||
log.set_mode(Mode::Async);
|
||||
log.info("queued");
|
||||
log.flush();
|
||||
Void::Testing::expect_eq(delivered.load(), 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test async_writes_happen_on_worker_thread()
|
||||
{
|
||||
std::thread::id write_thread{};
|
||||
CallbackLogger log{[&](const Entry&) {
|
||||
write_thread = std::this_thread::get_id();
|
||||
}};
|
||||
|
||||
log.info("from main");
|
||||
log.flush();
|
||||
Void::Testing::expect(write_thread != std::this_thread::get_id());
|
||||
}
|
||||
|
||||
// ── overflow policies ─────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test block_policy_loses_nothing_under_pressure()
|
||||
{
|
||||
std::atomic<int> delivered{0};
|
||||
{
|
||||
// Tiny capacity + slow-ish sink forces producers to block.
|
||||
CallbackLogger log{[&](const Entry&) { ++delivered; }, Mode::Async,
|
||||
4, OverflowPolicy::Block};
|
||||
for (int i = 0; i < 500; ++i) {
|
||||
log.info("pressure");
|
||||
}
|
||||
log.flush();
|
||||
}
|
||||
Void::Testing::expect_eq(delivered.load(), 500);
|
||||
}
|
||||
|
||||
Void::Testing::Test drop_newest_policy_bounds_pending()
|
||||
{
|
||||
std::atomic<bool> release{false};
|
||||
std::atomic<int> delivered{0};
|
||||
{
|
||||
CallbackLogger log{[&](const Entry&) {
|
||||
// Stall the worker so the queue stays full.
|
||||
release.wait(false);
|
||||
++delivered;
|
||||
},
|
||||
Mode::Async, 2, OverflowPolicy::DropNewest};
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
log.info("burst");
|
||||
}
|
||||
release = true;
|
||||
release.notify_all();
|
||||
log.flush();
|
||||
}
|
||||
// Far fewer than 100 must have arrived; none blocked the producer.
|
||||
Void::Testing::expect(delivered.load() < 100);
|
||||
Void::Testing::expect(delivered.load() >= 1);
|
||||
}
|
||||
|
||||
// ── multi-producer stress ─────────────────────────────────────────────────────
|
||||
|
||||
// @tag stress, concurrency
|
||||
Void::Testing::Test async_multi_producer_no_loss()
|
||||
{
|
||||
constexpr int producers = 8;
|
||||
constexpr int per_producer = 50'000;
|
||||
|
||||
std::atomic<long long> delivered{0};
|
||||
{
|
||||
CallbackLogger log{[&](const Entry&) { ++delivered; }, Mode::Async,
|
||||
1024, OverflowPolicy::Block};
|
||||
log.set_level(Level::Trace);
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(producers);
|
||||
for (int p = 0; p < producers; ++p) {
|
||||
threads.emplace_back([&log] {
|
||||
for (int i = 0; i < per_producer; ++i) {
|
||||
log.info("stress");
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
log.flush();
|
||||
}
|
||||
Void::Testing::expect_eq(
|
||||
delivered.load(),
|
||||
static_cast<long long>(producers) * per_producer);
|
||||
}
|
||||
Reference in new issue
Block a user