Simple-Log  alpha-v0.7
RecordBuilder.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_BUILDER_HPP
7 #define SL_LOG_RECORD_BUILDER_HPP
8 
9 #pragma once
10 
11 #include "Record.hpp"
12 
13 #include <functional>
14 #include <sstream>
15 #include <type_traits>
16 
17 namespace sl::log
18 {
29  template <class TSeverityLevel>
30  class SetSev
31  {
32  public:
33  using Sev_t = TSeverityLevel;
34 
39  explicit constexpr SetSev(
40  TSeverityLevel data
41  )
42  noexcept(std::is_nothrow_move_constructible_v<Sev_t>) :
43  m_Data{ std::move(data) }
44  {
45  }
46 
52  template <Record TRecord>
53  void operator ()(TRecord& rec) const
54  {
55  rec.setSeverity(m_Data);
56  }
57 
58  private:
59  Sev_t m_Data;
60  };
61 
68  template <class TChannel>
69  class SetChan
70  {
71  public:
72  using Chan_t = TChannel;
73 
78  explicit constexpr SetChan(
79  TChannel data
80  ) noexcept(
81  std::is_nothrow_move_constructible_v<Chan_t>) :
82  m_Data{ std::move(data) }
83  {
84  }
85 
91  template <Record TRecord>
92  void operator ()(TRecord& rec) const
93  {
94  rec.setChannel(m_Data);
95  }
96 
97  private:
98  Chan_t m_Data;
99  };
100 
108  template <Record TRecord>
110  {
111  public:
112  using Record_t = std::remove_cvref_t<TRecord>;
115 
116  private:
117  using LogCallback_t = std::function<void(Record_t)>;
118 
119  public:
129  explicit RecordBuilder(Record_t prefabRec, LogCallback_t cb) noexcept :
130  m_Record{ std::move(prefabRec) },
131  m_LogCallback{ std::move(cb) }
132  {
133  }
134 
139  ~RecordBuilder() noexcept
140  {
141  if (m_LogCallback)
142  {
143  try
144  {
145  m_Record.setMessage(std::move(m_Stream).str());
146  m_LogCallback(std::move(m_Record));
147  }
148  catch (...)
149  {
150  }
151  }
152  }
153 
159  RecordBuilder(RecordBuilder&& other) noexcept
160  {
161  *this = std::move(other);
162  }
163 
171  {
172  using std::swap;
173  swap(m_Record, other.m_Record);
174  swap(m_Stream, other.m_Stream);
175  m_LogCallback = std::exchange(other.m_LogCallback, {});
176  return *this;
177  }
178 
182  RecordBuilder(const RecordBuilder&) = delete;
187 
191  [[nodiscard]]
192  Record_t& record() noexcept
193  {
194  return m_Record;
195  }
196 
200  [[nodiscard]]
201  const Record_t& record() const noexcept
202  {
203  return m_Record;
204  }
205 
213  template <class T>
214  requires requires(T&& data)
215  {
216  { std::declval<std::ostringstream>() << std::forward<T>(data) };
217  }
219  {
220  m_Stream << std::forward<T>(data);
221  return *this;
222  }
223 
229  template <std::invocable<Record_t&> TAction>
230  RecordBuilder& operator <<(TAction action)
231  {
232  std::invoke(action, m_Record);
233  return *this;
234  }
235 
236  private:
237  Record_t m_Record;
238  std::ostringstream m_Stream;
239  LogCallback_t m_LogCallback;
240  };
241 
243 }
244 
245 #endif
Helper class for building new Records.
Definition: RecordBuilder.hpp:110
RecordBuilder(RecordBuilder &&other) noexcept
Move constructor.
Definition: RecordBuilder.hpp:159
const Record_t & record() const noexcept
Const accessor to the internal record object.
Definition: RecordBuilder.hpp:201
RecordChannel_t< Record_t > Channel_t
Definition: RecordBuilder.hpp:114
Record_t & record() noexcept
Accessor to the internal record object.
Definition: RecordBuilder.hpp:192
requires requires(T &&data)
Output operator.
Definition: RecordBuilder.hpp:214
~RecordBuilder() noexcept
Destructor.
Definition: RecordBuilder.hpp:139
RecordBuilder & operator=(RecordBuilder &&other) noexcept
Move-assign operator.
Definition: RecordBuilder.hpp:170
std::remove_cvref_t< TRecord > Record_t
Definition: RecordBuilder.hpp:112
RecordBuilder & operator<<(T &&data)
Definition: RecordBuilder.hpp:218
RecordSeverity_t< Record_t > SeverityLevel_t
Definition: RecordBuilder.hpp:113
RecordBuilder(const RecordBuilder &)=delete
Deleted copy constructor.
RecordBuilder(Record_t prefabRec, LogCallback_t cb) noexcept
Constructor.
Definition: RecordBuilder.hpp:129
Manipulates the channel of the current RecordBuilder object.
Definition: RecordBuilder.hpp:70
void operator()(TRecord &rec) const
Changes channel of the passed Record object.
Definition: RecordBuilder.hpp:92
TChannel Chan_t
Definition: RecordBuilder.hpp:72
constexpr SetChan(TChannel data) noexcept(std::is_nothrow_move_constructible_v< Chan_t >)
Constructor accepting channel data.
Definition: RecordBuilder.hpp:78
Manipulates the channel of the current RecordBuilder object.
Definition: RecordBuilder.hpp:31
constexpr SetSev(TSeverityLevel data) noexcept(std::is_nothrow_move_constructible_v< Sev_t >)
Constructor accepting severity level data.
Definition: RecordBuilder.hpp:39
TSeverityLevel Sev_t
Definition: RecordBuilder.hpp:33
void operator()(TRecord &rec) const
Changes severity level of the passed Record object.
Definition: RecordBuilder.hpp:53
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