6 #ifndef SL_LOG_OSTREAM_SINK_HPP
7 #define SL_LOG_OSTREAM_SINK_HPP
18 #include <string_view>
32 template <Record TRecord>
42 using FlushPolicy_t = std::unique_ptr<detail::AbstractFlushPolicyWrapper<Record_t>>;
48 return std::make_unique<detail::FlushPolicyWrapper<Record_t, AlwaysFlushPolicy>>();
94 std::scoped_lock lock{ m_FlushPolicyMx };
95 m_FlushPolicy = std::make_unique<detail::FlushPolicyWrapper<TRecord, TPolicy>>(std::forward<TPolicy>(policy));
104 std::scoped_lock lock{ m_FlushPolicyMx };
114 std::scoped_lock lock{ m_StreamMx };
128 template <
class TData>
131 std::scoped_lock lock{ m_StreamMx };
132 m_Stream << std::forward<TData>(data);
159 std::recursive_mutex m_StreamMx;
160 std::ostream& m_Stream;
162 std::mutex m_FlushPolicyMx;
165 void handleFlushPolicy(
const Record_t& record, std::size_t messageByteSize)
167 if (std::scoped_lock lock{ m_FlushPolicyMx }; !std::invoke(*m_FlushPolicy, record, messageByteSize))
175 m_Stream << std::flush;
176 m_FlushPolicy->flushed();
179 void writeMessage(
const Record_t& record, std::string_view message)
final override
181 const auto msgSize = std::size(message) *
sizeof(std::string_view::value_type);
183 std::scoped_lock lock{ m_StreamMx };
185 m_Stream << message <<
"\n";
187 handleFlushPolicy(record, msgSize);
Abstract Sink class which offers basic filtering, formatting functionality.
Definition: BasicSink.hpp:52
std::function< bool(const Record_t &)> Filter_t
Definition: BasicSink.hpp:59
std::function< std::string(const Record_t &)> Formatter_t
Definition: BasicSink.hpp:58
std::remove_cvref_t< TRecord > Record_t
Used Record type.
Definition: ISink.hpp:34
Sink interface class.
Definition: ISink.hpp:29
std::remove_cvref_t< TRecord > Record_t
Used Record type.
Definition: ISink.hpp:34
An std::ostream orientated Sink class which extends BasicSink.
Definition: OStreamSink.hpp:35
std::unique_ptr< detail::AbstractFlushPolicyWrapper< Record_t > > FlushPolicy_t
Definition: OStreamSink.hpp:42
void removeFlushPolicy()
Replaces the current Flush-Policy with the default one.
Definition: OStreamSink.hpp:102
void writeToStream(TData &&data)
Writes directly to the internal stream.
Definition: OStreamSink.hpp:129
~OStreamSink() noexcept=default
Default destructor.
void flush()
Flushes all pending output of the internal stream.
Definition: OStreamSink.hpp:112
OStreamSink(std::ostream &stream)
Constructor.
Definition: OStreamSink.hpp:56
std::remove_cvref_t< TRecord > Record_t
Used Record type.
Definition: ISink.hpp:34
virtual void beforeMessageWrite(const Record_t &record, std::string_view message)
Virtual method which will be called before the actual message is written to the stream.
Definition: OStreamSink.hpp:143
static constexpr FlushPolicy_t defaultFlushPolicy() noexcept
Definition: OStreamSink.hpp:46
void setFlushPolicy(TPolicy &&policy)
Sets the active Flush-Policy.
Definition: OStreamSink.hpp:92
virtual void afterMessageWrite(const Record_t &record, std::string_view message)
Virtual method which will be called after the actual message is written to the stream.
Definition: OStreamSink.hpp:154
Definition: BasicSink.hpp:22
concept FlushPolicyFor
Concept for invokable flush policies.
Definition: FlushPolicies.hpp:27