CppTrail
Loading...
Searching...
No Matches
message.h
Go to the documentation of this file.
1
8#ifndef CPPTRAIL_MESSAGE_H
9#define CPPTRAIL_MESSAGE_H
10
11#include <memory>
12#include <string>
13#include <exception>
14
15#if __cplusplus >= 201703L
16#include <string_view>
17#endif
18
19#include "cpptrail/def.h"
20
21namespace CppTrail {
26 template<typename char_t>
28 public:
30 using char_type = char_t;
31
33 using string_type = std::basic_string<char_type>;
34
35 public:
36 virtual ~BasicMessageImpl() = default;
37
38 public:
44 virtual string_type stealString() = 0;
45 };
46
54
57
58#if __cplusplus >= 202002L
61#endif
62
65
68
75 template<typename char_t>
77 public:
80
82 using char_type = char_t;
83
85 using string_type = std::basic_string<char_type>;
86
87 protected:
93 explicit BasicMessage(
94 string_type sLevel,
95 std::shared_ptr<impl_type> pImpl
96 ) : m_sLevel(std::move(sLevel)),
97 m_pImpl(std::move(pImpl)) {
98 }
99
100 public:
104 BasicMessage() = default;
105
112 if (!this->m_pImpl) return string_type{};
113 return this->m_pImpl->stealString();
114 }
115
122 return std::move(this->m_sLevel);
123 }
124
129 std::weak_ptr<impl_type> getImplementation() noexcept {
130 return this->m_pImpl;
131 }
132
133 private:
135 string_type m_sLevel;
137 std::shared_ptr<impl_type> m_pImpl;
138 };
139
147
150
151#if __cplusplus >= 202002L
154#endif
155
158
161
168 template<typename char_t>
169 class BasicStringMessage : public BasicMessage<char_t> {
170 public:
173
175 using char_type = char_t;
176
178 using string_type = std::basic_string<char_type>;
179
180#if __cplusplus >= 201703L
182 using string_view_type = std::basic_string_view<char_type>;
183#endif
184
185 public:
192 string_type sLevel,
193 string_type sMessage
194 ) : BasicMessage<char_t>(
195 std::move(sLevel),
196 std::make_shared<Impl>(std::move(sMessage))
197 ) {
198 }
199
200#if __cplusplus >= 201703L
207 string_type sLevel,
208 string_view_type sMessage
209 ) : BasicMessage<char_t>(
210 std::move(sLevel),
211 std::make_shared<Impl>(string_type{sMessage})
212 ) {
213 }
214#endif
215
223 template<typename T = char_t,
224 typename std::enable_if<std::is_same<T, char>::value, int>::type = 0>
226 string_type sLevel,
227 const std::exception &oMessage
228 ) : BasicMessage<char_t>(
229 std::move(sLevel),
230 std::make_shared<Impl>(oMessage.what())
231 ) {
232 }
233
234 private:
238 class Impl : public BasicMessageImpl<char_t> {
239 public:
244 explicit Impl(string_type sString)
245 : m_sString(std::move(sString)) {
246 }
247
248 public:
253 string_type stealString() override {
254 return std::move(this->m_sString);
255 }
256
257 private:
259 string_type m_sString;
260 };
261 };
262
270
273
274#if __cplusplus >= 202002L
277#endif
278
281
284
286}
287
288#endif
Abstract base class for message implementations.
Definition message.h:27
std::basic_string< char_type > string_type
Alias for the basic_string type associated with this logger's encoding.
Definition message.h:33
char_t char_type
Alias for the underlying character type.
Definition message.h:30
virtual string_type stealString()=0
Returns the string message to log.
Container for log message implementations.
Definition message.h:76
char_t char_type
Alias for the underlying character type.
Definition message.h:82
BasicMessage()=default
Default constructor.
std::weak_ptr< impl_type > getImplementation() noexcept
Retrieves a weak pointer to the internal implementation.
Definition message.h:129
string_type stealLevel() noexcept
Extracts the string representation of the log level.
Definition message.h:121
BasicMessage(string_type sLevel, std::shared_ptr< impl_type > pImpl)
Internal constructor for derived message types.
Definition message.h:93
string_type stealString()
Returns the string message to log.
Definition message.h:111
std::basic_string< char_type > string_type
Alias for the basic_string type associated with this logger's encoding.
Definition message.h:85
A standard string-based log message.
Definition message.h:169
BasicStringMessage(string_type sLevel, string_type sMessage)
Constructs a message from a string and a level.
Definition message.h:191
char_t char_type
Alias for the underlying character type.
Definition message.h:175
BasicStringMessage(string_type sLevel, string_view_type sMessage)
Constructs a message from a string view and a level.
Definition message.h:206
std::basic_string< char_type > string_type
Alias for the basic_string type associated with this logger's encoding.
Definition message.h:178
BasicStringMessage(string_type sLevel, const std::exception &oMessage)
Constructs a message from a standard exception and a level.
Definition message.h:225
std::basic_string_view< char_type > string_view_type
Alias for the basic_string_view type associated with this logger's encoding.
Definition message.h:182
Global definitions, enumerations, and string mapping utilities for CppTrail.
Root namespace for the CppTrail logging library.
Definition async_logger.h:24