CppTrail
Loading...
Searching...
No Matches
base_logger.h
Go to the documentation of this file.
1
11#ifndef CPPTRAIL_BASE_LOGGER_H
12#define CPPTRAIL_BASE_LOGGER_H
13
14#include "cpptrail/message.h"
15
16namespace CppTrail {
27 template<typename char_t>
29 public:
31 using char_type = char_t;
32
34 using string_type = std::basic_string<char_type>;
35
38
39#if __cplusplus >= 201703L
41 using string_view_type = std::basic_string_view<char_type>;
42#endif
43
44 public:
46 virtual ~BasicLoggerImpl() = default;
47
52 virtual void log(message_type oMessage) = 0;
53
58 virtual void start() = 0;
59
65 virtual void stop() = 0;
66
72 virtual void signalStop() = 0;
73
78 virtual void join() = 0;
79
84 virtual Status status() = 0;
85 };
86
93
96
97#if __cplusplus >= 202002L
100#endif
101
104
107
120 template<typename char_t>
122 public:
124 BasicLogger(nullptr_t) = delete;
125
126 protected:
131 BasicLogger(std::shared_ptr<BasicLoggerImpl<char_t> > pLogger)
132 : m_pLogger(std::move(pLogger)) {
133 }
134
146 if (this->m_pLogger && this->m_pLogger.use_count() == 1) {
147 this->m_pLogger->stop();
148 this->m_pLogger->join();
149 }
150 }
151
152 public:
154 BasicLogger(const BasicLogger &pLogger) = default;
155
157 BasicLogger(BasicLogger &&pLogger) = default;
158
160 BasicLogger &operator=(const BasicLogger &pLogger) = default;
161
163 BasicLogger &operator=(BasicLogger &&pLogger) = default;
164
165 public:
168
171
174
175#if __cplusplus >= 201703L
178#endif
179
180 public:
186 void start() {
187 if (this->m_pLogger) this->m_pLogger->start();
188 }
189
195 void stop() {
196 if (this->m_pLogger) this->m_pLogger->stop();
197 }
198
205 void signalStop() {
206 if (this->m_pLogger) this->m_pLogger->signalStop();
207 }
208
215 void join() {
216 if (this->m_pLogger) this->m_pLogger->join();
217 }
218
223 [[nodiscard]] Status status() {
224 if (this->m_pLogger) return this->m_pLogger->status();
225 }
226
230 public:
237 if (this->m_pLogger) this->m_pLogger->log(std::move(oMessage));
238 return *this;
239 }
240
247 BasicLogger &log(const Level nLevel, string_type sMessage) {
248 return this->log(BasicStringMessage<char_t>(
249 tgetName<char_t>(nLevel), std::move(sMessage)
250 ));
251 }
252
260 return this->log(BasicStringMessage<char_t>(
261 std::move(sLevel), std::move(sMessage)
262 ));
263 }
264
267 public:
276 BasicLogger &success(string_type sMessage) { return this->log(Level::SUCCESS, std::move(sMessage)); }
277
283 BasicLogger &trace(string_type sMessage) { return this->log(Level::TRACE, std::move(sMessage)); }
284
290 BasicLogger &debug(string_type sMessage) { return this->log(Level::DEBUG, std::move(sMessage)); }
291
297 BasicLogger &info(string_type sMessage) { return this->log(Level::INFO, std::move(sMessage)); }
298
304 BasicLogger &message(string_type sMessage) { return this->log(Level::MESSAGE, std::move(sMessage)); }
305
311 BasicLogger &warning(string_type sMessage) { return this->log(Level::WARNING, std::move(sMessage)); }
312
318 BasicLogger &error(string_type sMessage) { return this->log(Level::ERROR, std::move(sMessage)); }
319
325 BasicLogger &critical(string_type sMessage) { return this->log(Level::CRITICAL, std::move(sMessage)); }
326
332 BasicLogger &fatal(string_type sMessage) { return this->log(Level::FATAL, std::move(sMessage)); }
335#if __cplusplus >= 201703L
339 public:
346 BasicLogger &log(const Level nLevel, string_view_type sMessage) {
347 return this->log(BasicStringMessage<char_t>(tgetName<char_t>(nLevel), sMessage));
348 }
349
357 return this->log(BasicStringMessage<char_t>(std::move(sLevel), sMessage));
358 }
359
360 public:
364 BasicLogger &success(string_view_type sMsg) { return this->log(Level::SUCCESS, sMsg); }
366 BasicLogger &trace(string_view_type sMsg) { return this->log(Level::TRACE, sMsg); }
368 BasicLogger &debug(string_view_type sMsg) { return this->log(Level::DEBUG, sMsg); }
370 BasicLogger &info(string_view_type sMsg) { return this->log(Level::INFO, sMsg); }
372 BasicLogger &message(string_view_type sMsg) { return this->log(Level::MESSAGE, sMsg); }
374 BasicLogger &warning(string_view_type sMsg) { return this->log(Level::WARNING, sMsg); }
376 BasicLogger &error(string_view_type sMsg) { return this->log(Level::ERROR, sMsg); }
380 BasicLogger &fatal(string_view_type sMsg) { return this->log(Level::FATAL, sMsg); }
388 public:
395 BasicLogger &log(const Level nLevel, const char_t *sMessage) {
396 return this->log(BasicStringMessage<char_t>(tgetName<char_t>(nLevel), string_type{sMessage}));
397 }
398
405 BasicLogger &log(string_type sLevel, const char_t *sMessage) {
406 return this->log(BasicStringMessage<char_t>(std::move(sLevel), string_type{sMessage}));
407 }
408
409 public:
413 BasicLogger &success(const char_t *sMsg) { return this->log(Level::SUCCESS, sMsg); }
415 BasicLogger &trace(const char_t *sMsg) { return this->log(Level::TRACE, sMsg); }
417 BasicLogger &debug(const char_t *sMsg) { return this->log(Level::DEBUG, sMsg); }
419 BasicLogger &info(const char_t *sMsg) { return this->log(Level::INFO, sMsg); }
421 BasicLogger &message(const char_t *sMsg) { return this->log(Level::MESSAGE, sMsg); }
423 BasicLogger &warning(const char_t *sMsg) { return this->log(Level::WARNING, sMsg); }
425 BasicLogger &error(const char_t *sMsg) { return this->log(Level::ERROR, sMsg); }
427 BasicLogger &critical(const char_t *sMsg) { return this->log(Level::CRITICAL, sMsg); }
429 BasicLogger &fatal(const char_t *sMsg) { return this->log(Level::FATAL, sMsg); }
435#endif
436
437 public:
448 template<typename T = char_t, typename std::enable_if<std::is_same<T, char>::value, int>::type = 0>
449 BasicLogger &log(const std::exception &oException) {
450 return this->log(BasicStringMessage<char_t>(tgetName<char_t>(Level::ERROR), oException));
451 }
452
461 template<typename T = char_t, typename std::enable_if<std::is_same<T, char>::value, int>::type = 0>
462 BasicLogger &log(const Level nLevel, const std::exception &oException) {
463 return this->log(BasicStringMessage<char_t>(tgetName<char_t>(nLevel), oException));
464 }
465
474 template<typename T = char_t, typename std::enable_if<std::is_same<T, char>::value, int>::type = 0>
475 BasicLogger &log(string_type sLevel, const std::exception &oException) {
476 return this->log(BasicStringMessage<char_t>(std::move(sLevel), oException));
477 }
478
481 private:
483 std::shared_ptr<BasicLoggerImpl<char_t> > m_pLogger;
484 };
485
493
496
497#if __cplusplus >= 202002L
500#endif
501
504
507
509}
510
511#endif
Abstract template interface defining the mandatory contract for all loggers.
Definition base_logger.h:28
virtual void join()=0
Waits for the asynchronous end of the shutdown sequence.
virtual void start()=0
Synchronously starts the logger service.
virtual void signalStop()=0
Asynchronously signals the logger to initiate a shutdown sequence.
std::basic_string_view< char_type > string_view_type
Alias for the basic_string_view type associated with this logger's encoding.
Definition base_logger.h:41
char_t char_type
Alias for the underlying character type used by this logger.
Definition base_logger.h:31
virtual ~BasicLoggerImpl()=default
Virtual destructor to ensure proper cleanup of derived logger resources.
virtual Status status()=0
Retrieves the current operational health and state of the logger.
virtual void log(message_type oMessage)=0
Primary interface for submitting a log record.
std::basic_string< char_type > string_type
Alias for the basic_string type associated with this logger's encoding.
Definition base_logger.h:34
virtual void stop()=0
Synchronously stops the logger service.
A high-level handle for logging operations.
Definition base_logger.h:121
BasicLogger & warning(const char_t *sMsg)
Warning wrapper for string litterals.
Definition base_logger.h:423
BasicLogger & log(const Level nLevel, string_type sMessage)
Logs a message with a specific severity level.
Definition base_logger.h:247
BasicLogger & info(string_view_type sMsg)
Info wrapper for string views.
Definition base_logger.h:370
void start()
Starts the underlying logging service.
Definition base_logger.h:186
void stop()
Performs a synchronous shutdown of the logger.
Definition base_logger.h:195
BasicLogger & fatal(string_view_type sMsg)
Fatal wrapper for string views.
Definition base_logger.h:380
BasicLogger & operator=(const BasicLogger &pLogger)=default
Assignment operator.
BasicLogger & info(string_type sMessage)
Submits an INFO level log message.
Definition base_logger.h:297
BasicLogger(nullptr_t)=delete
Prevents construction from a null pointer.
BasicLogger & warning(string_type sMessage)
Submits a WARNING level log message.
Definition base_logger.h:311
BasicLogger & fatal(const char_t *sMsg)
Fatal wrapper for string litterals.
Definition base_logger.h:429
BasicLogger & success(string_type sMessage)
Submits a SUCCESS level log message.
Definition base_logger.h:276
BasicLogger & critical(string_view_type sMsg)
Critical wrapper for string views.
Definition base_logger.h:378
BasicLogger & message(const char_t *sMsg)
Message wrapper for string litterals.
Definition base_logger.h:421
BasicLogger & trace(const char_t *sMsg)
Trace wrapper for string litterals.
Definition base_logger.h:415
BasicLogger & success(string_view_type sMsg)
Success wrapper for string views.
Definition base_logger.h:364
BasicLogger & log(const Level nLevel, const std::exception &oException)
Logs an exception with a specific severity level.
Definition base_logger.h:462
BasicLogger & success(const char_t *sMsg)
Success wrapper for string litterals.
Definition base_logger.h:413
BasicLogger & critical(const char_t *sMsg)
Critical wrapper for string litterals.
Definition base_logger.h:427
BasicLogger & info(const char_t *sMsg)
Info wrapper for string litterals.
Definition base_logger.h:419
BasicLogger & fatal(string_type sMessage)
Submits a FATAL level log message.
Definition base_logger.h:332
BasicLogger & message(string_type sMessage)
Submits a MESSAGE level log message.
Definition base_logger.h:304
~BasicLogger()
Destructor that ensures a graceful shutdown of the logger.
Definition base_logger.h:145
BasicLogger & critical(string_type sMessage)
Submits a CRITICAL level log message.
Definition base_logger.h:325
typename BasicLoggerImpl< char_t >::string_view_type string_view_type
Alias for the basic_string_view type associated with this logger's encoding.
Definition base_logger.h:177
BasicLogger & log(string_type sLevel, const char_t *sMessage)
Logs a message view with a custom string level.
Definition base_logger.h:405
BasicLogger & message(string_view_type sMsg)
Message wrapper for string views.
Definition base_logger.h:372
BasicLogger(const BasicLogger &pLogger)=default
Copy constructor (defaults to shared ownership).
typename BasicLoggerImpl< char_t >::char_type char_type
Alias for the underlying character type used by this logger.
Definition base_logger.h:167
BasicLogger(std::shared_ptr< BasicLoggerImpl< char_t > > pLogger)
Protected constructor for derived factories.
Definition base_logger.h:131
BasicLogger & log(const Level nLevel, string_view_type sMessage)
Logs a message view with a specific severity level.
Definition base_logger.h:346
BasicLogger & trace(string_view_type sMsg)
Trace wrapper for string views.
Definition base_logger.h:366
BasicLogger & log(string_type sLevel, string_type sMessage)
Logs a message with a custom string-based level.
Definition base_logger.h:259
typename BasicLoggerImpl< char_t >::message_type message_type
Alias for the BasicMessage type associated to the logger.
Definition base_logger.h:173
Status status()
Retrieves the current operational status of the logger.
Definition base_logger.h:223
BasicLogger & trace(string_type sMessage)
Submits a TRACE level log message.
Definition base_logger.h:283
BasicLogger & error(const char_t *sMsg)
Error wrapper for string litterals.
Definition base_logger.h:425
typename BasicLoggerImpl< char_t >::string_type string_type
Alias for the basic_string type associated with this logger's encoding.
Definition base_logger.h:170
BasicLogger & error(string_view_type sMsg)
Error wrapper for string views.
Definition base_logger.h:376
void signalStop()
Initiates an asynchronous shutdown request.
Definition base_logger.h:205
BasicLogger & log(const std::exception &oException)
Logs an exception with the default ERROR level.
Definition base_logger.h:449
BasicLogger & error(string_type sMessage)
Submits an ERROR level log message.
Definition base_logger.h:318
BasicLogger & log(const Level nLevel, const char_t *sMessage)
Logs a message view with a specific severity level.
Definition base_logger.h:395
BasicLogger & warning(string_view_type sMsg)
Warning wrapper for string views.
Definition base_logger.h:374
BasicLogger(BasicLogger &&pLogger)=default
Move constructor.
BasicLogger & log(string_type sLevel, const std::exception &oException)
Logs an exception with a custom string level.
Definition base_logger.h:475
BasicLogger & debug(const char_t *sMsg)
Debug wrapper for string litterals.
Definition base_logger.h:417
BasicLogger & debug(string_type sMessage)
Submits a DEBUG level log message.
Definition base_logger.h:290
void join()
Blocks until the asynchronous shutdown sequence is complete.
Definition base_logger.h:215
BasicLogger & log(message_type oMessage)
Submits a pre-constructed message object to the logger.
Definition base_logger.h:236
BasicLogger & operator=(BasicLogger &&pLogger)=default
Assignment operator.
BasicLogger & debug(string_view_type sMsg)
Debug wrapper for string views.
Definition base_logger.h:368
BasicLogger & log(string_type sLevel, string_view_type sMessage)
Logs a message view with a custom string level.
Definition base_logger.h:356
Container for log message implementations.
Definition message.h:76
A standard string-based log message.
Definition message.h:169
Log message definitions.
Root namespace for the CppTrail logging library.
Definition async_logger.h:24
Level
Severity levels for log entries.
Definition def.h:42
@ WARNING
Indications of potential issues or non-critical failures.
@ FATAL
Terminal errors that require immediate application termination.
@ TRACE
Extremely fine-grained diagnostic events (wire-level).
@ INFO
General operational messages about application progress.
@ MESSAGE
High-level communication or protocol-specific messages.
@ CRITICAL
Severe failures that may lead to localized component shutdown.
@ ERROR
Significant issues that require attention but allow continued execution.
@ SUCCESS
Positive confirmation of a successful operation.
@ DEBUG
Information useful for application debugging.
Status
Represents the current operational state of a Logger implementation.
Definition def.h:31