6 #ifndef SL_LOG_CONSOLE_SINK_HPP
7 #define SL_LOG_CONSOLE_SINK_HPP
16 #include "third_party/rang/rang.hpp"
22 #include <unordered_map>
96 template <
class T,
class TRecord>
99 std::is_invocable_r_v<ConsoleTextStyle, T, const TRecord&>;
111 namespace sl::log::detail
137 detail::applyStyle(out, style.
style);
138 detail::applyTextColor(out, style.
textColor);
139 detail::applyBackgroundColor(out, style.
bgColor);
153 template <
class TProjection,
class TTable>
159 using Key_t =
typename Table_t::key_type;
167 m_Projection{ std::move(projection) },
168 m_StyleTable{ std::move(table) }
179 template <Record TRecord>
182 if (
auto itr = m_StyleTable.find(std::invoke(m_Projection, record)); itr != std::end(m_StyleTable))
197 m_StyleTable.insert_or_assign(std::move(key), std::move(style));
219 std::invocable<const TRecord&> TProjection,
220 class TTable = std::unordered_map<
221 std::remove_cvref_t<std::invoke_result_t<TProjection, const TRecord&>>,
236 template <Record TRecord>
301 std::scoped_lock lock{ m_TextStylePolicyMx };
302 m_TextStylePolicy = std::forward<TStylePolicy>(policy);
310 std::scoped_lock lock{ m_TextStylePolicyMx };
315 std::mutex m_TextStylePolicyMx;
318 void beforeMessageWrite(
const Record_t& record, std::string_view message)
override
320 std::scoped_lock lock{ m_TextStylePolicyMx };
321 assert(m_TextStylePolicy &&
"TextStylePolicy must not be empty.");
322 std::cout << std::invoke(m_TextStylePolicy, record);
325 void afterMessageWrite(
const Record_t& record, std::string_view message)
override
335 namespace sl::log::detail
340 if (color < Color::brightBlack)
342 const auto begin =
static_cast<unsigned>(rang::fg::black);
343 out << static_cast<rang::fg>(
static_cast<unsigned>(color) + begin);
347 const auto begin =
static_cast<unsigned>(rang::fgB::black);
348 const auto localColor =
static_cast<Color
>(begin +
349 static_cast<unsigned>(color) -
350 static_cast<unsigned>(Color::brightBlack));
351 out << static_cast<rang::fgB>(localColor);
359 if (color < Color::brightBlack)
361 const auto begin =
static_cast<unsigned>(rang::bg::black);
362 out << static_cast<rang::bg>(
static_cast<unsigned>(color) + begin);
366 const auto begin =
static_cast<unsigned>(rang::bgB::black);
367 const auto localColor =
static_cast<Color
>(begin +
368 static_cast<unsigned>(color) -
369 static_cast<unsigned>(Color::brightBlack));
370 out << static_cast<rang::bgB>(localColor);
377 const auto value =
static_cast<rang::style
>(style);
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
Sink class for directly logging onto std::cout.
Definition: ConsoleSink.hpp:239
void setTextStylePolicy(TStylePolicy &&policy)
Sets the active ConsoleTextStylePolicy.
Definition: ConsoleSink.hpp:299
~ConsoleSink() noexcept=default
Default destructor.
void removeTextStylePolicy()
Replaces the current ConsoleTextStylePolicy with the default one.
Definition: ConsoleSink.hpp:308
ConsoleSink()
Constructor.
Definition: ConsoleSink.hpp:264
std::remove_cvref_t< TRecord > Record_t
Used Record type.
Definition: ISink.hpp:34
static constexpr TextStylePolicy_t defaultTextStylePolicy()
Constructs the default TextStylePolicy.
Definition: ConsoleSink.hpp:255
std::function< ConsoleTextStyle(const Record_t &)> TextStylePolicy_t
Definition: ConsoleSink.hpp:247
Convenience class for setting up style policies for a given Record member.
Definition: ConsoleSink.hpp:155
typename Table_t::key_type Key_t
Definition: ConsoleSink.hpp:159
ConsoleTextStyle operator()(const TRecord &record) const
Invocation operator.
Definition: ConsoleSink.hpp:180
void insert(Key_t key, ConsoleTextStyle style)
Inserts a style policy.
Definition: ConsoleSink.hpp:195
ConsoleTextStyleTable(TProjection projection, TTable table)
Constructor.
Definition: ConsoleSink.hpp:166
std::remove_cvref_t< TTable > Table_t
Definition: ConsoleSink.hpp:158
std::remove_cvref_t< TProjection > Projection_t
Definition: ConsoleSink.hpp:157
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
std::remove_cvref_t< TRecord > Record_t
Used Record type.
Definition: ISink.hpp:34
constexpr ConsoleTextStyle defaultConsoleTextStyle
A constant object used for resetting the style back to default.
Definition: ConsoleSink.hpp:105
concept ConsoleTextStylePolicyFor
Concept which checks for validity of given text style policy.
Definition: ConsoleSink.hpp:97
auto makeConsoleTextStyleTableFor(TProjection projection, TTable table)
The factory function for creating ConsoleTextStyleTable instances.
Definition: ConsoleSink.hpp:224
std::ostream & operator<<(std::ostream &out, const ConsoleTextStyle &style)
Operator << overload for ConsoleTextStyle type.
Definition: ConsoleSink.hpp:135
concept Record
Concept which all the necessary concepts for Record types.
Definition: Record.hpp:204
Definition: BasicSink.hpp:22
Collection of possible style and color options for text printed onto the console.
Definition: ConsoleSink.hpp:43
Color textColor
determines the text color
Definition: ConsoleSink.hpp:85
Style
Definition: ConsoleSink.hpp:67
Style style
determines the style
Definition: ConsoleSink.hpp:80
Color bgColor
determines the background color
Definition: ConsoleSink.hpp:90
Color
Definition: ConsoleSink.hpp:45