Add configurable split file logging
This commit is contained in:
3 files changed
+68
-8
No files matched your search
@@ -1,10 +1,12 @@
|
||||
#include <Void/Log/SplitFileLogger.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Logging
|
||||
{
|
||||
@@ -35,7 +37,7 @@ namespace Void::Logging
|
||||
}
|
||||
if (m_options.combined) {
|
||||
m_combined = std::make_unique<Detail::RollingFile>(
|
||||
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<std::filesystem::path> 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)
|
||||
|
||||
Reference in new issue
Block a user