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 = QX_TEXT("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  virtual ~base_logger_stream() = default;
89 
90  /**
91  @brief Flush stream
92  **/
93  virtual void flush() = 0;
94 
95  /**
96  @brief Output to stream
97  @tparam char_t - char type, typically char or wchar_t
98  @param eVerbosity message verbosity
99  @param category - code category
100  @param svFile - file name string
101  @param svFunction - function name string
102  @param nLine - code line number
103  @param swLogMessage - formatted log line
104  **/
105  void log(
106  verbosity eVerbosity,
107  const category& category,
108  string_view svFile,
109  string_view svFunction,
110  int nLine,
111  string_view swLogMessage);
112 
113  /**
114  @brief Register logger unit
115  @param svUnitName - unit name (category name, file or function)
116  @param unit - unit info
117  **/
118  void register_unit(string_view svUnitName, const log_unit_info& unit) noexcept;
119 
120  /**
121  @brief Deregister logger unit
122  @param svUnitName - unit name (category name, file or function)
123  **/
124  void deregister_unit(string_view svUnitName) noexcept;
125 
126  /**
127  @brief Try to find log unit info based on trace location info
128  @param category - code category
129  @param eVerbosity - message verbosity
130  @param svFile - file string
131  @param svFunction - function string
132  @retval - log unit info if found
133  **/
134  std::optional<log_unit> get_unit_info(
135  const category& category,
136  verbosity eVerbosity,
137  string_view svFile,
138  string_view svFunction) const noexcept;
139 
140  /**
141  @brief Format time string to the buffer
142  @param sTime - output time buffer
143  @param chDateDelimiter - char to use as delimiter in date part
144  @param chTimeDelimiter - char to use as delimiter in time part
145  **/
146  static void append_time_string(string& sTime, char_type chDateDelimiter, char_type chTimeDelimiter) noexcept;
147 
148 protected:
149  /**
150  @brief Get string buffers
151  @retval - string buffers
152  **/
153  logger_buffer& get_log_buffer() noexcept;
154 
155  /**
156  @brief Format logger line
157  @param buffers - string buffers to reduce num of allocations
158  @param eVerbosity - message verbosity
159  @param category - code category
160  @param svFile - file name string
161  @param svFunction - function name string
162  @param nLine - code line number
163  @param swLogMessage - formatted log line
164  **/
165  virtual void format_line(
166  logger_buffer& buffers,
167  verbosity eVerbosity,
168  const category& category,
169  string_view svFile,
170  string_view svFunction,
171  int nLine,
172  string_view swLogMessage) noexcept;
173 
174 private:
175  /**
176  @brief Proceed stream logging
177  @param svMessage - message string
178  @param logUnit - log unit info
179  @param colors - color ranges to colorize output
180  @param eVerbosity - this message verbosity
181  **/
182  virtual void do_log(
183  string_view svMessage,
184  const log_unit& logUnit,
185  const std::vector<logger_color_range>& colors,
186  verbosity eVerbosity) = 0;
187 
188 private:
189  std::unordered_map<string_hash, log_unit_info> m_Units;
190  logger_buffer m_Buffer;
191  QX_PERF_MUTEX(m_LoggerStreamMutex);
192  bool m_bAlwaysFlush = false;
193 };
194 
195 } // namespace qx
196 
197 #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.
void log(verbosity eVerbosity, const category &category, string_view svFile, string_view svFunction, int nLine, string_view swLogMessage)
Output to stream.
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.
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:56
RGBA color.
Definition: color.h:37
Contains perf scope macros for profiler (for internal usage only, but user may override them)