CppTrail
Loading...
Searching...
No Matches
ostream_logger.h
Go to the documentation of this file.
1
11#ifndef CPPTRAIL_OSTREAM_LOGGER_H
12#define CPPTRAIL_OSTREAM_LOGGER_H
13
14#if __cplusplus >= 202002L
15#include "rfc3339.h"
16#endif
17
18#include <iostream>
19
20#include "cpptrail/logger.h"
21
22namespace CppTrail {
31 template<typename char_t>
32 class BasicSyncOstreamLogger : public BasicLogger<char_t> {
33 public:
40 std::basic_ostream<char_t> &oOstream,
41 const bool bUseLocalTime = true
43 std::reference_wrapper<std::basic_ostream<char_t> >(oOstream),
44 bUseLocalTime
45 ) {
46 }
47
56 std::reference_wrapper<std::basic_ostream<char_t> > oOstream,
57 const bool bUseLocalTime = true
58 ) : BasicLogger<char_t>(
59 std::make_shared<Impl>(oOstream, bUseLocalTime)
60 ) {
61 }
62
63 private:
71 class Impl : public BasicSyncLoggerImpl<char_t> {
72 public:
74 using char_type = typename BasicLoggerImpl<char_t>::char_type;
75
77 using string_type = typename BasicLoggerImpl<char_t>::string_type;
78
80 using message_type = typename BasicLoggerImpl<char_t>::message_type;
81
82 public:
88 Impl(
89 std::reference_wrapper<std::basic_ostream<char_t> > oOstream,
90 const bool bUseLocalTime
91 ) : m_oOstream(oOstream),
92 m_bUseLocalTime(bUseLocalTime) {
93 }
94
95 ~Impl() override {
96 this->stop();
97 this->join();
98 }
99
100 protected:
115 void work(message_type oMessage) override;
116
121 Status serviceStatus() override {
122 return Status::TRASCENDENT;
123 }
124
126 void serviceStart() override {
127 }
128
130 void serviceStop() override {
131 }
132
133 private:
135 std::reference_wrapper<std::basic_ostream<char_t> > m_oOstream;
136
138 bool m_bUseLocalTime;
139 };
140 };
141
148
151
152#if __cplusplus >= 202002L
155#endif
156
159
162
175 public:
181 }
182 };
183
194 public:
200 }
201 };
202
216 template<typename char_t>
217 class BasicAsyncOstreamLogger : public BasicLogger<char_t> {
218 public:
225 std::basic_ostream<char_t> &oOstream,
226 const bool bUseLocalTime = true
228 std::reference_wrapper<std::basic_ostream<char_t> >(oOstream),
229 bUseLocalTime
230 ) {
231 }
232
241 std::reference_wrapper<std::basic_ostream<char_t> > oOstream,
242 const bool bUseLocalTime = true
243 ) : BasicLogger<char_t>(
244 std::make_shared<Impl>(oOstream, bUseLocalTime)
245 ) {
246 }
247
260 std::reference_wrapper<std::basic_ostream<char_t> > oOstream,
261 const bool bUseLocalTime,
262 std::size_t nMaxEntryCount,
263 bool bThrowOnOverflow
264 ) : BasicLogger<char_t>(
265 std::make_shared<Impl>(
266 oOstream,
267 bUseLocalTime,
268 nMaxEntryCount,
269 bThrowOnOverflow
270 )
271 ) {
272 }
273
274 private:
286 class Impl : public BasicAsyncLoggerImpl<char_t> {
287 public:
289 using char_type = typename BasicLoggerImpl<char_t>::char_type;
290
292 using string_type = typename BasicLoggerImpl<char_t>::string_type;
293
295 using message_type = typename BasicLoggerImpl<char_t>::message_type;
296
297 public:
303 Impl(
304 std::reference_wrapper<std::basic_ostream<char_t> > oOstream,
305 const bool bUseLocalTime
306 ) : m_oOstream(oOstream),
307 m_bUseLocalTime(bUseLocalTime) {
308 }
309
319 Impl(
320 std::reference_wrapper<std::basic_ostream<char_t> > oOstream,
321 const bool bUseLocalTime,
322 std::size_t nMaxEntryCount,
323 bool bThrowOnOverflow
324 ) : BasicAsyncLoggerImpl<char_t>(
325 nMaxEntryCount,
326 bThrowOnOverflow,
327 1
328 ), m_oOstream(oOstream),
329 m_bUseLocalTime(bUseLocalTime) {
330 }
331
345 ~Impl() override {
346 this->stop();
347 this->join();
348 }
349
350 protected:
365 void work(message_type oMessage) override;
366
371 Status serviceStatus() override {
372 return Status::TRASCENDENT;
373 }
374
376 void serviceStart() override {
377 }
378
380 void serviceStop() override {
381 }
382
383 private:
385 std::reference_wrapper<std::basic_ostream<char_t> > m_oOstream;
387 bool m_bUseLocalTime;
388 };
389 };
390
397
400
401#if __cplusplus >= 202002L
404#endif
405
408
411
426 public:
431 }
432 };
433
447 public:
452 }
453 };
454
455 template<typename char_t>
456 void BasicSyncOstreamLogger<char_t>::Impl::work(
457 message_type oMessage
458 ) {
459 // Get ostream
460 std::basic_ostream<char_t> &oOstream = m_oOstream.get();
461
462#if __cplusplus >= 202002L
463 // Write time
464 Detail::toRfc3339<char_t>(oOstream, this->m_bUseLocalTime);
465#endif
466
467 // Write level + message
468 oOstream << oMessage.stealLevel();
469 oOstream.put(static_cast<char_t>(' '));
470 oOstream << oMessage.stealString();
471 oOstream.put(static_cast<char_t>('\n'));
472
473 // Flush
474 oOstream.flush();
475 }
476
477 template<typename char_t>
478 void BasicAsyncOstreamLogger<char_t>::Impl::work(
479 message_type oMessage
480 ) {
481 // Get ostream
482 std::basic_ostream<char_t> &oOstream = m_oOstream.get();
483
484#if __cplusplus >= 202002L
485 // Write time
486 Detail::toRfc3339<char_t>(oOstream, this->m_bUseLocalTime);
487#endif
488
489 // Write level
490 oOstream << oMessage.stealLevel();
491
492 // Write message
493 oOstream.put(static_cast<char_t>(' '));
494 oOstream << oMessage.stealString();
495 oOstream.put(static_cast<char_t>('\n'));
496
497 // Flush
498 oOstream.flush();
499 }
500}
501
502#endif
A specialized asynchronous logger handle targeting the standard error stream (std::cerr).
Definition ostream_logger.h:446
AsyncCerrLogger()
Constructs a CerrAsyncLogger initialized with the std::cerr sink.
Definition ostream_logger.h:451
A specialized asynchronous logger handle targeting the standard output stream (std::cout).
Definition ostream_logger.h:425
AsyncCoutLogger()
Constructs a CoutAsyncLogger initialized with the std::cout sink.
Definition ostream_logger.h:430
An asynchronous logger template utilizing background worker threads and object recycling....
Definition async_logger.h:53
void join() final
Blocks until all background activity (workers and killer threads) has ceased.
Definition async_logger.h:471
BasicAsyncLoggerImpl()
Default constructor.
Definition async_logger.h:69
void stop() final
Synchronously shuts down the worker pool and the service.
Definition async_logger.h:413
An asynchronous logger handle that dispatches output to a C++ standard stream.
Definition ostream_logger.h:217
BasicAsyncOstreamLogger(std::basic_ostream< char_t > &oOstream, const bool bUseLocalTime=true)
Constructs a AsyncLogger from a raw stream reference.
Definition ostream_logger.h:224
BasicAsyncOstreamLogger(std::reference_wrapper< std::basic_ostream< char_t > > oOstream, const bool bUseLocalTime, std::size_t nMaxEntryCount, bool bThrowOnOverflow)
Constructs a AsyncLogger from a reference_wrapper.
Definition ostream_logger.h:259
BasicAsyncOstreamLogger(std::reference_wrapper< std::basic_ostream< char_t > > oOstream, const bool bUseLocalTime=true)
Constructs a AsyncLogger from a reference_wrapper.
Definition ostream_logger.h:240
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
A high-level handle for logging operations.
Definition base_logger.h:121
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
void join() override
Waits for the asynchronous end of the shutdown sequence.
Definition sync_logger.h:110
void stop() final
Stops the logger service.
Definition sync_logger.h:91
A synchronous logger handle that directs output to a C++ standard output stream.
Definition ostream_logger.h:32
BasicSyncOstreamLogger(std::basic_ostream< char_t > &oOstream, const bool bUseLocalTime=true)
Constructs a logger from a raw stream reference.
Definition ostream_logger.h:39
BasicSyncOstreamLogger(std::reference_wrapper< std::basic_ostream< char_t > > oOstream, const bool bUseLocalTime=true)
Constructs a logger from a reference_wrapper.
Definition ostream_logger.h:55
A specialized logger handle targeting the standard error stream (std::cerr).
Definition ostream_logger.h:193
SyncCerrLogger()
Constructs a new CerrLogger.
Definition ostream_logger.h:199
A specialized logger handle targeting the standard output stream (std::cout).
Definition ostream_logger.h:174
SyncCoutLogger()
Constructs a new CoutLogger.
Definition ostream_logger.h:180
High-level wrapper for the CppTrail logging system.
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.