Files
Library/include/Void/Log/SplitFileLogger.hpp
T

104 lines
3.3 KiB
C++

#pragma once
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <Void/Log/Detail/RollingFile.hpp>
#include <Void/Log/Formatter.hpp>
#include <Void/Log/Logger.hpp>
#include <chrono>
#include <cstddef>
#include <filesystem>
#include <map>
#include <memory>
#include <span>
namespace Void::Logging
{
/// Options for SplitFileLogger (namespace scope to avoid the GCC
/// nested-default-arg trap).
struct SplitFileOptions {
/// One combined file `<dir>/all.log` with every level, in addition
/// to the per-level files.
bool combined = true;
std::size_t max_bytes = 1024 * 1024;
/// 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;
OverflowPolicy policy = OverflowPolicy::Block;
};
/// 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`. 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. The oldest snapshots are
/// pruned so at most SplitFileOptions::max_files remain.
class SplitFileLogger final : public Logger
{
public:
using Options = SplitFileOptions;
explicit SplitFileLogger(std::filesystem::path dir,
Options options = {});
~SplitFileLogger() noexcept override;
SplitFileLogger(const SplitFileLogger&) = delete;
SplitFileLogger& operator=(const SplitFileLogger&) = delete;
SplitFileLogger(SplitFileLogger&&) = delete;
SplitFileLogger& operator=(SplitFileLogger&&) = delete;
/// Non-throwing factory (fails if the directory cannot be
/// created).
[[nodiscard]] static Void::Result::
Result<std::unique_ptr<SplitFileLogger>, Void::Result::Error>
open(std::filesystem::path dir, Options options = {});
void set_formatter(std::shared_ptr<const Formatter> formatter)
{
m_formatter = std::move(formatter);
}
[[nodiscard]] const std::filesystem::path& dir() const noexcept
{
return m_dir;
}
protected:
void write(const Entry& entry) override;
void on_flush(std::span<const Entry> entries) override;
private:
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();
void prune_history();
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