Simple-Log  alpha-v0.7
Logger.hpp
Go to the documentation of this file.
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 
6 #ifndef SL_LOG_LOGGER_HPP
7 #define SL_LOG_LOGGER_HPP
8 
9 #pragma once
10 
11 #include "Core.hpp"
12 #include "Record.hpp"
13 #include "RecordBuilder.hpp"
14 
15 #include <cassert>
16 #include <functional>
17 #include <version>
18 
19 #ifdef __cpp_lib_source_location
20 #include <source_location>
21 #endif
22 
23 namespace sl::log
24 {
40  template <class TLogger>
42  {
43  using Record_t = typename TLogger::Record_t;
44  };
45 
52  template <class TLogger>
54  requires requires
55  {
57  }
60 
64  template <class T>
65  concept Logger =
66  requires
67  {
68  typename LoggerRecord_t<T>;
69  } &&
70  Record<LoggerRecord_t<T>> &&
71  std::is_invocable_r_v<RecordBuilder<LoggerRecord_t<T>>, T>;
72 
82  template <Record TRecord>
83  class BaseLogger
84  {
85  public:
86  using Record_t = std::remove_cvref_t<TRecord>;
91  using LogCallback_t = std::function<void(Record_t)>;
92 
100  explicit BaseLogger(
101  LogCallback_t logCallback,
102  SeverityLevel_t defaultSeverityLvl = {},
104  ) noexcept :
105  m_LogCallback{ std::move(logCallback) },
106  m_DefaultSeverityLvl{ std::move(defaultSeverityLvl) },
107  m_DefaultChannel{ std::move(defaultChannel) }
108  {
109  }
110 
114  ~BaseLogger() noexcept = default;
115 
119  BaseLogger(const BaseLogger&) noexcept = default;
123  BaseLogger& operator =(const BaseLogger&) noexcept = default;
124 
128  BaseLogger(BaseLogger&&) noexcept = default;
132  BaseLogger& operator =(BaseLogger&&) noexcept = default;
133 
140  [[nodiscard]]
141 #ifdef __cpp_lib_source_location
142  RecordBuilder_t operator ()(const std::source_location& srcLoc = std::source_location::current())
143 #else
145 #endif
146  {
147  assert(m_LogCallback != nullptr && "Log callback must be set.");
148  Record_t prefabRec;
149  RecordSetters_t::setTimePoint(prefabRec, std::chrono::system_clock::now());
150  RecordSetters_t::setSeverity(prefabRec, m_DefaultSeverityLvl);
151  RecordSetters_t::setChannel(prefabRec, m_DefaultChannel);
152  RecordBuilder_t builder{ std::move(prefabRec), m_LogCallback };
153 
154 #ifdef __cpp_lib_source_location
155  builder.record().sourceLocation = srcLoc;
156 #endif
157 
158  return builder;
159  }
160 
165  template <std::convertible_to<SeverityLevel_t> USeverityLevel>
166  void setDefaultSeverity(USeverityLevel&& sevLvl) noexcept
167  {
168  m_DefaultSeverityLvl = std::forward<USeverityLevel>(sevLvl);
169  }
170 
175  [[nodiscard]]
176  const SeverityLevel_t& defaultSeverity() const noexcept
177  {
178  return m_DefaultSeverityLvl;
179  }
180 
185  template <std::convertible_to<Channel_t> UChannel>
186  void setDefaultChannel(UChannel&& channel) noexcept
187  {
188  m_DefaultChannel = std::forward<UChannel>(channel);
189  }
190 
195  [[nodiscard]]
196  const Channel_t& defaultChannel() const noexcept
197  {
198  return m_DefaultChannel;
199  }
200 
201  private:
202  LogCallback_t m_LogCallback;
203  SeverityLevel_t m_DefaultSeverityLvl;
204  Channel_t m_DefaultChannel;
205  };
206 
218  template <Logger TLogger, class... TArgs>
219  TLogger makeLogger(Core<LoggerRecord_t<TLogger>>& core, TArgs&&... args)
220  {
221  return TLogger{
222  [&core](LoggerRecord_t<TLogger>&& rec)
223  {
224  core.log(std::move(rec));
225  },
226  std::forward<TArgs>(args)...
227  };
228  }
229 
231 }
232 
233 #endif
Convenience class for generating Record s.
Definition: Logger.hpp:84
RecordChannel_t< Record_t > Channel_t
Definition: Logger.hpp:89
RecordBuilder_t operator()()
Creates a new instance of RecordBuilder.
Definition: Logger.hpp:144
void setDefaultSeverity(USeverityLevel &&sevLvl) noexcept
Setter for the default severity level.
Definition: Logger.hpp:166
std::function< void(Record_t)> LogCallback_t
Definition: Logger.hpp:91
const Channel_t & defaultChannel() const noexcept
Getter for the default channel.
Definition: Logger.hpp:196
const SeverityLevel_t & defaultSeverity() const noexcept
Getter for the default severity level.
Definition: Logger.hpp:176
RecordSeverity_t< Record_t > SeverityLevel_t
Definition: Logger.hpp:88
BaseLogger(LogCallback_t logCallback, SeverityLevel_t defaultSeverityLvl={}, Channel_t defaultChannel={}) noexcept
Constructor.
Definition: Logger.hpp:100
void setDefaultChannel(UChannel &&channel) noexcept
Setter for the default channel.
Definition: Logger.hpp:186
~BaseLogger() noexcept=default
Destructor.
std::remove_cvref_t< TRecord > Record_t
Definition: Logger.hpp:86
The central point of the whole library. Needs to be instantiated at least once.
Definition: Core.hpp:51
Helper class for building new Records.
Definition: RecordBuilder.hpp:110
typename LoggerTypedefs< TLogger >::Record_t LoggerRecord_t
Typedef for easier access to Logger's Record type.
Definition: Logger.hpp:59
concept Logger
Concept for Logger classes.
Definition: Logger.hpp:65
TLogger makeLogger(Core< LoggerRecord_t< TLogger >> &core, TArgs &&... args)
Creates a Logger object and setup its callback to the given Core instance.
Definition: Logger.hpp:219
BaseRecord< SevLvl, std::string > Record_t
Prepared Record type.
Definition: PresetTypes.hpp:52
typename RecordTypedefs< TRecord >::Severity_t RecordSeverity_t
Typedef for easier access to Record's severity type.
Definition: Record.hpp:69
typename RecordTypedefs< TRecord >::Channel_t RecordChannel_t
Typedef for easier access to Record's channel type.
Definition: Record.hpp:81
Definition: BasicSink.hpp:22
Provides a layer of abstraction to Record member types.
Definition: Logger.hpp:42
typename TLogger::Record_t Record_t
Definition: Logger.hpp:43
Provides a layer of abstraction to Record member setter.
Definition: Record.hpp:149
constexpr static auto setSeverity
Definition: Record.hpp:161
constexpr static auto setChannel
Definition: Record.hpp:169
constexpr static auto setTimePoint
Definition: Record.hpp:177