diff --git a/include/Void/Log/SplitFileLogger.hpp b/include/Void/Log/SplitFileLogger.hpp
index d9437fa..057d5c1 100644
--- a/include/Void/Log/SplitFileLogger.hpp
+++ b/include/Void/Log/SplitFileLogger.hpp
@@ -22,9 +22,8 @@ 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.
+ /// Maximum number of synchronized history snapshots retained.
+ /// Zero removes a snapshot immediately after rotation.
std::size_t max_files = 5;
Mode mode = Mode::Async;
std::size_t capacity = 512;
@@ -43,8 +42,8 @@ namespace Void::Logging
/// ones well under the limit) is moved, under its own name unchanged,
/// into `
/history//`, where ``
/// 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.
+ /// files then continue at their usual paths. The oldest snapshots are
+ /// pruned so at most SplitFileOptions::max_files remain.
class SplitFileLogger final : public Logger
{
public:
@@ -91,6 +90,7 @@ namespace Void::Logging
/// owned file into a shared history/ snapshot
/// directory and start each one fresh.
void maybe_rotate_group();
+ void prune_history();
std::filesystem::path m_dir;
Options m_options;
diff --git a/src/Log/SplitFileLogger.cpp b/src/Log/SplitFileLogger.cpp
index 70119b2..fe2da32 100644
--- a/src/Log/SplitFileLogger.cpp
+++ b/src/Log/SplitFileLogger.cpp
@@ -1,10 +1,12 @@
#include
#include
+#include
#include
#include
#include
#include
+#include
namespace Void::Logging
{
@@ -35,7 +37,7 @@ namespace Void::Logging
}
if (m_options.combined) {
m_combined = std::make_unique(
- m_dir / "trace.log",
+ m_dir / "all.log",
Detail::RollingFileOptions{m_options.max_bytes,
m_options.max_files},
/*self_rotate=*/false);
@@ -113,6 +115,36 @@ namespace Void::Logging
m_combined->snapshot_into(snapshot_dir);
}
m_epoch_start = std::chrono::system_clock::now();
+ prune_history();
+ }
+
+ void SplitFileLogger::prune_history()
+ {
+ const auto history_dir = m_dir / "history";
+ std::error_code ec;
+ if (!std::filesystem::exists(history_dir, ec) || ec) {
+ return;
+ }
+
+ std::vector snapshots;
+ for (std::filesystem::directory_iterator it{history_dir, ec}, end;
+ !ec && it != end; it.increment(ec)) {
+ if (it->is_directory(ec) && !ec) {
+ snapshots.push_back(it->path());
+ }
+ }
+ if (ec) {
+ return;
+ }
+
+ std::ranges::sort(snapshots);
+ while (snapshots.size() > m_options.max_files) {
+ std::filesystem::remove_all(snapshots.front(), ec);
+ if (ec) {
+ return;
+ }
+ snapshots.erase(snapshots.begin());
+ }
}
void SplitFileLogger::write(const Entry& entry)
diff --git a/tests/standalone/Log/FileSinks.cpp b/tests/standalone/Log/FileSinks.cpp
index dba0130..6a3e37f 100644
--- a/tests/standalone/Log/FileSinks.cpp
+++ b/tests/standalone/Log/FileSinks.cpp
@@ -38,6 +38,19 @@ namespace
}
return count;
}
+
+ std::size_t snapshot_count(const std::filesystem::path& dir)
+ {
+ const auto history = dir / "history";
+ if (!std::filesystem::exists(history)) {
+ return 0;
+ }
+ std::size_t count = 0;
+ for (const auto& entry : std::filesystem::directory_iterator(history)) {
+ count += entry.is_directory() ? 1U : 0U;
+ }
+ return count;
+ }
} // namespace
class SplitFileLoggerTests : public Void::Testing::Fixture
@@ -81,7 +94,7 @@ class SplitFileLoggerTests : public Void::Testing::Fixture
log.info("info one");
log.error("error one");
}
- const auto all = slurp(dir / "trace.log");
+ const auto all = slurp(dir / "all.log");
Void::Testing::expect(all.find("info one") != std::string::npos);
Void::Testing::expect(all.find("error one") != std::string::npos);
}
@@ -94,9 +107,24 @@ class SplitFileLoggerTests : public Void::Testing::Fixture
.mode = Void::Logging::Mode::Immediate}};
log.info("solo");
}
- Void::Testing::expect(!std::filesystem::exists(dir / "trace.log"));
+ Void::Testing::expect(!std::filesystem::exists(dir / "all.log"));
Void::Testing::expect(std::filesystem::exists(dir / "info.log"));
}
+
+ Void::Testing::Test bounds_synchronized_history()
+ {
+ {
+ SplitFileLogger log{dir,
+ {.max_bytes = 32,
+ .max_files = 2,
+ .mode = Void::Logging::Mode::Immediate}};
+ for (int i = 0; i < 20; ++i) {
+ log.info("rotation payload " + std::to_string(i));
+ }
+ }
+ Void::Testing::expect(snapshot_count(dir) >= 1);
+ Void::Testing::expect(snapshot_count(dir) <= 2);
+ }
};
class HistoryLoggerTests : public Void::Testing::Fixture