CppTrail
Loading...
Searching...
No Matches
sync_logger.h
Go to the documentation of this file.
1
10#ifndef CPPTRAIL_SYNC_LOGGER_H
11#define CPPTRAIL_SYNC_LOGGER_H
12
13#include <mutex>
14
16
17namespace CppTrail {
28 template<typename char_t>
29 class BasicSyncLoggerImpl : public BasicLoggerImpl<char_t> {
30 public:
33
36
39
40 protected:
46 ~BasicSyncLoggerImpl() override = default;
47
48 protected:
54 virtual void work(message_type oMessage) = 0;
55
56 public:
70 void log(message_type oMessage) final {
71 std::unique_lock<std::mutex> oLock(this->m_oGeneralMutex);
72 auto nStatus = this->serviceStatus();
73 if (nStatus == Status::TRASCENDENT || nStatus == Status::RUNNING)
74 this->work(std::move(oMessage));
75 }
76
77 public:
82 void start() final {
83 std::unique_lock<std::mutex> oLock(this->m_oGeneralMutex);
84 this->serviceStart();
85 }
86
91 void stop() final {
92 std::unique_lock<std::mutex> oLock(this->m_oGeneralMutex);
93 this->serviceStop();
94 }
95
101 void signalStop() final {
102 std::unique_lock<std::mutex> oLock(this->m_oGeneralMutex);
103 this->serviceStop();
104 }
105
110 void join() override {
111 }
112
117 Status status() final {
118 std::unique_lock<std::mutex> oLock(this->m_oGeneralMutex);
119 return this->serviceStatus();
120 }
121
122 protected:
129 virtual Status serviceStatus() = 0;
130
132 virtual void serviceStart() = 0;
133
135 virtual void serviceStop() = 0;
136
139 private:
141 std::mutex m_oGeneralMutex;
142 };
143
150
153
154#if __cplusplus >= 202002L
157#endif
158
161
164
166}
167
168#endif
Core logging interfaces and high-level handle abstractions.
Abstract template interface defining the mandatory contract for all loggers.
Definition base_logger.h:28
char_t char_type
Alias for the underlying character type used by this logger.
Definition base_logger.h:31
std::basic_string< char_type > string_type
Alias for the basic_string type associated with this logger's encoding.
Definition base_logger.h:34
Container for log message implementations.
Definition message.h:76
A synchronous, thread-safe logger implementation template. This class ensures that every log call is ...
Definition sync_logger.h:29
typename BasicLoggerImpl< char_t >::string_type string_type
Alias for the basic_string type associated with this logger's encoding.
Definition sync_logger.h:35
typename BasicLoggerImpl< char_t >::message_type message_type
Alias for the BasicMessage type associated to the logger.
Definition sync_logger.h:38
typename BasicLoggerImpl< char_t >::char_type char_type
Alias for the underlying character type used by this logger.
Definition sync_logger.h:32
virtual Status serviceStatus()=0
Non-locking call to get the current service status.
void join() override
Waits for the asynchronous end of the shutdown sequence.
Definition sync_logger.h:110
Status status() final
Retrieves the current status of the service.
Definition sync_logger.h:117
virtual void work(message_type oMessage)=0
Abstract method where the actual logging I/O occurs.
void stop() final
Stops the logger service.
Definition sync_logger.h:91
void signalStop() final
Signals the logger to stop.
Definition sync_logger.h:101
void log(message_type oMessage) final
Synchronously processes and records a log message.
Definition sync_logger.h:70
virtual void serviceStop()=0
Non-locking call to initiate service shutdown.
void start() final
Starts the logger service.
Definition sync_logger.h:82
virtual void serviceStart()=0
Non-locking call to initiate service startup.
~BasicSyncLoggerImpl() override=default
Destructor.
Root namespace for the CppTrail logging library.
Definition async_logger.h:24
Status
Represents the current operational state of a Logger implementation.
Definition def.h:31
@ TRASCENDENT
The logger operates outside standard lifecycle management.
@ RUNNING
The logger is active and processing entries.