Simple-Log  alpha-v0.7
CustomizeBaseRecord/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 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!
69 
70  */
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