change rotation
CI / GCC 16 / C++26 / default (push) Has been cancelled
CI / GCC 16 / C++26 / sanitizers (push) Has been cancelled
CI / GCC 16 / C++26 / werror (push) Has been cancelled

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-06 14:42:28 +03:00
1 parent a4f52cf174
commit 827c9e150a
7 files changed
+221 -33

No files matched your search

+55 -6
View File
@@ -1,8 +1,10 @@
#pragma once
#include <chrono>
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <string>
#include <string_view>
namespace Void::Logging::Detail
@@ -12,27 +14,70 @@ namespace Void::Logging::Detail
std::size_t max_files = 5;
};
/// Format a time point as a filesystem-safe, lexicographically (and
/// therefore chronologically) sortable UTC timestamp:
/// `YYYYMMDD-HHMMSS-ffffff` (microseconds).
[[nodiscard]] std::string
format_timestamp(std::chrono::system_clock::time_point tp);
/// A single append-only log file with size-based rotation. Not thread
/// safe — each sink owns one and drives it from its single worker.
///
/// On reaching max_bytes the files shift (`app.log` → `app.log.1` → …);
/// at most max_files rotated files are kept, the oldest is removed. With
/// max_files == 0 the file is truncated on rotation instead of kept.
/// Two rotation modes, selected by the `self_rotate` constructor
/// argument:
///
/// - self_rotate = true (default), "solo": write() rotates on its
/// own once the file exceeds max_bytes. The live file is closed
/// and renamed in place to `<name>-<rotation_time>` — same
/// directory, no subfolder. At most max_files such archives are
/// kept; the oldest (by name, which sorts chronologically) is
/// deleted. With max_files == 0 nothing is kept — the file is
/// just truncated.
///
/// - self_rotate = false, "grouped": write() only tracks size and
/// never rotates by itself. The owner polls exceeds_limit()
/// across a set of RollingFiles and, once any one of them crosses
/// the threshold, calls snapshot_into(dir) on every file in that
/// set so they all land — under their original names — in the
/// same directory at the same moment (e.g. so several files can
/// roll together into one shared `history/<creation_time>/`).
/// max_files does not apply to this path: nothing is ever deleted,
/// every snapshot is kept.
class RollingFile
{
public:
RollingFile(std::filesystem::path path, RollingFileOptions opt);
RollingFile(std::filesystem::path path, RollingFileOptions opt,
bool self_rotate = true);
/// Open for append (creating parent dirs). False on failure.
[[nodiscard]] bool open();
/// Append a line (a trailing '\n' is added) and rotate if the
/// file then exceeds max_bytes. No-op if not open.
/// Append a line (a trailing '\n' is added). In solo mode,
/// rotates automatically once max_bytes is exceeded; in
/// grouped mode this only updates the byte count — the
/// caller drives rotation via exceeds_limit()/snapshot_into().
/// No-op if not open.
void write(std::string_view line);
void flush();
/// True once the current file has reached max_bytes. Meant
/// for grouped (self_rotate == false) use, so an owner can
/// check a whole set of files and decide when to roll them
/// together.
[[nodiscard]] bool exceeds_limit() const noexcept
{
return m_current_bytes >= m_opt.max_bytes;
}
/// Move the live file — same name, unmodified — into `dir`
/// (created if needed), then continue with a fresh file at
/// path(). Ignores max_files; nothing is ever deleted here.
/// Intended to be called on every file in a group so they
/// land together.
void snapshot_into(const std::filesystem::path& dir);
[[nodiscard]] bool is_open() const noexcept
{
return m_file.is_open();
@@ -46,11 +91,15 @@ namespace Void::Logging::Detail
private:
void rotate();
void prune_archives();
std::filesystem::path m_path;
RollingFileOptions m_opt;
bool m_self_rotate;
std::ofstream m_file;
std::size_t m_current_bytes{0};
std::chrono::time_point<std::chrono::system_clock> m_creationTime;
std::chrono::time_point<std::chrono::system_clock> m_rotationTime;
};
} // namespace Void::Logging::Detail
+22 -3
View File
@@ -6,6 +6,7 @@
#include <Void/Log/Formatter.hpp>
#include <Void/Log/Logger.hpp>
#include <chrono>
#include <cstddef>
#include <filesystem>
#include <map>
@@ -21,6 +22,9 @@ namespace Void::Logging
/// to the per-level files.
bool combined = true;
std::size_t max_bytes = 1024 * 1024;
/// Unused. Rotation here is a synchronized, unbounded history
/// (see class comment) rather than a max_files-bounded rotation,
/// so this has no effect — kept only for API compatibility.
std::size_t max_files = 5;
Mode mode = Mode::Async;
std::size_t capacity = 512;
@@ -29,9 +33,18 @@ namespace Void::Logging
/// File sink that routes each entry to a per-level file
/// (`<dir>/<level>.log`, e.g. `error.log`, lazily created) and, when
/// enabled, also to a single combined `<dir>/all.log`. Each file rolls
/// independently. Async by default. An installed Formatter replaces the
/// default "[level] message" rendering (no color in files).
/// enabled, also to a single combined `<dir>/all.log`. Async by default.
/// An installed Formatter replaces the default "[level] message"
/// rendering (no color in files).
///
/// Rotation is synchronized across the whole group rather than
/// per-file: as soon as any one file — a per-level file or the
/// combined file — reaches max_bytes, every file currently open (even
/// ones well under the limit) is moved, under its own name unchanged,
/// into `<dir>/history/<creation_time>/`, where `<creation_time>`
/// marks when this batch of files started filling up. Fresh, empty
/// files then continue at their usual paths. Every snapshot is kept
/// indefinitely — see SplitFileOptions::max_files.
class SplitFileLogger final : public Logger
{
public:
@@ -74,11 +87,17 @@ namespace Void::Logging
Detail::RollingFile& file_for(Level level);
void append(const Entry& entry);
/// If any owned file has reached max_bytes, sweep every
/// owned file into a shared history/<creation_time> snapshot
/// directory and start each one fresh.
void maybe_rotate_group();
std::filesystem::path m_dir;
Options m_options;
std::map<Level, Detail::RollingFile> m_per_level;
std::unique_ptr<Detail::RollingFile> m_combined;
std::shared_ptr<const Formatter> m_formatter;
std::chrono::system_clock::time_point m_epoch_start;
};
} // namespace Void::Logging