qxLib
base_logger_stream.h
Go to the documentation of this file.
1 /**
2 
3  @file base_logger_stream.h
4  @author Khrapov
5  @date 28.07.2021
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/category.h>
13 #include <qx/internal/perf_scope.h>
14 #include <qx/verbosity.h>
15 
16 #include <iostream>
17 #include <memory>
18 #include <mutex>
19 
20 QX_DEFINE_CATEGORY(CatLogger, qx::color::dark_turquoise());
21 
22 namespace qx
23 {
24 
25 /**
26 
27  @class base_logger_stream
28  @brief Base class for logger streams
29  @author Khrapov
30  @date 28.07.2021
31 
32 **/
34 {
35 public:
36  struct config
37  {
38  // if it's required to protect log with a mutex
39  bool bProtectLog = true;
40 
41  // the minimum verbosity level at which flush will be called
42  verbosity eMinFlushVerbosity = verbosity::error;
43  };
44 
45 public:
46  /**
47  @brief base_logger_stream object constructor
48  @param streamConfig - logger configuration
49  **/
50  base_logger_stream(const config& streamConfig) noexcept;
51 
52  base_logger_stream(base_logger_stream&&) noexcept = default;
53 
54  virtual ~base_logger_stream() noexcept = default;
55 
56  /**
57  @brief Output to stream
58  @tparam char_t - char type, typically char or wchar_t
59  @param category - code category
60  @param eVerbosity message verbosity
61  @param svMessage - formatted log line
62  **/
63  void log(const category& category, verbosity eVerbosity, string_view svMessage);
64 
65  /**
66  @brief Flush the stream
67  **/
68  void flush();
69 
70 private:
71  /**
72  @brief Proceed stream logging
73  @param eVerbosity - this message verbosity
74  @param category - code category
75  @param svMessage - message string
76  **/
77  virtual void do_log(const category& category, verbosity eVerbosity, string_view svMessage) = 0;
78 
79  /**
80  @brief Flush the stream
81  **/
82  virtual void do_flush() = 0;
83 
84 private:
85  std::unique_ptr<std::recursive_mutex> m_pMutex;
86  bool m_bProtectLog = true;
87  verbosity m_eMinFlushVerbosity = verbosity::error;
88 };
89 
90 } // namespace qx
91 
92 #include <qx/logger/base_logger_stream.inl>
#define QX_DEFINE_CATEGORY(name,...)
Define a category.
Definition: category.h:21
Base class for logger streams.
void log(const category &category, verbosity eVerbosity, string_view svMessage)
Output to stream.
void flush()
Flush the stream.
base_logger_stream(const config &streamConfig) noexcept
base_logger_stream object constructor
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:59
Contains perf scope macros for profiler (for internal usage only, but user may override them)