qxLib
base_logger_stream.inl
Go to the documentation of this file.
1 /**
2 
3  @file base_logger_stream.inl
4  @author Khrapov
5  @date 30.07.2021
6  @copyright (c) Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 
10 QX_SET_FILE_CATEGORY(CatLogger);
11 
12 namespace qx
13 {
14 
15 namespace details
16 {
17 
18 template<class char_t /* = char */>
19 struct get_cerr
20 {
21  static auto& get() noexcept
22  {
23  return std::cerr;
24  }
25 };
26 
27 template<>
28 struct get_cerr</* class char_t = */ wchar_t>
29 {
30  static auto& get() noexcept
31  {
32  return std::wcerr;
33  }
34 };
35 
36 template<class char_t /* = char */>
37 struct get_cout
38 {
39  static auto& get() noexcept
40  {
41  return std::cout;
42  }
43 };
44 
45 template<>
46 struct get_cout</* class char_t = */ wchar_t>
47 {
48  static auto& get() noexcept
49  {
50  return std::wcout;
51  }
52 };
53 
54 } // namespace details
55 
56 inline base_logger_stream::base_logger_stream(const config& streamConfig) noexcept
57  : m_pMutex(std::make_unique<std::recursive_mutex>())
58  , m_eMinFlushVerbosity(streamConfig.eMinFlushVerbosity)
59  , m_bProtectLog(streamConfig.bProtectLog)
60 {
61 }
62 
63 inline compile_pattern_result base_logger_stream::set_pattern(pattern_string_t sPattern) noexcept
64 {
65  const compile_pattern_result eResult = compile_pattern(sPattern);
66 
67  if (eResult == compile_pattern_result::ok)
68  m_optPattern = std::move(sPattern);
69 
70  return eResult;
71 }
72 
73 inline std::optional<string_view> base_logger_stream::get_pattern() const noexcept
74 {
75  return m_optPattern ? std::optional<string_view>(*m_optPattern) : std::nullopt;
76 }
77 
79  const category& category,
80  verbosity eVerbosity,
81  std::thread::id threadId,
82  std::chrono::system_clock::time_point messageTime,
83  string_view svFile,
84  string_view svFunction,
85  int nLine,
86  string_view svMessage)
87 {
88  QX_PERF_SCOPE("Log");
89 
90  if (m_bProtectLog)
91  m_pMutex->lock();
92 
93  do_log(category, eVerbosity, threadId, messageTime, svFile, svFunction, nLine, svMessage);
94 
95  if (eVerbosity >= m_eMinFlushVerbosity)
96  flush();
97 
98  if (m_bProtectLog)
99  m_pMutex->unlock();
100 }
101 
103 {
104  QX_PERF_SCOPE("Flush");
105 
106  if (m_bProtectLog)
107  m_pMutex->lock();
108 
109  do_flush();
110 
111  if (m_bProtectLog)
112  m_pMutex->unlock();
113 }
114 
116  const category& category,
117  verbosity eVerbosity,
118  std::thread::id threadId,
119  std::chrono::system_clock::time_point messageTime,
120  string_view svFile,
121  string_view svFunction,
122  int nLine) const noexcept
123 {
124  return false;
125 }
126 
127 } // namespace qx
#define QX_SET_FILE_CATEGORY(_category)
Set the file category You can access this value via QX_GET_FILE_CATEGORY() This category will not be ...
Definition: category.h:32
virtual bool log_unconditionally_required(const category &category, verbosity eVerbosity, std::thread::id threadId, std::chrono::system_clock::time_point messageTime, string_view svFile, string_view svFunction, int nLine) const noexcept
Returns true if this message should be processed by this stream even if the logger filters did not pa...
std::optional< string_view > get_pattern() const noexcept
Get a message pattern if present.
compile_pattern_result set_pattern(pattern_string_t sPattern) noexcept
Set a message pattern that will be used for this stream.
void log(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)
Output to stream.
void flush()
Flush the stream.
base_logger_stream(const config &streamConfig) noexcept
base_logger_stream object constructor
String class.
Definition: string.h:83
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:62
compile_pattern_result compile_pattern(basic_string< char_t, traits_t > &sPattern) noexcept
Compile an fmt pattern with named arguments into replacement fields.