RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
RotatingFileSink.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <cstddef>
6#include <fstream>
7#include <string>
8#include <string_view>
9
10namespace RST::Log {
11
13 class RotatingFileSink : public ASink {
14 public:
15 static constexpr std::size_t DEFAULT_MAX_SIZE = 10 * 1024 * 1024;
16 static constexpr std::size_t DEFAULT_MAX_FILES = 5;
17
20 explicit RotatingFileSink(std::string_view filepath, std::size_t maxSize = DEFAULT_MAX_SIZE, std::size_t maxFiles = DEFAULT_MAX_FILES);
22
23 [[nodiscard]] std::size_t currentSize() const noexcept { return _currentSize; }
24 [[nodiscard]] std::size_t maxSize() const noexcept { return _maxSize; }
25 [[nodiscard]] std::size_t maxFiles() const noexcept { return _maxFiles; }
26
27 protected:
28 void log(const LogMessage& message) override;
29 void flushSink() override;
30
31 private:
32 void rotate() noexcept;
33 [[nodiscard]] std::string backupPath(std::size_t index) const;
34
35 std::string _baseFilepath;
36 std::ofstream _file;
37 std::size_t _maxSize;
38 std::size_t _maxFiles;
39 std::size_t _currentSize = 0;
40 bool _rotationFailureReported = false;
41 };
42
43}
Base class for sinks: handles the level, the pattern and the locking.
Definition ASink.hpp:17
Writes to a file and rotates it when it grows too large.
Definition RotatingFileSink.hpp:13
void flushSink() override
Flushes the output; called with the sink mutex locked.
static constexpr std::size_t DEFAULT_MAX_FILES
Definition RotatingFileSink.hpp:16
std::size_t currentSize() const noexcept
Definition RotatingFileSink.hpp:23
void log(const LogMessage &message) override
Writes one message; called with the sink mutex locked.
std::size_t maxFiles() const noexcept
Definition RotatingFileSink.hpp:25
std::size_t maxSize() const noexcept
Definition RotatingFileSink.hpp:24
static constexpr std::size_t DEFAULT_MAX_SIZE
Definition RotatingFileSink.hpp:15
RotatingFileSink(std::string_view filepath, std::size_t maxSize=DEFAULT_MAX_SIZE, std::size_t maxFiles=DEFAULT_MAX_FILES)
Starts a new file past maxSize bytes, keeping maxFiles backups; <filepath>.1.log is the newest.
Thread-safe logging: loggers, sinks, patterns and macros.
Definition Terminal.hpp:8
A log record as passed to the sinks.
Definition LogMessage.hpp:18