Simple-Log  alpha-v0.7
Record

Modules

 Record Concepts
 

Classes

class  sl::log::BaseRecord< TSeverityLevel, TChannel, TMessage, TTimePoint >
 A collection of logging related information. More...
 
class  sl::log::SetSev< TSeverityLevel >
 Manipulates the channel of the current RecordBuilder object. More...
 
class  sl::log::SetChan< TChannel >
 Manipulates the channel of the current RecordBuilder object. More...
 
class  sl::log::RecordBuilder< TRecord >
 Helper class for building new Records. More...
 

Detailed Description

Records are basically a collection of information, which has been gathered either implicitly or explicitly while a logging Message was created. It is not intended that users ever use a plain Record or set it up by theirselves. The library provides a simple interface which does all the trivial part of a logging step, while users can concentrate on setting up their message.
Have a look at Logger and RecordBuilder if you want to know more about how to setup Records.

Custom Record Types

In the preset namespace of this library users can find a Record_t alias, which will accept values of the SevLvl enum as SeverityLevel and std::strings as Channel type. Users may exchange any or all of these predefined types with their own. If you use any Filter, FlushPolicy or anything else which watches out for a specific Channel, it might be wise to exchange the std::string type with something less expensive to compare, e.g. an enum type or plain int. For example users could use the BaseRecord type, but exchanging some member types with any other type or even create their custom Record type from scratch. Here is an example for the former:

8 
9 #include <sstream>
10 
11 using namespace sl::log;
12 using preset::SevLvl;
13 
14 enum class Channel
15 {
16  standard,
17  network,
18  stats
19 };
20 
21 // A simple overload, thus our Channel enum can be printed into every ostream object.
22 inline std::ostream& operator <<(std::ostream& out, Channel lvl)
23 {
24  constexpr const char* str[] = { "standard", "network", "stats" };
25  out << str[static_cast<std::size_t>(lvl)];
26  return out;
27 }
28 
29 // Yes, these are a few typedefs which might seem tedious at the first glance, but you'll usually need to do this once per program.
31 using Core_t = Core<Record_t>;
35 
36 inline Core_t gCore;
37 // every Record, which will be created by this Logger will be from Channel "standard" by default
38 inline auto gLog = makeLogger<Logger_t>(gCore, SevLvl::info, Channel::standard);
39 inline auto& gConsoleSink
40 {
41  []() -> auto&
42  {
43  // let's create the console sink in disabled state. Will become automatically enabled after this scope is left.
44  auto wrappedSink = gCore.makeDisabledSink<ConsoleSink_t>();
45  // Only messages with the Channel network shall be printed onto the console
46  wrappedSink->setFilter(makeChannelFilterFor<Record_t>(Equals{ Channel::network }));
47  // setting up a custom formatter, thus for each Record only the channel followed by the message will be printed.
48  wrappedSink->setFormatter(
49  [](const Record_t& record)
50  {
51  std::stringstream out;
52  out << record.channel() << " >> " << record.message();
53  return std::move(out).str();
54  }
55  );
56 
57  return *wrappedSink;
58  }()
59 };
60 
61 int main()
62 {
63  gLog() << "Hello, World!"; // this message is ignored by our Console sink
64  gLog() << SetChan(Channel::network) << "Hello, Network!"; // our Console sink handles this record.
65 }
66 
67 /* The output of the console looks like this:
68  network >> Hello, Network!
Convenience class for generating Record s.
Definition: Logger.hpp:84
A collection of logging related information.
Definition: Record.hpp:227
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... > ScopedSinkDisabling< Record_t, TSink > makeDisabledSink(TArgs &&... args)
Creates Sink disabled and registers it at this Core instance.
Definition: Core.hpp:149
Compares equality with constant at invocation.
Definition: Predicates.hpp:25
Class for logging into files.
Definition: FileSink.hpp:81
Manipulates the channel of the current RecordBuilder object.
Definition: RecordBuilder.hpp:70
std::ostream & operator<<(std::ostream &out, const ConsoleTextStyle &style)
Operator << overload for ConsoleTextStyle type.
Definition: ConsoleSink.hpp:135
ConsoleSink< Record_t > ConsoleSink_t
Type alias for log::ConsoleSink which uses preset::Record_t as Record type.
Definition: PresetTypes.hpp:80
SevLvl
A simple severity level enum type.
Definition: PresetTypes.hpp:26
Core< Record_t > Core_t
Type alias for log::Core which uses preset::Record_t as Record type.
Definition: PresetTypes.hpp:56
BaseLogger< Record_t > Logger_t
Type alias for log::Logger which uses preset::Record_t as Record type.
Definition: PresetTypes.hpp:60
BaseRecord< SevLvl, std::string > Record_t
Prepared Record type.
Definition: PresetTypes.hpp:52
FileSink< Record_t > FileSink_t
Type alias for log::FileSink which uses preset::Record_t as Record type.
Definition: PresetTypes.hpp:76
auto gLog
Definition: ReadyToGo.hpp:20
Core_t gCore
Definition: ReadyToGo.hpp:18
auto & gConsoleSink
Definition: ReadyToGo.hpp:19
Definition: BasicSink.hpp:22

If your needs are more specific and you want to add things such as custom properties to the Record type, this might be an example for you:

8 
9 #include <sstream>
10 
11 using namespace sl::log;
12 using preset::SevLvl;
13 
14 class MyCustomRecord :
15  public preset::Record_t
16 {
17 public:
18  using Domain_t = std::string;
19 
20  Domain_t domain;
21 };
22 
23 // Yes, these are a few typedefs which might seem tedious at the first glance, but you'll usually need to do this once per program.
28 
29 // Now that our Record class contains an additional domain property, we would like to have a manipulator for our RecordBuilder
30 using RecordBuilder_t = RecordBuilder<MyCustomRecord>;
31 
32 class SetDomain
33 {
34 public:
35  using Domain_t = MyCustomRecord::Domain_t;
36 
37  explicit SetDomain(Domain_t data) :
38  m_Data{ std::move(data) }
39  {
40  }
41 
42  void operator ()(MyCustomRecord& rec)
43  {
44  rec.domain = std::move(m_Data);
45  }
46 
47 private:
48  Domain_t m_Data;
49 };
50 
51 inline Core_t gCore;
52 inline auto gLog = makeLogger<Logger_t>(gCore, SevLvl::info);
53 inline auto& gConsoleSink
54 {
55  []() -> auto&
56  {
57  // let's create the console sink in disabled state. Will become automatically enabled after this scope is left.
58  auto wrappedSink = gCore.makeDisabledSink<ConsoleSink_t>();
59  // setting up a custom formatter, thus for each Record only the domain followed by the message will be printed.
60  wrappedSink->setFormatter(
61  [](const MyCustomRecord& record)
62  {
63  std::stringstream out;
64  out << record.domain << " >> " << record.message();
65  return std::move(out).str();
66  }
67  );
68 
69  return *wrappedSink;
70  }()
71 };
72 
73 int main()
74 {
75  gLog() << "Hello, World!";
76  // this clearly belongs to the domain of palindromes, thus let's tag it like this!
77  gLog() << SetDomain("palindrome") << "A Man, A Plan, A Canal, Panama!";
78 }
79 
80 /* The output of the console looks like this:
81  >> Hello, World!
82 palindrome >> A Man, A Plan, A Canal, Panama!
Helper class for building new Records.
Definition: RecordBuilder.hpp:110