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

+98 -16
View File
@@ -1,14 +1,45 @@
#include <Void/Log/Detail/RollingFile.hpp>
#include <algorithm>
#include <chrono>
#include <ctime>
#include <format>
#include <system_error>
#include <utility>
#include <vector>
namespace Void::Logging::Detail
{
namespace
{
std::tm to_utc_tm(std::time_t tt)
{
std::tm tm{};
#if defined(_WIN32)
gmtime_s(&tm, &tt);
#else
gmtime_r(&tt, &tm);
#endif
return tm;
}
} // namespace
RollingFile::RollingFile(std::filesystem::path path, RollingFileOptions opt)
: m_path(std::move(path)), m_opt(opt)
std::string format_timestamp(std::chrono::system_clock::time_point tp)
{
using namespace std::chrono;
const auto since_epoch = tp.time_since_epoch();
const auto secs = duration_cast<seconds>(since_epoch);
const auto us = duration_cast<microseconds>(since_epoch - secs);
const std::tm tm = to_utc_tm(system_clock::to_time_t(tp));
return std::format("{:04d}{:02d}{:02d}-{:02d}{:02d}{:02d}-{:06d}",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
static_cast<int>(us.count()));
}
RollingFile::RollingFile(std::filesystem::path path, RollingFileOptions opt,
bool self_rotate)
: m_path(std::move(path)), m_opt(opt), m_self_rotate(self_rotate)
{
}
@@ -23,6 +54,7 @@ namespace Void::Logging::Detail
std::filesystem::file_size(m_path, ec));
}
m_file.open(m_path, std::ios::app);
m_creationTime = std::chrono::system_clock::now();
return m_file.is_open();
}
@@ -34,7 +66,7 @@ namespace Void::Logging::Detail
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) {
if (m_self_rotate && m_current_bytes >= m_opt.max_bytes) {
rotate();
}
}
@@ -48,29 +80,79 @@ namespace Void::Logging::Detail
void RollingFile::rotate()
{
m_rotationTime = std::chrono::system_clock::now();
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);
// Create the history/ subdirectory next to the original file
auto parent = m_path.parent_path();
auto history_dir = parent / "history";
std::filesystem::create_directories(history_dir, ec);
// Build new name: <stem>-<timestamp>.<ext>
auto stem = m_path.stem();
auto ext = m_path.extension();
auto new_filename = stem.string() + "-" +
format_timestamp(m_rotationTime) + ext.string();
auto archived = history_dir / new_filename;
std::filesystem::rename(m_path, archived, ec);
prune_archives(); // now looks inside history/
} else {
std::filesystem::remove(m_path, ec);
}
m_file.open(m_path, std::ios::trunc);
m_current_bytes = 0;
m_creationTime = m_rotationTime;
}
void RollingFile::prune_archives()
{
// Prune only from the history/ subdirectory
auto parent = m_path.parent_path();
auto history_dir = parent / "history";
if (!std::filesystem::exists(history_dir)) {
return;
}
auto stem = m_path.stem().string();
std::vector<std::filesystem::path> archives;
std::error_code ec;
try {
for (const auto& entry :
std::filesystem::directory_iterator(history_dir, ec)) {
if (ec) {
return;
}
auto filename = entry.path().filename().string();
if (filename.starts_with(stem + "-")) {
archives.push_back(entry.path());
}
}
} catch (const std::filesystem::filesystem_error&) {
return; // best-effort pruning only
}
std::sort(archives.begin(), archives.end());
while (archives.size() > m_opt.max_files) {
std::filesystem::remove(archives.front(), ec);
archives.erase(archives.begin());
}
}
void RollingFile::snapshot_into(const std::filesystem::path& dir)
{
m_rotationTime = std::chrono::system_clock::now();
m_file.close();
std::error_code ec;
std::filesystem::create_directories(dir, ec);
std::filesystem::rename(m_path, dir / m_path.filename(), ec);
m_file.open(m_path, std::ios::trunc);
m_current_bytes = 0;
m_creationTime = m_rotationTime;
}
} // namespace Void::Logging::Detail
+34 -4
View File
@@ -24,7 +24,8 @@ namespace Void::Logging
SplitFileLogger::SplitFileLogger(std::filesystem::path dir, Options options)
: Logger(options.mode, options.capacity, options.policy),
m_dir(std::move(dir)), m_options(options)
m_dir(std::move(dir)), m_options(options),
m_epoch_start(std::chrono::system_clock::now())
{
std::error_code ec;
std::filesystem::create_directories(m_dir, ec);
@@ -34,9 +35,10 @@ namespace Void::Logging
}
if (m_options.combined) {
m_combined = std::make_unique<Detail::RollingFile>(
m_dir / "all.log",
m_dir / "trace.log",
Detail::RollingFileOptions{m_options.max_bytes,
m_options.max_files});
m_options.max_files},
/*self_rotate=*/false);
(void)m_combined->open();
}
Void::Log::debug("Log.Split", "split logging into '{}'",
@@ -63,7 +65,8 @@ namespace Void::Logging
Detail::RollingFile file{
m_dir / level_filename(level),
Detail::RollingFileOptions{m_options.max_bytes,
m_options.max_files}};
m_options.max_files},
/*self_rotate=*/false};
(void)file.open();
it = m_per_level.emplace(level, std::move(file)).first;
}
@@ -83,6 +86,33 @@ namespace Void::Logging
if (m_combined) {
m_combined->write(line);
}
maybe_rotate_group();
}
void SplitFileLogger::maybe_rotate_group()
{
bool any_exceeds = m_combined && m_combined->exceeds_limit();
if (!any_exceeds) {
for (const auto& [level, file] : m_per_level) {
if (file.exceeds_limit()) {
any_exceeds = true;
break;
}
}
}
if (!any_exceeds) {
return;
}
const auto snapshot_dir =
m_dir / "history" / Detail::format_timestamp(m_epoch_start);
for (auto& [level, file] : m_per_level) {
file.snapshot_into(snapshot_dir);
}
if (m_combined) {
m_combined->snapshot_into(snapshot_dir);
}
m_epoch_start = std::chrono::system_clock::now();
}
void SplitFileLogger::write(const Entry& entry)