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/rtti/rtti.h>
15 #include <qx/verbosity.h>
16 
17 #include <chrono>
18 #include <iostream>
19 #include <memory>
20 #include <mutex>
21 #include <thread>
22 
23 QX_DEFINE_CATEGORY(CatLogger, qx::color::dark_turquoise());
24 
25 namespace qx
26 {
27 
28 /**
29 
30  @class base_logger_stream
31  @brief Base class for logger streams
32  @author Khrapov
33  @date 28.07.2021
34 
35 **/
36 class base_logger_stream : public rtti_root<>
37 {
38  QX_RTTI_CLASS(base_logger_stream, rtti_root<>);
39 
40 public:
41  struct config
42  {
43  // if it's required to protect log with a mutex
44  bool bProtectLog = true;
45 
46  // the minimum verbosity level at which flush will be called
47  verbosity eMinFlushVerbosity = verbosity::error;
48  };
49 
50 public:
51  /**
52  @brief base_logger_stream object constructor
53  @param streamConfig - logger configuration
54  **/
55  base_logger_stream(const config& streamConfig) noexcept;
56 
57  base_logger_stream(base_logger_stream&&) noexcept = default;
58 
59  /**
60  @brief Output to stream
61  @tparam char_t - char type, typically char or wchar_t
62  @param category - code category
63  @param eVerbosity - message verbosity
64  @param threadId - thread where the log is called
65  @param messageTime - message creation time
66  @param svFile - file name string
67  @param svFunction - function name string
68  @param nLine - code line number
69  @param svMessage - formatted log line
70  **/
71  void log(
72  const category& category,
73  verbosity eVerbosity,
74  std::thread::id threadId,
75  std::chrono::system_clock::time_point messageTime,
76  string_view svFile,
77  string_view svFunction,
78  int nLine,
79  string_view svMessage);
80 
81  /**
82  @brief Flush the stream
83  **/
84  void flush();
85 
86  /**
87  @brief Returns true if this message should be processed by this stream
88  even if the logger filters did not pass it.
89  @param category - code category
90  @param eVerbosity - message verbosity
91  @param threadId - thread where the log is called
92  @param messageTime - message creation time
93  @param svFile - file name string
94  @param svFunction - function name string
95  @param nLine - code line number
96  @retval - true is this message is unconditionally required by this stream
97  **/
98  virtual bool log_unconditionally_required(
99  const category& category,
100  verbosity eVerbosity,
101  std::thread::id threadId,
102  std::chrono::system_clock::time_point messageTime,
103  string_view svFile,
104  string_view svFunction,
105  int nLine) const noexcept;
106 
107 private:
108  /**
109  @brief Proceed stream logging
110  @param category - code category
111  @param eVerbosity - this message verbosity
112  @param threadId - thread where the log is called
113  @param messageTime - message creation time
114  @param svFile - file name string
115  @param svFunction - function name string
116  @param nLine - code line number
117  @param svMessage - message string
118  **/
119  virtual void do_log(
120  const category& category,
121  verbosity eVerbosity,
122  std::thread::id threadId,
123  std::chrono::system_clock::time_point messageTime,
124  string_view svFile,
125  string_view svFunction,
126  int nLine,
127  string_view svMessage) = 0;
128 
129  /**
130  @brief Flush the stream
131  **/
132  virtual void do_flush() = 0;
133 
134 private:
135  std::unique_ptr<std::recursive_mutex> m_pMutex;
136  bool m_bProtectLog = true;
137  verbosity m_eMinFlushVerbosity = verbosity::error;
138 };
139 
140 } // namespace qx
141 
142 #include <qx/logger/base_logger_stream.inl>
#define QX_DEFINE_CATEGORY(name,...)
Define a category.
Definition: category.h:21
Base class for logger streams.
virtual bool log_unconditionally_required(const category &category, verbosity eVerbosity, std::thread::id threadId, std::chrono::system_clock::time_point messageTime, string_view svFile, string_view svFunction, int nLine) const noexcept
Returns true if this message should be processed by this stream even if the logger filters did not pa...
void log(const category &category, verbosity eVerbosity, std::thread::id threadId, std::chrono::system_clock::time_point messageTime, string_view svFile, string_view svFunction, int nLine, 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
RTTI root class.
Definition: rtti.h:58
Contains perf scope macros for profiler (for internal usage only, but user may override them)
RTTI system based on polymorphism.