qxLib
error_context_stream.h
Go to the documentation of this file.
1 /**
2 
3  @file error_context_stream.h
4  @author Khrapov
5  @date 10.02.2026
6  @copyright © Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/algo/contains.h>
14 #include <qx/memory/unique_ref.h>
15 #include <qx/patterns/delegate.h>
16 
17 #include <unordered_map>
18 
19 namespace qx
20 {
21 
22 /**
23 
24  @class error_context_stream
25  @brief A stream that allows capturing all messages and redirecting them to error contexts
26  @author Khrapov
27  @date 10.02.2026
28 
29 **/
31 {
33  QX_NONCOPYABLE(error_context_stream);
34  QX_MOVABLE(error_context_stream);
35 
36 public:
37  using on_message_delegate_t = delegate<void(verbosity, string_view)>;
38  using on_message_delegates_map_t = std::unordered_map<std::thread::id, on_message_delegate_t>;
39  using on_error_delegate = delegate<void(std::thread::id)>;
40 
41 public:
42  error_context_stream() noexcept;
43 
44  /**
45  @brief Get on messages delegate
46  @retval - on messages delegate
47  **/
49 
50  /**
51  @brief Get on error delegate
52  @retval - on error delegate
53  **/
55 
56  /**
57  @brief Emit error event for all listeners
58  @param errorThreadId - the thread where error occured
59  **/
60  void on_error(std::thread::id errorThreadId) noexcept;
61 
62 private:
63  // base_logger_stream
64  //
65  virtual bool log_unconditionally_required(
66  const category& category,
67  verbosity eVerbosity,
68  std::thread::id threadId,
69  std::chrono::system_clock::time_point messageTime,
70  string_view svFile,
71  string_view svFunction,
72  int nLine) const noexcept override;
73  void do_log(
74  const category& category,
75  verbosity eVerbosity,
76  std::thread::id threadId,
77  std::chrono::system_clock::time_point messageTime,
78  string_view svFile,
79  string_view svFunction,
80  int nLine,
81  string_view svMessage) override;
82  void do_flush() override;
83 
84 private:
85  struct error_context_stream_data
86  {
90  };
92 };
93 
94 } // namespace qx
95 
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:59
Single or multicast delegate.
Definition: delegate.h:66
A stream that allows capturing all messages and redirecting them to error contexts.
threads_shared< on_message_delegates_map_t > & get_on_messages() noexcept
Get on messages delegate.
void on_error(std::thread::id errorThreadId) noexcept
Emit error event for all listeners.
threads_shared< on_error_delegate, std::recursive_mutex > & get_on_error() noexcept
Get on error delegate.