Pattern formatting (PatternFormatter):
- Token spec grammar {token:[align][width][,color]} — align <,>,^,
std::format-style width, color a name or "auto" (= the level's color).
- Custom tokens via register_token(name, fn); unknown {name} resolves
against registered renderers, then Entry::fields by key, else stays
literal — so structured fields print as {request_id:>8}.
- Color model: AnsiColor + ColorPolicy{enabled, level_colors,
token_colors} (ColorPolicy::defaults()); only PatternFormatter
colorizes (never file/JSON/logfmt). ConsoleLogger now renders its
default line through a colored PatternFormatter and exposes
set_color_policy (honors Stacktrace::Color::g_color_enabled).
Structured fields:
- Entry gains a fields vector; Logging::FieldScope (RAII, thread-local)
attaches key/values to every entry on the thread, snapshotted like
scopes. JsonLines/logfmt include them.
New file sinks (async by default):
- Detail::RollingFile factors size+max-files rotation; RotatingFileLogger
now delegates to it.
- SplitFileLogger: per-level files (<dir>/<level>.log) plus an optional
combined <dir>/all.log.
- HistoryLogger: always-on, never-level-filtered durable JSON-lines
audit trail over a rolling file.
- from_config gains split/history sinks and a log.color toggle.
22 new tests (pattern width/align/custom-token/field, color on/off + name
parsing, FieldScope nesting, split routing + combined, history JSON +
rotation, from_config split/history). 987 green; module build clean
under -Werror.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
62 lines
2.1 KiB
C++
62 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include <Void/Log/Level.hpp>
|
|
|
|
#include <chrono>
|
|
#include <source_location>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Logging
|
|
{
|
|
/// Distinguishes regular messages from scope boundary markers.
|
|
///
|
|
/// Sinks may use Kind to render scope entries differently —
|
|
/// e.g. with indentation, brackets, or a dedicated colour.
|
|
///
|
|
/// Message — a regular log record
|
|
/// ScopeEnter — emitted when a named scope opens (scope() call)
|
|
/// ScopeExit — emitted when a named scope closes (RAII destructor)
|
|
enum class Kind { Message, ScopeEnter, ScopeExit };
|
|
|
|
/// A single, immutable log record produced by Logger and handed to
|
|
/// write() / on_flush(). All fields are populated by Logger before
|
|
/// dispatch; derived sinks treat Entry as read-only.
|
|
struct Entry {
|
|
Kind kind{Kind::Message};
|
|
Level level{Level::Debug};
|
|
|
|
/// Human-readable text. For ScopeEnter/ScopeExit entries this
|
|
/// is the scope name.
|
|
std::string message{};
|
|
|
|
/// Wall-clock time at the moment log() / scope() was called.
|
|
std::chrono::system_clock::time_point timestamp{
|
|
std::chrono::system_clock::now()};
|
|
|
|
/// Caller site captured automatically via
|
|
/// std::source_location::current().
|
|
std::source_location location{};
|
|
|
|
/// OS thread that produced this entry.
|
|
std::thread::id thread_id{std::this_thread::get_id()};
|
|
|
|
/// Frozen snapshot of the thread-local ScopedContext stack at log
|
|
/// time. Ordered outermost → innermost (index 0 is the root
|
|
/// scope). For ScopeEnter this already includes the new scope as
|
|
/// the last element; for ScopeExit it reflects the stack *before*
|
|
/// the pop.
|
|
std::vector<std::string> scopes{};
|
|
|
|
/// Structured key/value fields attached to this entry — a frozen
|
|
/// snapshot of the thread-local FieldScope stack at log time.
|
|
/// Formatters expose them as custom pattern tokens
|
|
/// (`{field:key}`) and in JSON/logfmt output. Later duplicate
|
|
/// keys win.
|
|
std::vector<std::pair<std::string, std::string>> fields{};
|
|
};
|
|
|
|
} // namespace Void::Logging
|