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