qxLib
logger.h
Go to the documentation of this file.
1 /**
2 
3  @file logger.h
4  @author Khrapov
5  @date 17.06.2019
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
12 #include <qx/patterns/singleton.h>
13 
14 #include <memory>
15 
16 #ifndef QX_LOGGER_INSTANCE
17  #define QX_LOGGER_INSTANCE qx::logger_singleton::get_instance()
18 #endif
19 
20 /**
21  @brief Log with category
22  @param category - category to be used to manage output
23  @param eVerbosity - message verbosity
24  @param format - format string
25  @param ... - additional args for formatting
26 **/
27 #define QX_LOG_C(category, eVerbosity, format, ...) \
28  QX_LOGGER_INSTANCE \
29  .log(eVerbosity, format, category, QX_SHORT_FILE, qx::to_string(__FUNCTION__), __LINE__, ##__VA_ARGS__)
30 
31 /**
32  @def QX_LOG
33  @brief Log message
34  @param eVerbosity - message verbosity
35  @param format - format string
36  @param ... - additional args for formatting
37 **/
38 #define QX_LOG(eVerbosity, format, ...) QX_LOG_C(CatDefault, eVerbosity, format, ##__VA_ARGS__)
39 
40 namespace qx
41 {
42 
43 template<class... args_t>
44 concept log_acceptable_args = (sizeof...(args_t) > 0 && format_acceptable_args<char_type, args_t...>);
45 
46 /**
47 
48  @class logger
49  @brief Logger class
50  @details ~
51  @author Khrapov
52  @date 10.01.2020
53 
54 **/
55 class logger
56 {
57 public:
58  /**
59  @brief Log to all streams
60  @param eVerbosity - message verbosity
61  @param svFormat - format string
62  @param category - code category
63  @param svFile - file name string
64  @param svFunction - function name string
65  @param nLine - code line number
66  **/
67  void log(
68  verbosity eVerbosity,
69  string_view svFormat,
70  const category& category,
71  string_view svFile,
72  string_view svFunction,
73  int nLine);
74 
75  /**
76  @brief Log to all streams
77  @tparam args_t - template parameter pack type
78  @param eVerbosity - message verbosity
79  @param sFormat - format string
80  @param category - code category
81  @param svFile - file name string
82  @param svFunction - function name string
83  @param nLine - code line number
84  @param args - additional args for format
85  **/
86  template<class... args_t>
87  requires(log_acceptable_args<args_t...>)
88  void log(
89  verbosity eVerbosity,
91  const category& category,
92  string_view svFile,
93  string_view svFunction,
94  int nLine,
95  const args_t&... args);
96 
97  /**
98  @brief Flush all streams
99  **/
100  void flush();
101 
102  /**
103  @brief Add output stream to the logger
104  @param pStream - stream unique pointer
105  **/
106  void add_stream(std::unique_ptr<base_logger_stream> pStream) noexcept;
107 
108  /**
109  @brief Reset logger and clear all streams
110  **/
111  void reset() noexcept;
112 
113  /**
114  @brief Returns true if any of streams will accept this message
115  @details Typically you don't want to use it
116  It may be useful with async logging to avoid unnecessary formatting and queueing
117  @param category - code category
118  @param eVerbosity - message verbosity
119  @param svFile - file name string
120  @param svFunction - function name string
121  @retval - true if any of streams will accept this message
122  **/
124  const category& category,
125  verbosity eVerbosity,
126  string_view svFile,
127  string_view svFunction) const noexcept;
128 
129 private:
130  std::vector<std::unique_ptr<base_logger_stream>> m_Streams;
131 };
132 
133 /**
134 
135  @class logger_singleton
136  @brief Default logger instance
137  @details ~
138  @author Khrapov
139  @date 19.08.2021
140 
141 **/
142 class logger_singleton : public logger
143 {
145 };
146 
147 } // namespace qx
148 
149 #include <qx/logger/logger.inl>
Base class for logger streams.
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:56
Default logger instance.
Definition: logger.h:143
Logger class.
Definition: logger.h:56
void log(verbosity eVerbosity, string_view svFormat, const category &category, string_view svFile, string_view svFunction, int nLine)
Log to all streams.
Definition: logger.inl:13
bool will_any_stream_accept(const category &category, verbosity eVerbosity, string_view svFile, string_view svFunction) const noexcept
Returns true if any of streams will accept this message.
Definition: logger.inl:57
void add_stream(std::unique_ptr< base_logger_stream > pStream) noexcept
Add output stream to the logger.
Definition: logger.inl:47
requires(log_acceptable_args< args_t... >) void log(verbosity eVerbosity
Log to all streams.
void reset() noexcept
Reset logger and clear all streams.
Definition: logger.inl:52
void flush()
Flush all streams.
Definition: logger.inl:41
#define QX_SINGLETON(T)
Simple Meyer's singleton.
Definition: singleton.h:90
std::basic_format_string wrapper that performs additional compile time checks
Definition: format_string.h:29