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 (c) Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/category.h>
13 #include <qx/internal/perf_scope.h>
15 #include <qx/rtti/rtti.h>
16 #include <qx/verbosity.h>
17 
18 #include <chrono>
19 #include <iostream>
20 #include <memory>
21 #include <mutex>
22 #include <thread>
23 
24 QX_DEFINE_CATEGORY(CatLogger, qx::color::dark_turquoise());
25 
26 namespace qx
27 {
28 
29 /**
30 
31  @class base_logger_stream
32  @brief Base class for logger streams
33  @author Khrapov
34  @date 28.07.2021
35 
36 **/
37 class base_logger_stream : public rtti_root<>
38 {
39  QX_RTTI_CLASS(base_logger_stream, rtti_root<>);
40 
41 public:
42  struct config
43  {
44  // if it's required to protect log with a mutex
45  bool bProtectLog = true;
46 
47  // the minimum verbosity level at which flush will be called
48  verbosity eMinFlushVerbosity = verbosity::error;
49  };
50 
51 public:
52  /**
53  @brief base_logger_stream object constructor
54  @param streamConfig - logger configuration
55  **/
56  base_logger_stream(const config& streamConfig) noexcept;
57 
58  base_logger_stream(base_logger_stream&&) noexcept = default;
59 
60  /**
61  @brief Set a message pattern that will be used for this stream
62  @see qx::logger_specifiers for details
63  @warning Not thread safe on purpose. Set it before you pass this stream to a logger!
64  @param sPattern - a pattern for the stream
65  @retval - pattern compilation result
66  **/
67  compile_pattern_result set_pattern(string sPattern) noexcept;
68 
69  /**
70  @brief Get a message pattern if present
71  @retval - a pattern for the stream
72  **/
73  std::optional<string_view> get_pattern() const noexcept;
74 
75  /**
76  @brief Output to stream
77  @tparam char_t - char type, typically char or wchar_t
78  @param category - code category
79  @param eVerbosity - message verbosity
80  @param threadId - thread where the log is called
81  @param messageTime - message creation time
82  @param svFile - file name string
83  @param svFunction - function name string
84  @param nLine - code line number
85  @param svMessage - formatted log line
86  **/
87  void log(
88  const category& category,
89  verbosity eVerbosity,
90  std::thread::id threadId,
91  std::chrono::system_clock::time_point messageTime,
92  string_view svFile,
93  string_view svFunction,
94  int nLine,
95  string_view svMessage);
96 
97  /**
98  @brief Flush the stream
99  **/
100  void flush();
101 
102  /**
103  @brief Returns true if this message should be processed by this stream
104  even if the logger filters did not pass it.
105  @param category - code category
106  @param eVerbosity - message verbosity
107  @param threadId - thread where the log is called
108  @param messageTime - message creation time
109  @param svFile - file name string
110  @param svFunction - function name string
111  @param nLine - code line number
112  @retval - true is this message is unconditionally required by this stream
113  **/
114  virtual bool log_unconditionally_required(
115  const category& category,
116  verbosity eVerbosity,
117  std::thread::id threadId,
118  std::chrono::system_clock::time_point messageTime,
119  string_view svFile,
120  string_view svFunction,
121  int nLine) const noexcept;
122 
123 private:
124  /**
125  @brief Proceed stream logging
126  @param category - code category
127  @param eVerbosity - this message verbosity
128  @param threadId - thread where the log is called
129  @param messageTime - message creation time
130  @param svFile - file name string
131  @param svFunction - function name string
132  @param nLine - code line number
133  @param svMessage - message string
134  **/
135  virtual void do_log(
136  const category& category,
137  verbosity eVerbosity,
138  std::thread::id threadId,
139  std::chrono::system_clock::time_point messageTime,
140  string_view svFile,
141  string_view svFunction,
142  int nLine,
143  string_view svMessage) = 0;
144 
145  /**
146  @brief Flush the stream
147  **/
148  virtual void do_flush() = 0;
149 
150 private:
151  std::optional<string> m_optPattern;
152  std::unique_ptr<std::recursive_mutex> m_pMutex;
153  verbosity m_eMinFlushVerbosity = verbosity::error;
154  bool m_bProtectLog = true;
155 };
156 
157 } // namespace qx
158 
159 #include <qx/logger/base_logger_stream.inl>
#define QX_DEFINE_CATEGORY(name,...)
Define a category.
Definition: category.h:24
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...
std::optional< string_view > get_pattern() const noexcept
Get a message pattern if present.
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
compile_pattern_result set_pattern(string sPattern) noexcept
Set a message pattern that will be used for this stream.
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:62
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.