6 #ifndef SL_LOG_BASIC_SINK_HPP
7 #define SL_LOG_BASIC_SINK_HPP
30 template <
class T,
class TRecord>
33 std::is_invocable_r_v<std::string, T, const TRecord&>;
38 template <
class T,
class TRecord>
41 std::predicate<T, const TRecord&>;
49 template <Record TRecord>
66 using namespace std::chrono;
67 using namespace std::chrono_literals;
71 const auto hour = duration_cast<hours>(today);
72 const auto minute = duration_cast<minutes>(today) % 1h;
73 const auto second = duration_cast<seconds>(today) % 1min;
74 const auto millisecond = duration_cast<milliseconds>(today) % 1s;
76 std::ostringstream out;
77 out << std::setfill(
'0') <<
78 std::setw(2) << hour.count() <<
":" <<
79 std::setw(2) << minute.count() <<
":" <<
80 std::setw(2) << second.count() <<
"." <<
81 std::setw(3) << millisecond.count() <<
86 return std::move(out).str();
92 return [](
const Record_t& rec) {
return true; };
135 if (std::scoped_lock lock{ m_FilterMx }; !std::invoke(m_Filter, record))
138 const auto message = [&]
140 std::scoped_lock lock{ m_FormatterMx };
141 return std::invoke(m_Formatter, record);
179 template <RecordFormatterFor<Record_t> TFormatter>
182 std::scoped_lock lock{ m_FormatterMx };
183 m_Formatter = std::forward<TFormatter>(formatter);
191 std::scoped_lock lock{ m_FormatterMx };
206 template <RecordFilterFor<Record_t> TFilter>
209 std::scoped_lock lock{ m_FilterMx };
210 m_Filter = std::forward<TFilter>(filter);
218 std::scoped_lock lock{ m_FilterMx };
233 std::mutex m_FormatterMx;
236 std::mutex m_FilterMx;
239 std::atomic_bool m_Enabled{
false };
Abstract Sink class which offers basic filtering, formatting functionality.
Definition: BasicSink.hpp:52
void setFilter(TFilter &&filter)
Sets the active filter.
Definition: BasicSink.hpp:207
void removeFilter()
Replaces the active filter with the default one.
Definition: BasicSink.hpp:216
void setEnabled(bool enable=true) noexcept final override
Enables or disables the Sink object.
Definition: BasicSink.hpp:152
std::function< bool(const Record_t &)> Filter_t
Definition: BasicSink.hpp:59
static constexpr Formatter_t defaultFormatter() noexcept
Definition: BasicSink.hpp:62
bool isEnabled() const noexcept final override
Checks if the Sink object is enabled.
Definition: BasicSink.hpp:162
BasicSink() noexcept=default
Constructor.
void removeFormatter()
Replaces the active formatter with the default one.
Definition: BasicSink.hpp:189
void log(const Record_t &record) final override
Handles the given Record object.
Definition: BasicSink.hpp:130
std::function< std::string(const Record_t &)> Formatter_t
Definition: BasicSink.hpp:58
void setFormatter(TFormatter &&formatter)
Sets the active formatter.
Definition: BasicSink.hpp:180
virtual void writeMessage(const Record_t &record, std::string_view message)=0
This function will be called when the actual message should be printed.
static constexpr Filter_t defaultFilter() noexcept
Definition: BasicSink.hpp:90
Sink interface class.
Definition: ISink.hpp:29
std::remove_cvref_t< TRecord > Record_t
Used Record type.
Definition: ISink.hpp:34
concept RecordFilterFor
Concept for invokable filter objects.
Definition: BasicSink.hpp:39
concept RecordFormatterFor
Concept for invokable formatter objects.
Definition: BasicSink.hpp:31
Definition: BasicSink.hpp:22
Provides a layer of abstraction to Record member setter.
Definition: Record.hpp:118
constexpr static auto timePoint
Definition: Record.hpp:122
constexpr static auto message
Definition: Record.hpp:119
constexpr static auto severity
Definition: Record.hpp:120