Files
Library/src/Log/SplitFileLogger.cpp
T

173 lines
4.0 KiB
C++

#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
{
namespace
{
std::string level_filename(Level level)
{
std::string name{to_string(level)};
for (auto& c : name) {
c = static_cast<char>(
std::tolower(static_cast<unsigned char>(c)));
}
return name + ".log";
}
} // namespace
SplitFileLogger::SplitFileLogger(std::filesystem::path dir, Options options)
: Logger(options.mode, options.capacity, options.policy),
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);
if (ec) {
throw std::runtime_error(std::format(
"SplitFileLogger: cannot create '{}'", m_dir.string()));
}
if (m_options.combined) {
m_combined = std::make_unique<Detail::RollingFile>(
m_dir / "all.log",
Detail::RollingFileOptions{m_options.max_bytes,
m_options.max_files},
/*self_rotate=*/false);
(void)m_combined->open();
}
Void::Log::debug("Log.Split", "split logging into '{}'",
m_dir.string());
}
Void::Result::Result<std::unique_ptr<SplitFileLogger>, Void::Result::Error>
SplitFileLogger::open(std::filesystem::path dir, Options options)
{
try {
return Void::Result::ok(
std::make_unique<SplitFileLogger>(std::move(dir), options));
} catch (const std::exception& e) {
return Void::Result::fail(std::string{e.what()});
}
}
SplitFileLogger::~SplitFileLogger() noexcept { flush(); }
Detail::RollingFile& SplitFileLogger::file_for(Level level)
{
auto it = m_per_level.find(level);
if (it == m_per_level.end()) {
Detail::RollingFile file{
m_dir / level_filename(level),
Detail::RollingFileOptions{m_options.max_bytes,
m_options.max_files},
/*self_rotate=*/false};
(void)file.open();
it = m_per_level.emplace(level, std::move(file)).first;
}
return it->second;
}
void SplitFileLogger::append(const Entry& entry)
{
std::string line;
if (m_formatter) {
line = m_formatter->format(entry);
} else {
line =
std::format("[{}] {}", to_string(entry.level), entry.message);
}
file_for(entry.level).write(line);
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();
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)
{
append(entry);
file_for(entry.level).flush();
if (m_combined) {
m_combined->flush();
}
}
void SplitFileLogger::on_flush(std::span<const Entry> entries)
{
for (const auto& e : entries) {
append(e);
}
for (auto& [level, file] : m_per_level) {
file.flush();
}
if (m_combined) {
m_combined->flush();
}
}
} // namespace Void::Logging