Logging: colors, configurable/custom pattern tokens, split + history sinks

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>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-14 00:24:31 +03:00
1 parent ff6960a2af
commit 6b5f61f00a
22 files changed
+1384 -233

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
#include <Void/Log/Detail/RollingFile.hpp>
#include <format>
#include <system_error>
#include <utility>
namespace Void::Logging::Detail
{
RollingFile::RollingFile(std::filesystem::path path, RollingFileOptions opt)
: m_path(std::move(path)), m_opt(opt)
{
}
bool RollingFile::open()
{
std::error_code ec;
if (m_path.has_parent_path()) {
std::filesystem::create_directories(m_path.parent_path(), 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);
return m_file.is_open();
}
void RollingFile::write(std::string_view line)
{
if (!m_file.is_open()) {
return;
}
m_file.write(line.data(), static_cast<std::streamsize>(line.size()));
m_file.put('\n');
m_current_bytes += line.size() + 1;
if (m_current_bytes >= m_opt.max_bytes) {
rotate();
}
}
void RollingFile::flush()
{
if (m_file.is_open()) {
m_file.flush();
}
}
void RollingFile::rotate()
{
m_file.close();
std::error_code ec;
const auto numbered = [&](std::size_t n) {
auto p = m_path;
p += std::format(".{}", n);
return p;
};
std::filesystem::remove(numbered(m_opt.max_files), ec);
for (std::size_t n = m_opt.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_opt.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;
}
} // namespace Void::Logging::Detail