84 lines
2.1 KiB
C++
84 lines
2.1 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 <cstddef>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <span>
|
|
|
|
namespace Void::Logging
|
|
{
|
|
struct RotatingFileOptions {
|
|
std::size_t max_bytes = 1024 * 1024;
|
|
std::size_t max_files = 5;
|
|
Mode mode = Mode::Async;
|
|
std::size_t capacity = 512;
|
|
OverflowPolicy policy = OverflowPolicy::Block;
|
|
bool create_directories = true;
|
|
|
|
[[nodiscard]] static constexpr RotatingFileOptions
|
|
roadmap_defaults() noexcept
|
|
{
|
|
return RotatingFileOptions{
|
|
.max_bytes = 16ULL * 1024ULL * 1024ULL,
|
|
.max_files = 5,
|
|
.mode = Mode::Async,
|
|
.capacity = 4096,
|
|
.policy = OverflowPolicy::DropNewest,
|
|
.create_directories = false};
|
|
}
|
|
};
|
|
|
|
class RotatingFileLogger final : public Logger
|
|
{
|
|
public:
|
|
|
|
using Options = RotatingFileOptions;
|
|
|
|
explicit RotatingFileLogger(const std::filesystem::path& path,
|
|
Options options = {});
|
|
|
|
~RotatingFileLogger() noexcept override;
|
|
|
|
RotatingFileLogger(const RotatingFileLogger&) = delete;
|
|
RotatingFileLogger& operator=(const RotatingFileLogger&) = delete;
|
|
RotatingFileLogger(RotatingFileLogger&&) = delete;
|
|
RotatingFileLogger& operator=(RotatingFileLogger&&) = delete;
|
|
|
|
[[nodiscard]] static Void::Result::
|
|
Result<std::unique_ptr<RotatingFileLogger>, Void::Result::Error>
|
|
open(const std::filesystem::path& path, Options options = {});
|
|
|
|
void set_formatter(std::shared_ptr<const Formatter> formatter)
|
|
{
|
|
m_formatter = std::move(formatter);
|
|
}
|
|
|
|
[[nodiscard]] Void::Result::Result<void> reopen();
|
|
[[nodiscard]] Void::Result::Result<void> sync_files() override;
|
|
|
|
[[nodiscard]] const std::filesystem::path& path() const noexcept
|
|
{
|
|
return m_path;
|
|
}
|
|
|
|
protected:
|
|
|
|
void write(const Entry& entry) override;
|
|
void on_flush(std::span<const Entry> entries) override;
|
|
|
|
private:
|
|
|
|
void append(const Entry& entry);
|
|
|
|
std::filesystem::path m_path;
|
|
Detail::RollingFile m_file;
|
|
std::shared_ptr<const Formatter> m_formatter;
|
|
};
|
|
|
|
} // namespace Void::Logging
|