6 #ifndef SL_LOG_FLUSH_POLICIES_HPP
7 #define SL_LOG_FLUSH_POLICIES_HPP
18 #include <type_traits>
26 template <
class T,
class TRecord>
29 std::predicate<T, const TRecord&, std::size_t> &&
36 namespace sl::log::detail
38 template <Record TRecord>
39 class AbstractFlushPolicyWrapper
42 using Record_t = std::remove_cvref_t<TRecord>;
44 virtual ~AbstractFlushPolicyWrapper() noexcept = default;
46 AbstractFlushPolicyWrapper(const AbstractFlushPolicyWrapper&) = delete;
47 AbstractFlushPolicyWrapper& operator =(const AbstractFlushPolicyWrapper&) = delete;
48 AbstractFlushPolicyWrapper(AbstractFlushPolicyWrapper&&) = delete;
49 AbstractFlushPolicyWrapper& operator =(AbstractFlushPolicyWrapper&&) = delete;
51 virtual
bool operator ()(const
Record_t& record, std::
size_t messageByteSize) = 0;
52 virtual
void flushed() = 0;
55 AbstractFlushPolicyWrapper() noexcept = default;
59 class FlushPolicyWrapper final :
60 public AbstractFlushPolicyWrapper<TRecord>
62 using Super = AbstractFlushPolicyWrapper<TRecord>;
65 using FlushPolicy_t = std::remove_reference_t<TFlushPolicy>;
67 explicit FlushPolicyWrapper() noexcept(std::is_nothrow_constructible_v<FlushPolicy_t>) :
72 explicit FlushPolicyWrapper(TFlushPolicy flushPolicy) noexcept(std::is_nothrow_move_constructible_v<FlushPolicy_t>) :
73 m_FlushPolicy{ std::move(flushPolicy) }
77 bool operator ()(
const Record_t& record, std::size_t messageByteSize)
override
79 return std::invoke(m_FlushPolicy, record, messageByteSize);
82 void flushed()
override
84 m_FlushPolicy.flushed();
88 FlushPolicy_t m_FlushPolicy;
91 template <auto VConstant>
92 struct ConstantInvokable
94 template <
class... TArgs>
95 constexpr
auto operator()(TArgs&&...) const noexcept
101 struct PredProjInvocation
103 template <
class TPredicate,
class TProjection,
class TProjected,
class... TArgs>
104 constexpr
auto operator ()(TPredicate& pred, TProjection& proj,
const TProjected& projected, TArgs&&... args)
const
106 return std::invoke(pred, std::invoke(proj, projected), std::forward<TArgs>(args)...);
110 struct PredProjInvocationIgnoreArgs
112 template <
class TPredicate,
class TProjection,
class TProjected,
class... TArgs>
113 constexpr
auto operator ()(TPredicate& pred, TProjection& proj,
const TProjected& projected, TArgs&&...)
const
115 return std::invoke(pred, std::invoke(proj, projected));
142 template <
class TPredicate,
class TProjection = std::
identity,
class TInvocationRule = detail::PredProjInvocation>
158 TProjection projection = Projection_t{},
159 TInvocationRule invocation = InvocationRule_t{}
161 noexcept(std::is_nothrow_move_constructible_v<TPredicate> && std::is_nothrow_move_constructible_v<TProjection> &&
162 std::is_nothrow_move_constructible_v<TInvocationRule>) :
163 m_Predicate{ std::move(predicate) },
164 m_Projection{ std::move(projection) },
165 m_Invocation{ std::move(invocation) }
176 template <
class... TArgs>
177 requires std::constructible_from<Predicate_t, TArgs...>
182 noexcept(std::is_nothrow_constructible_v<TPredicate, TArgs...> && std::is_nothrow_constructible_v<TProjection> &&
183 std::is_nothrow_constructible_v<TInvocationRule>) :
184 m_Predicate{ std::forward<TArgs>(args)... }
196 template <Record TRecord>
197 bool operator ()(
const TRecord& record, std::size_t messageByteSize)
199 return m_Invocation(m_Predicate, m_Projection, record, messageByteSize);
210 Predicate_t m_Predicate;
211 Projection_t m_Projection;
213 InvocationRule_t m_Invocation;
222 template <
class TAlgorithm,
class... TFlushPolicies>
233 TFlushPolicies ... policies
235 noexcept(std::is_nothrow_constructible_v<Algorithm_t> && (std::is_nothrow_move_constructible_v<TFlushPolicies> && ...)) :
237 m_Policies{ std::move(policies)... }
247 TAlgorithm algorithm,
248 TFlushPolicies ... policies
250 noexcept(std::is_nothrow_move_constructible_v<Algorithm_t> && (std::is_nothrow_move_constructible_v<TFlushPolicies> && ...
253 m_Algorithm{ std::move(algorithm) },
254 m_Policies{ std::forward<TFlushPolicies>(policies)... }
266 template <Record TRecord>
267 bool operator ()(
const TRecord& record, std::size_t messageByteSize)
269 return std::invoke(m_Algorithm, m_Policies, record, messageByteSize);
277 detail::TupleForEach{}(
291 constexpr
bool empty() const noexcept
293 return std::tuple_size_v<decltype(m_Policies)> == 0;
301 constexpr std::size_t
size() const noexcept
303 return std::tuple_size_v<decltype(m_Policies)>;
307 Algorithm_t m_Algorithm;
308 std::tuple<TFlushPolicies...> m_Policies;
315 template <
class... TFlushPolicies>
319 using Algorithm_t = detail::TupleAllOf;
327 TFlushPolicies ... policies
329 noexcept((std::is_nothrow_move_constructible_v<TFlushPolicies> && ...)) :
339 template <
class... TFlushPolicies>
343 using Algorithm_t = detail::TupleAnyOf;
351 TFlushPolicies ... policies
353 noexcept((std::is_nothrow_move_constructible_v<TFlushPolicies> && ...)) :
363 template <
class... TFlushPolicies>
367 using Algorithm_t = detail::TupleNoneOf;
375 TFlushPolicies ... policies
377 noexcept((std::is_nothrow_move_constructible_v<TFlushPolicies> && ...)) :
398 template <Record TRecord, std::predicate<const RecordMessage_t<TRecord>&> TUnaryPredicate>
402 std::forward<TUnaryPredicate>(predicate),
404 detail::PredProjInvocationIgnoreArgs{}
418 template <Record TRecord, std::predicate<const RecordSeverity_t<TRecord>&> TUnaryPredicate>
422 std::forward<TUnaryPredicate>(predicate),
424 detail::PredProjInvocationIgnoreArgs{}
438 template <Record TRecord, std::predicate<const RecordChannel_t<TRecord>&> TUnaryPredicate>
442 std::forward<TUnaryPredicate>(predicate),
444 detail::PredProjInvocationIgnoreArgs{}
458 template <Record TRecord, std::predicate<const RecordTimePo
int_t<TRecord>&> TUnaryPredicate>
462 std::forward<TUnaryPredicate>(predicate),
464 detail::PredProjInvocationIgnoreArgs{}
488 template <
class TRep,
class TPeriod>
490 m_Duration{ std::chrono::duration_cast<
Duration_t>(duration) }
501 template <Record TRecord>
502 bool operator ()(
const TRecord& record, std::size_t messageByteSize) noexcept
504 const auto now = Clock_t::now();
505 return m_Duration <= now - m_StartPoint;
513 m_StartPoint = Clock_t::now();
517 TimePoint_t m_StartPoint = Clock_t::now();
518 Duration_t m_Duration;
533 m_ByteThreshold{ byteThreshold }
544 template <Record TRecord>
545 bool operator ()(
const TRecord& record, std::size_t messageByteSize) noexcept
547 m_ByteCount += messageByteSize;
548 return m_ByteThreshold <= m_ByteCount;
560 std::size_t m_ByteThreshold;
561 std::size_t m_ByteCount = 0;
A Flush-Policy which acts on accumulated byte count.
Definition: FlushPolicies.hpp:526
void flushed() noexcept
Resets the internal byte counter.
Definition: FlushPolicies.hpp:554
ByteCountFlushPolicy(std::size_t byteThreshold) noexcept
Constructor.
Definition: FlushPolicies.hpp:532
Convenience type for chaining multiple FlushPolicies with AND.
Definition: FlushPolicies.hpp:318
constexpr FlushPolicyAllOf(TFlushPolicies ... policies) noexcept((std::is_nothrow_move_constructible_v< TFlushPolicies > &&...))
Constructor.
Definition: FlushPolicies.hpp:326
Convenience type for chaining multiple FlushPolicies with OR.
Definition: FlushPolicies.hpp:342
constexpr FlushPolicyAnyOf(TFlushPolicies ... policies) noexcept((std::is_nothrow_move_constructible_v< TFlushPolicies > &&...))
Constructor.
Definition: FlushPolicies.hpp:350
Chains multiple FlushPolicy objects together.
Definition: FlushPolicies.hpp:224
constexpr void flushed()
Calls flushed on all FlushPolicy objects.
Definition: FlushPolicies.hpp:275
constexpr FlushPolicyChain(TAlgorithm algorithm, TFlushPolicies ... policies) noexcept(std::is_nothrow_move_constructible_v< Algorithm_t > &&(std::is_nothrow_move_constructible_v< TFlushPolicies > &&...))
Constructor overload.
Definition: FlushPolicies.hpp:246
constexpr std::size_t size() const noexcept
Obtains the amount of attached FlushPolicy objects.
Definition: FlushPolicies.hpp:301
constexpr bool empty() const noexcept
Returns whether the are no FlushPolicy objects attached.
Definition: FlushPolicies.hpp:291
constexpr FlushPolicyChain(TFlushPolicies ... policies) noexcept(std::is_nothrow_constructible_v< Algorithm_t > &&(std::is_nothrow_move_constructible_v< TFlushPolicies > &&...))
Constructor.
Definition: FlushPolicies.hpp:232
TAlgorithm Algorithm_t
Definition: FlushPolicies.hpp:226
A customizable FlushPolicy class.
Definition: FlushPolicies.hpp:144
std::remove_cvref_t< TPredicate > Predicate_t
Definition: FlushPolicies.hpp:146
requires std::constructible_from< Predicate_t, TArgs... > FlushPolicy(std::in_place_t _, TArgs &&... args) noexcept(std::is_nothrow_constructible_v< TPredicate, TArgs... > &&std::is_nothrow_constructible_v< TProjection > &&std::is_nothrow_constructible_v< TInvocationRule >)
Constructor overload for in_place predicate initializing.
Definition: FlushPolicies.hpp:178
constexpr void flushed() const noexcept
NoOp.
Definition: FlushPolicies.hpp:205
std::remove_cvref_t< TInvocationRule > InvocationRule_t
Definition: FlushPolicies.hpp:148
FlushPolicy(TPredicate predicate=Predicate_t{}, TProjection projection=Projection_t{}, TInvocationRule invocation=InvocationRule_t{}) noexcept(std::is_nothrow_move_constructible_v< TPredicate > &&std::is_nothrow_move_constructible_v< TProjection > &&std::is_nothrow_move_constructible_v< TInvocationRule >)
Constructor.
Definition: FlushPolicies.hpp:156
std::remove_cvref_t< TProjection > Projection_t
Definition: FlushPolicies.hpp:147
Convenience type for chaining multiple FlushPolicies with NOR.
Definition: FlushPolicies.hpp:366
constexpr FlushPolicyNoneOf(TFlushPolicies ... policies) noexcept((std::is_nothrow_move_constructible_v< TFlushPolicies > &&...))
Constructor.
Definition: FlushPolicies.hpp:374
A Flush-Policy which acts on a durations.
Definition: FlushPolicies.hpp:475
void flushed() noexcept
Restarts the internal duration clock.
Definition: FlushPolicies.hpp:511
TimedFlushPolicy(std::chrono::duration< TRep, TPeriod > duration)
Constructs the object with the specified duration threshold.
Definition: FlushPolicies.hpp:489
std::chrono::steady_clock Clock_t
Definition: FlushPolicies.hpp:478
Clock_t::time_point TimePoint_t
Definition: FlushPolicies.hpp:479
std::chrono::milliseconds Duration_t
Definition: FlushPolicies.hpp:477
constexpr auto makeChannelFlushPolicyFor(TUnaryPredicate &&predicate)
Factory function for creating Flush-Policies based on Record::channel member.
Definition: FlushPolicies.hpp:439
constexpr auto makeSeverityFlushPolicyFor(TUnaryPredicate &&predicate)
Factory function for creating Flush-Policies based on Record::severity member.
Definition: FlushPolicies.hpp:419
constexpr auto makeTimePointFlushPolicyFor(TUnaryPredicate &&predicate)
Factory function for creating Flush-Policies based on Record::timePoint member.
Definition: FlushPolicies.hpp:459
constexpr auto makeMessageFlushPolicyFor(TUnaryPredicate &&predicate)
Factory function for creating Flush-Policies based on Record::message member.
Definition: FlushPolicies.hpp:399
BaseRecord< SevLvl, std::string > Record_t
Prepared Record type.
Definition: PresetTypes.hpp:52
concept Record
Concept which all the necessary concepts for Record types.
Definition: Record.hpp:204
Definition: BasicSink.hpp:22
concept FlushPolicyFor
Concept for invokable flush policies.
Definition: FlushPolicies.hpp:27
Provides a layer of abstraction to Record member setter.
Definition: Record.hpp:118