Simple-Log  alpha-v0.7
Record.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_RECORD_HPP
7 #define SL_LOG_RECORD_HPP
8 
9 #pragma once
10 
11 #include <chrono>
12 #include <string>
13 #include <version>
14 
15 #ifdef __cpp_lib_source_location
16  #include <source_location>
17 #endif
18 
19 namespace sl::log
20 {
38  template <class TRecord>
40  {
41  using Message_t = typename TRecord::Message_t;
42  using Severity_t = typename TRecord::SeverityLevel_t;
43  using Channel_t = typename TRecord::Channel_t;
44  using TimePoint_t = typename TRecord::TimePoint_t;
45  };
46 
53  template <class TRecord>
55  requires requires { typename RecordTypedefs<TRecord>::Message_t; }
58 
65  template <class TRecord>
67  requires requires { typename RecordTypedefs<TRecord>::Severity_t; }
70 
77  template <class TRecord>
79  requires requires { typename RecordTypedefs<TRecord>::Channel_t; }
82 
89  template <class TRecord>
91  requires requires { typename RecordTypedefs<TRecord>::TimePoint_t; }
94 
99  template <class TRecord>
101  requires
102  {
103  typename RecordMessage_t<TRecord>;
104  typename RecordSeverity_t<TRecord>;
105  typename RecordChannel_t<TRecord>;
107  };
108 
116  template <class TRecord>
118  {
119  constexpr static auto message{ [](auto& record) { return record.message(); } };
120  constexpr static auto severity{ [](auto& record) { return record.severity(); } };
121  constexpr static auto channel{ [](auto& record) { return record.channel(); } };
122  constexpr static auto timePoint{ [](auto& record) { return record.timePoint(); } };
123  };
124 
129  template <class TRecord>
131  RecordMemberTypedefs<TRecord> &&
132  requires(const TRecord& rec)
133  {
134  { RecordGetters<TRecord>::message(rec) } -> std::convertible_to<RecordMessage_t<TRecord>>;
135  { RecordGetters<TRecord>::severity(rec) } -> std::convertible_to<RecordSeverity_t<TRecord>>;
136  { RecordGetters<TRecord>::channel(rec) } -> std::convertible_to<RecordChannel_t<TRecord>>;
137  { RecordGetters<TRecord>::timePoint(rec) } -> std::convertible_to<RecordTimePoint_t<TRecord>>;
138  };
139 
147  template <class TRecord>
149  {
150  using Record_t = std::remove_cvref_t<TRecord>;
151 
152  constexpr static auto setMessage
153  {
154  []<class TMessage>(Record_t& record, TMessage&& msg)
155  {
156  return record.setMessage(std::forward<TMessage>(msg));
157  }
158  };
159 
160  constexpr static auto setSeverity
161  {
162  []<class TSeverity>(Record_t& record, TSeverity&& sev)
163  {
164  return record.setSeverity(std::forward<TSeverity>(sev));
165  }
166  };
167 
168  constexpr static auto setChannel
169  {
170  []<class TChannel>(Record_t& record, TChannel&& chan)
171  {
172  return record.setChannel(std::forward<TChannel>(chan));
173  }
174  };
175 
176  constexpr static auto setTimePoint
177  {
178  []<class TTimePoint>(Record_t& record, TTimePoint&& timePoint)
179  {
180  return record.setTimePoint(std::forward<TTimePoint>(timePoint));
181  }
182  };
183  };
184 
189  template <class TRecord>
191  RecordMemberTypedefs<TRecord> &&
192  requires(TRecord& rec)
193  {
198  };
199 
203  template <class TRecord>
204  concept Record =
205  std::movable<TRecord> &&
206  RecordMemberTypedefs<TRecord> &&
207  RecordMemberGetters<TRecord> &&
208  RecordMemberSetters<TRecord>;
209 
222  template <std::semiregular TSeverityLevel,
223  std::semiregular TChannel,
224  std::semiregular TMessage = std::string,
225  std::semiregular TTimePoint = std::chrono::system_clock::time_point>
227  {
228  public:
229  using Message_t = std::remove_cvref_t<TMessage>;
230  using SeverityLevel_t = std::remove_cvref_t<TSeverityLevel>;
231  using Channel_t = std::remove_cvref_t<TChannel>;
232  using TimePoint_t = std::remove_cvref_t<TTimePoint>;
233 #ifdef __cpp_lib_source_location
234  using SourceLocation_t = std::remove_cvref_t<std::source_location>;
235 #endif
236 
241  [[nodiscard]]
242  const Message_t& message() const noexcept
243  {
244  return m_Message;
245  }
246 
252  template <std::convertible_to<Message_t> UMessage>
253  void setMessage(UMessage&& msg)
254  {
255  m_Message = std::forward<UMessage>(msg);
256  }
257 
262  [[nodiscard]]
263  const TimePoint_t& timePoint() const noexcept
264  {
265  return m_TimePoint;
266  }
267 
273  template <std::convertible_to<TimePoint_t> UTimePoint>
274  void setTimePoint(UTimePoint&& timePoint)
275  {
276  m_TimePoint = std::forward<UTimePoint>(timePoint);
277  }
278 
283  [[nodiscard]]
284  const SeverityLevel_t& severity() const noexcept
285  {
286  return m_Severity;
287  }
288 
294  template <std::convertible_to<SeverityLevel_t> USeverityLevel>
295  void setSeverity(USeverityLevel&& severity)
296  {
297  m_Severity = std::forward<USeverityLevel>(severity);
298  }
299 
304  [[nodiscard]]
305  const Channel_t& channel() const noexcept
306  {
307  return m_Channel;
308  }
309 
315  template <std::convertible_to<Channel_t> UChannel>
316  void setChannel(UChannel&& channel)
317  {
318  m_Channel = std::forward<UChannel>(channel);
319  }
320 
321 #ifdef __cpp_lib_source_location
326  [[nodiscard]]
327  const SourceLocation_t& sourceLocation() const noexcept
328  {
329  return m_SourceLocation;
330  }
331 
332 
333  template <std::convertible_to<SourceLocation_t> USourceLocation>
334  void setChannel(USourceLocation&& sourceLocation)
335  {
336  m_SourceLocation = std::forward<USourceLocation>(sourceLocation);
337  }
338 #endif
339 
340  private:
341  Message_t m_Message{};
342  TimePoint_t m_TimePoint{};
343  SeverityLevel_t m_Severity{};
344  Channel_t m_Channel{};
345 #ifdef __cpp_lib_source_location
346  SourceLocation_t m_SourceLocation{};
347 #endif
348  };
349 
351 }
352 
353 #endif
A collection of logging related information.
Definition: Record.hpp:227
void setChannel(UChannel &&channel)
Sets the Record s severity level.
Definition: Record.hpp:316
const Message_t & message() const noexcept
Const access to the message.
Definition: Record.hpp:242
void setSeverity(USeverityLevel &&severity)
Sets the Record s severity level.
Definition: Record.hpp:295
std::remove_cvref_t< TMessage > Message_t
Definition: Record.hpp:229
const TimePoint_t & timePoint() const noexcept
Const access to the time stamp.
Definition: Record.hpp:263
const Channel_t & channel() const noexcept
Const access to the channel.
Definition: Record.hpp:305
const SeverityLevel_t & severity() const noexcept
Const access to the severity level.
Definition: Record.hpp:284
void setTimePoint(UTimePoint &&timePoint)
Sets the Record s time stamp.
Definition: Record.hpp:274
std::remove_cvref_t< TTimePoint > TimePoint_t
Definition: Record.hpp:232
std::remove_cvref_t< TSeverityLevel > SeverityLevel_t
Definition: Record.hpp:230
void setMessage(UMessage &&msg)
Sets the message string.
Definition: Record.hpp:253
std::remove_cvref_t< TChannel > Channel_t
Definition: Record.hpp:231
typename RecordTypedefs< TRecord >::Message_t RecordMessage_t
Typedef for easier access to Record's message type.
Definition: Record.hpp:57
concept RecordMemberGetters
Concept which checks for the necessary member projections of a Record type.
Definition: Record.hpp:130
typename RecordTypedefs< TRecord >::Severity_t RecordSeverity_t
Typedef for easier access to Record's severity type.
Definition: Record.hpp:69
typename RecordTypedefs< TRecord >::TimePoint_t RecordTimePoint_t
Typedef for easier access to Record's time-point type.
Definition: Record.hpp:93
concept RecordMemberSetters
Concept which checks for the necessary member setters of a Record type.
Definition: Record.hpp:190
concept Record
Concept which all the necessary concepts for Record types.
Definition: Record.hpp:204
concept RecordMemberTypedefs
Concept which checks for the necessary member typedefs of a Record type.
Definition: Record.hpp:100
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 setter.
Definition: Record.hpp:118
constexpr static auto timePoint
Definition: Record.hpp:122
constexpr static auto message
Definition: Record.hpp:119
constexpr static auto channel
Definition: Record.hpp:121
constexpr static auto severity
Definition: Record.hpp:120
Provides a layer of abstraction to Record member setter.
Definition: Record.hpp:149
constexpr static auto setSeverity
Definition: Record.hpp:161
constexpr static auto setMessage
Definition: Record.hpp:153
std::remove_cvref_t< TRecord > Record_t
Definition: Record.hpp:150
constexpr static auto setChannel
Definition: Record.hpp:169
constexpr static auto setTimePoint
Definition: Record.hpp:177
Provides a layer of abstraction to Record member types.
Definition: Record.hpp:40
typename TRecord::SeverityLevel_t Severity_t
Definition: Record.hpp:42
typename TRecord::Channel_t Channel_t
Definition: Record.hpp:43
typename TRecord::TimePoint_t TimePoint_t
Definition: Record.hpp:44
typename TRecord::Message_t Message_t
Definition: Record.hpp:41