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  // maximize cache locality, taking into consideration wchar_t size and logger_sbo size
52  using pattern_string_t =
53 #if QX_MSVC
55 #else
57 #endif
58  ;
59 
60 public:
61  /**
62  @brief base_logger_stream object constructor
63  @param streamConfig - logger configuration
64  **/
65  base_logger_stream(const config& streamConfig) noexcept;
66 
67  base_logger_stream(base_logger_stream&&) noexcept = default;
68 
69  /**
70  @brief Set a message pattern that will be used for this stream
71  @see qx::logger_specifiers for details
72  @warning Not thread safe on purpose. Set it before you pass this stream to a logger!
73  @param sPattern - a pattern for the stream
74  @retval - pattern compilation result
75  **/
76  compile_pattern_result set_pattern(pattern_string_t sPattern) noexcept;
77 
78  /**
79  @brief Get a message pattern if present
80  @retval - a pattern for the stream
81  **/
82  std::optional<string_view> get_pattern() const noexcept;
83 
84  /**
85  @brief Output to stream
86  @tparam char_t - char type, typically char or wchar_t
87  @param category - code category
88  @param eVerbosity - message verbosity
89  @param threadId - thread where the log is called
90  @param messageTime - message creation time
91  @param svFile - file name string
92  @param svFunction - function name string
93  @param nLine - code line number
94  @param svMessage - formatted log line
95  **/
96  void log(
97  const category& category,
98  verbosity eVerbosity,
99  std::thread::id threadId,
100  std::chrono::system_clock::time_point messageTime,
101  string_view svFile,
102  string_view svFunction,
103  int nLine,
104  string_view svMessage);
105 
106  /**
107  @brief Flush the stream
108  **/
109  void flush();
110 
111  /**
112  @brief Returns true if this message should be processed by this stream
113  even if the logger filters did not pass it.
114  @param category - code category
115  @param eVerbosity - message verbosity
116  @param threadId - thread where the log is called
117  @param messageTime - message creation time
118  @param svFile - file name string
119  @param svFunction - function name string
120  @param nLine - code line number
121  @retval - true is this message is unconditionally required by this stream
122  **/
123  virtual bool log_unconditionally_required(
124  const category& category,
125  verbosity eVerbosity,
126  std::thread::id threadId,
127  std::chrono::system_clock::time_point messageTime,
128  string_view svFile,
129  string_view svFunction,
130  int nLine) const noexcept;
131 
132 private:
133  /**
134  @brief Proceed stream logging
135  @param category - code category
136  @param eVerbosity - this message verbosity
137  @param threadId - thread where the log is called
138  @param messageTime - message creation time
139  @param svFile - file name string
140  @param svFunction - function name string
141  @param nLine - code line number
142  @param svMessage - message string
143  **/
144  virtual void do_log(
145  const category& category,
146  verbosity eVerbosity,
147  std::thread::id threadId,
148  std::chrono::system_clock::time_point messageTime,
149  string_view svFile,
150  string_view svFunction,
151  int nLine,
152  string_view svMessage) = 0;
153 
154  /**
155  @brief Flush the stream
156  **/
157  virtual void do_flush() = 0;
158 
159 private:
160  // memory order: max packing (important for logger_sbo)
161  std::optional<pattern_string_t> m_optPattern;
162  std::unique_ptr<std::recursive_mutex> m_pMutex;
163  verbosity m_eMinFlushVerbosity = verbosity::error;
164  bool m_bProtectLog = true;
165 };
166 
167 } // namespace qx
168 
169 #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.
compile_pattern_result set_pattern(pattern_string_t sPattern) noexcept
Set a message pattern that will be used for this stream.
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
String class.
Definition: string.h:83
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:62
Polymorphic base for qx class identity and inheritance queries.
Definition: rtti.h:69
Contains perf scope macros for profiler (for internal usage only, but user may override them)
RTTI system based on polymorphism.