qxLib
logger_specifiers.h
Go to the documentation of this file.
1 /**
2 
3  @file logger_specifiers.h
4  @author Khrapov
5  @date 9.09.2026
6  @copyright © Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/category.h>
13 #include <qx/verbosity.h>
14 
15 #include <chrono>
16 #include <thread>
17 
18 namespace qx
19 {
20 
21 // Values that can be used in logger patterns.
22 // Must be kept in line with format_log_specifiers().
23 // Format specifiers are intentionally small to let them fit in SBO more often.
24 enum class logger_specifiers
25 {
26  // Type: `qx::category`
27  // Usage: `{c}`
28  // Supports `{c:l}` ("log"),
29  // which results in `[CatName]` and so on, and `` for `CatDefault`.
30  category = 0,
31 
32  // Type: `qx::verbosity`
33  // Usage: `{v}`
34  // Supports `{v:l}` ("log"),
35  // which results in `[V]` ("verbose") and so on, and ` ` for the `qx::verbosity::log`.
36  verbosity = 1,
37 
38  // Type: `std::thread::id`
39  // Usage: `{ti}`
40  // Always results in `0` for now (`std::thread::id` is formattable since C++23).
41  thread_id = 2,
42 
43  // Type: `std::chrono::system_clock::time_point`
44  // Usage: `{t}`
45  // Seconds floored value.
46  time = 3,
47 
48  // Type: `qx::string_view`
49  // Usage: `{file}`
50  // QX_SHORT_FILE value
51  file = 4,
52 
53  // Type: `qx::string_view`
54  // Usage: `{func}`
55  // __FUNCTION__ value.
56  function = 5,
57 
58  // Type: `int`
59  // Usage: `{l}`
60  // QX_LINE value.
61  line = 6,
62 
63  // Type: `qx::string_view`
64  // Usage: `{m}`
65  // Formatting result.
66  message = 7,
67 };
68 
69 enum class compile_pattern_result
70 {
71  ok,
72  unclosed_field,
73  unmatched_closing_brace,
74  unknown_specifier,
75  invalid_field,
76 };
77 
78 /**
79  @brief Compile an fmt pattern with named arguments into replacement fields
80  @details This function is required because std format doesn't support named arguments.
81  This function doesn't allocate.
82  @tparam char_t - char type (char, wchar_t, etc)
83  @tparam traits_t - char traits. \see string_traits.h
84  @param sPattern - fmt pattern that must have only arguments from logger_specifiers.
85  One type may be absent or present multiple times.
86  You can use any format specifiers.
87  The result will be in the same string.
88  @retval - Compilation result. If != ok, sPattern is considered invalid.
89 **/
90 template<class char_t, class traits_t>
91 compile_pattern_result compile_pattern(basic_string<char_t, traits_t>& sPattern) noexcept;
92 
93 /**
94  @brief Format sStorage using qx::logger_specifiers
95  @param sStorage - where to put the result
96  @param svPattern - formatting pattern, see qx::logger_specifiers
97  @param category - code category
98  @param eVerbosity - message verbosity
99  @param threadId - thread where the log is called
100  @param messageTime - message creation time
101  @param svFile - file name string
102  @param svFunction - function name string
103  @param nLine - code line number
104  @param svMessage - user message string
105 **/
106 inline void format_log_specifiers(
107  string& sStorage,
108  string_view svPattern,
109  const category& category,
110  verbosity eVerbosity,
111  std::thread::id threadId,
112  std::chrono::system_clock::time_point messageTime,
113  string_view svFile,
114  string_view svFunction,
115  int nLine,
116  string_view svMessage);
117 
118 } // namespace qx
119 
void format_log_specifiers(string &sStorage, string_view svPattern, 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)
Format sStorage using qx::logger_specifiers.
compile_pattern_result compile_pattern(basic_string< char_t, traits_t > &sPattern) noexcept
Compile an fmt pattern with named arguments into replacement fields.