RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
RingBufferSink.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <cstddef>
6#include <deque>
7#include <vector>
8
9namespace RST::Log {
10
12 class RingBufferSink : public ASink {
13 public:
14 static constexpr std::size_t DEFAULT_MAX_BYTES = 1024 * 1024;
15
17 explicit RingBufferSink(std::size_t maxMessages, std::size_t maxBytes = DEFAULT_MAX_BYTES);
18
20 void replayInto(ISink& sink);
21
23 [[nodiscard]] std::vector<LogMessageBuffer> snapshot();
24 [[nodiscard]] std::size_t size();
25
26 protected:
27 void log(const LogMessage& message) override;
28
29 private:
30 std::deque<LogMessageBuffer> _messages;
31 std::size_t _bytes = 0;
32 std::size_t _maxMessages;
33 std::size_t _maxBytes;
34 };
35
36}
Base class for sinks: handles the level, the pattern and the locking.
Definition ASink.hpp:17
Interface of the log destinations; derive from ASink to write a new sink.
Definition ISink.hpp:14
Keeps the latest messages in memory, to replay them into another sink later.
Definition RingBufferSink.hpp:12
std::vector< LogMessageBuffer > snapshot()
Copies of the kept messages, oldest first.
static constexpr std::size_t DEFAULT_MAX_BYTES
Definition RingBufferSink.hpp:14
void replayInto(ISink &sink)
Writes the kept messages into sink, oldest first.
RingBufferSink(std::size_t maxMessages, std::size_t maxBytes=DEFAULT_MAX_BYTES)
Keeps up to maxMessages messages and about maxBytes bytes, dropping the oldest first.
void log(const LogMessage &message) override
Writes one message; called with the sink mutex locked.
Thread-safe logging: loggers, sinks, patterns and macros.
Definition Terminal.hpp:8
A log record as passed to the sinks.
Definition LogMessage.hpp:18