71 lines
1.7 KiB
C++
71 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Log/Formatter.hpp>
|
|
#include <Void/Log/Logger.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <memory>
|
|
#include <span>
|
|
|
|
namespace Void::Logging
|
|
{
|
|
class FileLogger final : public Logger
|
|
{
|
|
public:
|
|
|
|
explicit FileLogger(const std::filesystem::path& path,
|
|
Mode mode = Mode::Async,
|
|
std::size_t capacity = 512);
|
|
|
|
explicit FileLogger(const std::filesystem::path& path,
|
|
LoggerOptions options);
|
|
|
|
~FileLogger() noexcept override;
|
|
|
|
FileLogger(const FileLogger&) = delete;
|
|
FileLogger& operator=(const FileLogger&) = delete;
|
|
FileLogger(FileLogger&&) = delete;
|
|
FileLogger& operator=(FileLogger&&) = delete;
|
|
|
|
[[nodiscard]] static Void::Result::
|
|
Result<std::unique_ptr<FileLogger>, Void::Result::Error>
|
|
open(const std::filesystem::path& path,
|
|
Mode mode = Mode::Async,
|
|
std::size_t capacity = 512);
|
|
|
|
[[nodiscard]] static Void::Result::
|
|
Result<std::unique_ptr<FileLogger>, Void::Result::Error>
|
|
open(const std::filesystem::path& path,
|
|
LoggerOptions 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;
|
|
std::ofstream m_file;
|
|
std::shared_ptr<const Formatter> m_formatter;
|
|
};
|
|
|
|
} // namespace Void::Logging
|