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>
75 lines
2.2 KiB
C++
75 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Log/Formatter.hpp>
|
|
#include <Void/Log/Logger.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <memory>
|
|
#include <span>
|
|
|
|
namespace Void::Logging
|
|
{
|
|
/// Logger sink that appends formatted entries to a file on disk.
|
|
///
|
|
/// Defaults to Mode::Async — entries are queued lock-free and written
|
|
/// in batches by the logger's worker thread (one ofstream flush per
|
|
/// batch). The destructor flushes any remaining entries. Pass
|
|
/// Mode::Immediate/Buffered to opt out of the worker thread.
|
|
///
|
|
/// Two construction paths:
|
|
/// - `FileLogger::open(path)` returns
|
|
/// `Result<unique_ptr<FileLogger>, Error>` and never throws —
|
|
/// prefer this in code that already uses Void::Result for error
|
|
/// plumbing.
|
|
/// - The throwing constructor stays for callers that prefer
|
|
/// exceptions; it throws std::runtime_error if the path cannot
|
|
/// be opened for append.
|
|
class FileLogger final : public Logger
|
|
{
|
|
public:
|
|
|
|
explicit FileLogger(const std::filesystem::path& path,
|
|
Mode mode = Mode::Async,
|
|
std::size_t capacity = 512);
|
|
|
|
~FileLogger() noexcept override;
|
|
|
|
FileLogger(const FileLogger&) = delete;
|
|
FileLogger& operator=(const FileLogger&) = delete;
|
|
FileLogger(FileLogger&&) = delete;
|
|
FileLogger& operator=(FileLogger&&) = delete;
|
|
|
|
/// Non-throwing factory. On failure returns
|
|
/// `Failure<Error>{Error{"FileLogger: cannot open '<path>'"}}`.
|
|
[[nodiscard]] static Void::Result::
|
|
Result<std::unique_ptr<FileLogger>, Void::Result::Error>
|
|
open(const std::filesystem::path& path,
|
|
Mode mode = Mode::Async,
|
|
std::size_t capacity = 512);
|
|
|
|
/// Install a formatter replacing the default "[Level] message"
|
|
/// rendering. Call before logging starts.
|
|
void set_formatter(std::shared_ptr<const Formatter> formatter)
|
|
{
|
|
m_formatter = std::move(formatter);
|
|
}
|
|
|
|
protected:
|
|
|
|
void write(const Entry& entry) override;
|
|
void on_flush(std::span<const Entry> entries) override;
|
|
|
|
private:
|
|
|
|
void append(const Entry& entry);
|
|
|
|
std::filesystem::path m_path;
|
|
std::ofstream m_file;
|
|
std::shared_ptr<const Formatter> m_formatter;
|
|
};
|
|
|
|
} // namespace Void::Logging
|