Simple-Log  alpha-v0.7
CustomRecordType/main.cpp
1 // Copyright Dominic Koepke 2021 - 2021.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // https://www.boost.org/LICENSE_1_0.txt)
5 
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!
83 
84  */
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
Class for logging into files.
Definition: FileSink.hpp:81
Helper class for building new Records.
Definition: RecordBuilder.hpp:110
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
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