Simple-Log  alpha-v0.7
ConsoleSink

Classes

struct  sl::log::ConsoleTextStyle
 Collection of possible style and color options for text printed onto the console. More...
 
class  sl::log::ConsoleTextStyleTable< TProjection, TTable >
 Convenience class for setting up style policies for a given Record member. More...
 
class  sl::log::ConsoleSink< TRecord >
 Sink class for directly logging onto std::cout. More...
 

Functions

std::ostream & sl::log::operator<< (std::ostream &out, const ConsoleTextStyle &style)
 Operator << overload for ConsoleTextStyle type. More...
 
template<Record TRecord, std::invocable< const TRecord & > TProjection, class TTable = std::unordered_map< std::remove_cvref_t<std::invoke_result_t<TProjection, const TRecord&>>, ConsoleTextStyle >>
auto sl::log::makeConsoleTextStyleTableFor (TProjection projection, TTable table)
 The factory function for creating ConsoleTextStyleTable instances. More...
 

Variables

template<class T , class TRecord >
concept sl::log::ConsoleTextStylePolicyFor
 Concept which checks for validity of given text style policy. More...
 
constexpr ConsoleTextStyle sl::log::defaultConsoleTextStyle
 A constant object used for resetting the style back to default. More...
 

Detailed Description

ConsoleSink ConsoleSinks are tightly linked to the std::cout object.

Colorize Messages

The library offers a quick way setting up your intended color (and style) table, based on any Record property. Here is a quick example which illustrates the setup:

7 
8 // just pull everything into the current namespace
9 using namespace sl::log::preset;
10 using namespace sl::log;
11 
12 int main()
13 {
14  Core_t core;
15  auto log = makeLogger<Logger_t>(core, SevLvl::info);
16 
17  auto& gConsoleSink = core.makeSink<ConsoleSink_t>();
18 
19  using Color = ConsoleTextStyle::Color;
20  using Style = ConsoleTextStyle::Style;
21  gConsoleSink.setTextStylePolicy(
22  makeConsoleTextStyleTableFor<Record_t>(
23  &Record_t::severity, // use the severity property
24  {
25  // the desired SevLvl gets linked to a color and style setup
26  { SevLvl::debug, { .textColor = Color::yellow, .bgColor = Color::gray } },
27  { SevLvl::hint, { .bgColor = Color::green } },
28  { SevLvl::warning, { .bgColor = Color::yellow } },
29  { SevLvl::error, { .style = Style::bold, .textColor = Color::red, .bgColor = Color::yellow } },
30  { SevLvl::fatal, { .style = Style::crossed, .textColor = Color::gray, .bgColor = Color::red } }
31  }
32  )
33  );
34 
35  log() << "Hello, World!";
36  log() << SetSev(SevLvl::debug) << "Mighty debug message";
37  log() << "Print as hint!" << SetSev(SevLvl::hint);
38  log() << SetSev(SevLvl::warning) << "be warned...";
39  log() << "Print my important error!" << SetSev(SevLvl::error);
40  log() << "Print it fatal!" << SetSev(SevLvl::fatal);
41 }
42 // Above lines will be printed nicely colorful onto the console. Go, test it out ;)
const SeverityLevel_t & severity() const noexcept
Const access to the severity level.
Definition: Record.hpp:284
Sink class for directly logging onto std::cout.
Definition: ConsoleSink.hpp:239
The central point of the whole library. Needs to be instantiated at least once.
Definition: Core.hpp:51
requires std::constructible_from< TSink, TArgs... > TSink & makeSink(TArgs &&... args)
Creates Sink and registers it at this Core instance.
Definition: Core.hpp:130
Manipulates the channel of the current RecordBuilder object.
Definition: RecordBuilder.hpp:31
Definition: PresetTypes.hpp:14
auto & gConsoleSink
Definition: ReadyToGo.hpp:19
Definition: BasicSink.hpp:22
Style
Definition: ConsoleSink.hpp:67
Color
Definition: ConsoleSink.hpp:45

Function Documentation

◆ makeConsoleTextStyleTableFor()

template<Record TRecord, std::invocable< const TRecord & > TProjection, class TTable = std::unordered_map< std::remove_cvref_t<std::invoke_result_t<TProjection, const TRecord&>>, ConsoleTextStyle >>
auto sl::log::makeConsoleTextStyleTableFor ( TProjection  projection,
TTable  table 
)

The factory function for creating ConsoleTextStyleTable instances.

Template Parameters
TRecordThe currently used Record type
TProjectionThe projection type
TTableThe table type. The default type is of std::unordered_map. Users may exchange this with std::map.
Parameters
projectionThe projection to a member of the currently used Record type
tableThe finally established container object
Returns
Returns a newly created ConsoleTextStyleTable instance

This is the preferable way creating a ConsoleTextStyleTable object for a Record property, because the projection and the table type becomes strong checked via concept and therefore will provide much clearer feedback in cases of error, while creating ConsoleTextStyleTable objects manually will probably result in harder to read error message.

Version
since alpha-0.6

◆ operator<<()

std::ostream& sl::log::operator<< ( std::ostream &  out,
const ConsoleTextStyle style 
)
inline

Operator << overload for ConsoleTextStyle type.

Parameters
outthe stream object
stylethe provided style
Returns
Returns the parameter out as reference
Version
since alpha-0.6
Examples
CustomizeBaseRecord/main.cpp.

Variable Documentation

◆ ConsoleTextStylePolicyFor

template<class T , class TRecord >
concept sl::log::ConsoleTextStylePolicyFor
Initial value:
=
Record<TRecord> &&
std::is_invocable_r_v<ConsoleTextStyle, T, const TRecord&>

Concept which checks for validity of given text style policy.

◆ defaultConsoleTextStyle

constexpr ConsoleTextStyle sl::log::defaultConsoleTextStyle
constexpr

A constant object used for resetting the style back to default.

Version
since alpha-0.6