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>
15 #include <qx/verbosity.h>
16 
17 #include <ctime>
18 #include <functional>
19 #include <mutex>
20 #include <unordered_map>
21 
22 QX_DEFINE_CATEGORY(CatLogger, qx::color::dark_turquoise());
23 
24 namespace qx
25 {
26 
28 {
29  std::pair<size_t, size_t> range { 0, 0 };
30  color rangeColor = color::white();
31 };
32 
34 {
35  string sMessage; // result message (output)
36 
37  // ranges must not intersect and must increase
38  std::vector<logger_color_range> colors;
39 
40  void clear()
41  {
42  sMessage.clear();
43  colors.clear();
44  }
45 };
46 
48 {
49  using format_func = std::function<void(
50  logger_buffer& buffers,
51  verbosity eVerbosity,
52  const category& category,
53  string_view svFile,
54  string_view svFunction,
55  int nLine,
56  string_view swLogMessage)>;
57 
58  verbosity eMinVerbosity = verbosity::log;
59  format_func formatFunc;
60 };
61 
62 struct log_unit
63 {
64  const log_unit_info* pUnitInfo = nullptr;
65  string_view svUnitName;
66 };
67 
68 /**
69 
70  @class base_logger_stream
71  @brief Base class for logger streams
72  @author Khrapov
73  @date 28.07.2021
74 
75 **/
77 {
78 public:
79  static constexpr const char_type* k_svDefaultUnit = QXT("default");
80 
81 public:
82  /**
83  @brief base_logger_stream object constructor
84  @param bAlwaysFlush - true if need to flush after every output, decreases performance
85  **/
86  base_logger_stream(bool bAlwaysFlush);
87 
88  base_logger_stream(base_logger_stream&&) noexcept = default;
89 
90  virtual ~base_logger_stream() = default;
91 
92  /**
93  @brief Flush stream
94  **/
95  virtual void flush() = 0;
96 
97  /**
98  @brief Output to stream
99  @tparam char_t - char type, typically char or wchar_t
100  @param category - code category
101  @param eVerbosity message verbosity
102  @param svFile - file name string
103  @param svFunction - function name string
104  @param nLine - code line number
105  @param swLogMessage - formatted log line
106  **/
107  void log(
108  const category& category,
109  verbosity eVerbosity,
110  string_view svFile,
111  string_view svFunction,
112  int nLine,
113  string_view swLogMessage);
114 
115  /**
116  @brief Register logger unit
117  @param svUnitName - unit name (category name, file or function)
118  @param unit - unit info
119  **/
120  void register_unit(string_view svUnitName, const log_unit_info& unit) noexcept;
121 
122  /**
123  @brief Deregister logger unit
124  @param svUnitName - unit name (category name, file or function)
125  **/
126  void deregister_unit(string_view svUnitName) noexcept;
127 
128  /**
129  @brief Try to find log unit info based on trace location info
130  @param category - code category
131  @param eVerbosity - message verbosity
132  @param svFile - file string
133  @param svFunction - function string
134  @retval - log unit info if found
135  **/
136  std::optional<log_unit> get_unit_info(
137  const category& category,
138  verbosity eVerbosity,
139  string_view svFile,
140  string_view svFunction) const noexcept;
141 
142  /**
143  @brief Format time string to the buffer
144  @param sTime - output time buffer
145  @param chDateDelimiter - char to use as delimiter in date part
146  @param chTimeDelimiter - char to use as delimiter in time part
147  **/
148  static void append_time_string(string& sTime, char_type chDateDelimiter, char_type chTimeDelimiter) noexcept;
149 
150 protected:
151  /**
152  @brief Get string buffers
153  @retval - string buffers
154  **/
155  logger_buffer& get_log_buffer() noexcept;
156 
157  /**
158  @brief Format logger line
159  @param buffers - string buffers to reduce num of allocations
160  @param eVerbosity - message verbosity
161  @param category - code category
162  @param svFile - file name string
163  @param svFunction - function name string
164  @param nLine - code line number
165  @param swLogMessage - formatted log line
166  **/
167  virtual void format_line(
168  logger_buffer& buffers,
169  verbosity eVerbosity,
170  const category& category,
171  string_view svFile,
172  string_view svFunction,
173  int nLine,
174  string_view swLogMessage) noexcept;
175 
176 private:
177  /**
178  @brief Proceed stream logging
179  @param svMessage - message string
180  @param logUnit - log unit info
181  @param colors - color ranges to colorize output
182  @param eVerbosity - this message verbosity
183  **/
184  virtual void do_log(
185  string_view svMessage,
186  const log_unit& logUnit,
187  const std::vector<logger_color_range>& colors,
188  verbosity eVerbosity) = 0;
189 
190 private:
191  std::unordered_map<string_hash, log_unit_info> m_Units;
192  logger_buffer m_Buffer;
193  std::unique_ptr<std::mutex> m_pLoggerStreamMutex = std::make_unique<std::mutex>();
194  bool m_bAlwaysFlush = false;
195 };
196 
197 } // namespace qx
198 
199 #include <qx/logger/base_logger_stream.inl>
#define QX_DEFINE_CATEGORY(name,...)
Define a category.
Definition: category.h:21
Base class for logger streams.
static void append_time_string(string &sTime, char_type chDateDelimiter, char_type chTimeDelimiter) noexcept
Format time string to the buffer.
std::optional< log_unit > get_unit_info(const category &category, verbosity eVerbosity, string_view svFile, string_view svFunction) const noexcept
Try to find log unit info based on trace location info.
void log(const category &category, verbosity eVerbosity, string_view svFile, string_view svFunction, int nLine, string_view swLogMessage)
Output to stream.
logger_buffer & get_log_buffer() noexcept
Get string buffers.
virtual void flush()=0
Flush stream.
void deregister_unit(string_view svUnitName) noexcept
Deregister logger unit.
base_logger_stream(bool bAlwaysFlush)
base_logger_stream object constructor
void register_unit(string_view svUnitName, const log_unit_info &unit) noexcept
Register logger unit.
virtual void format_line(logger_buffer &buffers, verbosity eVerbosity, const category &category, string_view svFile, string_view svFunction, int nLine, string_view swLogMessage) noexcept
Format logger line.
String hash object.
Definition: string_hash.h:38
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:59
RGBA color.
Definition: color.h:193
Contains perf scope macros for profiler (for internal usage only, but user may override them)
Definition: base.h:52