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

101 lines
3.5 KiB
C++

#include <Void/Data/Json.hpp>
#include <Void/Log/Formatter.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
using Void::Logging::Entry;
using Void::Logging::JsonLinesFormatter;
using Void::Logging::Level;
using Void::Logging::LogfmtFormatter;
using Void::Logging::PatternFormatter;
namespace
{
Entry make_entry()
{
Entry e;
e.level = Level::Warning;
e.message = "disk almost full";
e.scopes = {"sys", "disk"};
return e;
}
} // namespace
// ── PatternFormatter ──────────────────────────────────────────────────────────
Void::Testing::Test pattern_default_renders_level_and_message()
{
PatternFormatter fmt;
const auto line = fmt.format(make_entry());
Void::Testing::expect(line.find("[Warning]") != std::string::npos);
Void::Testing::expect(line.find("disk almost full") !=
std::string::npos);
Void::Testing::expect(line.find("sys.disk") != std::string::npos);
}
Void::Testing::Test pattern_custom_layout()
{
PatternFormatter fmt{"{level}|{message}"};
Void::Testing::expect_eq(fmt.format(make_entry()),
std::string{"Warning|disk almost full"});
}
Void::Testing::Test pattern_unknown_placeholder_stays_literal()
{
PatternFormatter fmt{"{nope} {message}"};
Void::Testing::expect_eq(fmt.format(make_entry()),
std::string{"{nope} disk almost full"});
}
Void::Testing::Test pattern_time_is_iso8601()
{
PatternFormatter fmt{"{time}"};
const auto line = fmt.format(make_entry());
// "2026-06-12T10:30:00.123Z"
Void::Testing::expect_eq(line.size(), std::size_t{24});
Void::Testing::expect_eq(line[10], 'T');
Void::Testing::expect_eq(line.back(), 'Z');
}
// ── JsonLinesFormatter ────────────────────────────────────────────────────────
Void::Testing::Test jsonlines_produces_valid_json_with_fields()
{
JsonLinesFormatter fmt;
const auto line = fmt.format(make_entry());
const auto parsed = Void::Json::parse(line);
Void::Testing::expect(parsed.has_value());
Void::Testing::expect_eq((*parsed)["level"].as_string(),
std::string{"Warning"});
Void::Testing::expect_eq((*parsed)["message"].as_string(),
std::string{"disk almost full"});
Void::Testing::expect_eq((*parsed)["scopes"].as_array().size(),
std::size_t{2});
}
// ── LogfmtFormatter ───────────────────────────────────────────────────────────
Void::Testing::Test logfmt_renders_key_value_pairs()
{
LogfmtFormatter fmt;
const auto line = fmt.format(make_entry());
Void::Testing::expect(line.find("level=Warning") != std::string::npos);
Void::Testing::expect(line.find("msg=\"disk almost full\"") !=
std::string::npos);
Void::Testing::expect(line.find("scopes=sys.disk") !=
std::string::npos);
}
Void::Testing::Test logfmt_escapes_quotes_in_message()
{
Entry e = make_entry();
e.message = R"(said "hi")";
LogfmtFormatter fmt;
const auto line = fmt.format(e);
Void::Testing::expect(line.find(R"(msg="said \"hi\"")") !=
std::string::npos);
}