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,109 @@
|
||||
#include <Void/Log/RotatingFileLogger.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
|
||||
RotatingFileLogger::RotatingFileLogger(const std::filesystem::path& path,
|
||||
Options options)
|
||||
: Logger(options.mode, options.capacity, options.policy), m_path(path),
|
||||
m_options(options)
|
||||
{
|
||||
std::error_code ec;
|
||||
if (std::filesystem::exists(m_path, ec)) {
|
||||
m_current_bytes = static_cast<std::size_t>(
|
||||
std::filesystem::file_size(m_path, ec));
|
||||
}
|
||||
m_file.open(m_path, std::ios::app);
|
||||
if (!m_file.is_open()) {
|
||||
throw std::runtime_error(std::format(
|
||||
"RotatingFileLogger: cannot open '{}'", m_path.string()));
|
||||
}
|
||||
Void::Log::debug("Log.RotatingFile", "opened '{}' ({} bytes)",
|
||||
m_path.string(), m_current_bytes);
|
||||
}
|
||||
|
||||
Void::Result::Result<std::unique_ptr<RotatingFileLogger>,
|
||||
Void::Result::Error>
|
||||
RotatingFileLogger::open(const std::filesystem::path& path,
|
||||
Options options)
|
||||
{
|
||||
try {
|
||||
return Void::Result::ok(
|
||||
std::make_unique<RotatingFileLogger>(path, options));
|
||||
} catch (const std::exception& e) {
|
||||
return Void::Result::fail(std::string{e.what()});
|
||||
}
|
||||
}
|
||||
|
||||
RotatingFileLogger::~RotatingFileLogger() noexcept { flush(); }
|
||||
|
||||
void RotatingFileLogger::write(const Entry& entry)
|
||||
{
|
||||
append(entry);
|
||||
m_file.flush();
|
||||
rotate_if_needed();
|
||||
}
|
||||
|
||||
void RotatingFileLogger::on_flush(std::span<const Entry> entries)
|
||||
{
|
||||
for (const auto& entry : entries) {
|
||||
append(entry);
|
||||
}
|
||||
m_file.flush();
|
||||
rotate_if_needed();
|
||||
}
|
||||
|
||||
void RotatingFileLogger::append(const Entry& entry)
|
||||
{
|
||||
std::string line;
|
||||
if (m_formatter) {
|
||||
line = m_formatter->format(entry);
|
||||
} else {
|
||||
line =
|
||||
std::format("[{}] {}", to_string(entry.level), entry.message);
|
||||
}
|
||||
line += '\n';
|
||||
m_file << line;
|
||||
m_current_bytes += line.size();
|
||||
}
|
||||
|
||||
void RotatingFileLogger::rotate_if_needed()
|
||||
{
|
||||
if (m_current_bytes < m_options.max_bytes) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_file.close();
|
||||
std::error_code ec;
|
||||
|
||||
// Shift app.log.(N-1) -> app.log.N, ..., app.log -> app.log.1;
|
||||
// the oldest file falls off the end.
|
||||
const auto numbered = [&](std::size_t n) {
|
||||
auto p = m_path;
|
||||
p += std::format(".{}", n);
|
||||
return p;
|
||||
};
|
||||
std::filesystem::remove(numbered(m_options.max_files), ec);
|
||||
for (std::size_t n = m_options.max_files; n > 1; --n) {
|
||||
const auto from = numbered(n - 1);
|
||||
if (std::filesystem::exists(from, ec)) {
|
||||
std::filesystem::rename(from, numbered(n), ec);
|
||||
}
|
||||
}
|
||||
if (m_options.max_files > 0) {
|
||||
std::filesystem::rename(m_path, numbered(1), ec);
|
||||
} else {
|
||||
std::filesystem::remove(m_path, ec);
|
||||
}
|
||||
|
||||
m_file.open(m_path, std::ios::trunc);
|
||||
m_current_bytes = 0;
|
||||
Void::Log::debug("Log.RotatingFile", "rotated '{}'", m_path.string());
|
||||
}
|
||||
|
||||
} // namespace Void::Logging
|
||||
Reference in new issue
Block a user