qxLib
error_context.h
Go to the documentation of this file.
1 /**
2 
3  @file error_context.h
4  @author Khrapov
5  @date 10.02.2026
6  @copyright © Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 #pragma once
10 
12 #include <qx/logger/logger.h>
13 
14 namespace qx
15 {
16 
17 /**
18 
19  @class error_context
20  @brief Collects all log messages in the scope of its lifetime and outputs them when any assertion is triggered.
21  Particularly useful in multithreaded context where many threads write simultaneously,
22  allowing seeing a clear thread output.
23  @details Requires qx::error_context_stream to be added to the logger to work.
24  Does not display anything if there were no log messages.
25  Otherwise, it displays the header, all messages, and then the footer for visual differentiation.
26  Doesn't catch constexpr verbosity erased messages.
27  @warning Does not support fibres and coroutines (thread switching)
28  @author Khrapov
29  @date 10.02.2026
30 
31 **/
33 {
34  QX_NONCOPYMOVABLE(error_context);
35 
36 public:
37  /**
38  @brief error_context object constructor
39  @param eMinCaptureVerbosity - minimum verbosity of messages this context should capture
40  @param svHeader - a header to add when there is at least one message
41  @param svFooter - a footer to add when there is at least one message
42  **/
44  verbosity eMinCaptureVerbosity,
45  string_view svHeader = QXT("Error context start"),
46  string_view svFooter = QXT("Error context end")) noexcept;
47 
48  ~error_context() noexcept;
49 
50 private:
51  /**
52  @brief Message capturing event
53  @param eVerbosity - message verbosity
54  @param svMessage - message text
55  **/
56  void on_message(verbosity eVerbosity, string_view svMessage) noexcept;
57 
58  /**
59  @brief Error occured event
60  @param errorThreadId - the thread where error occured
61  **/
62  void on_error(std::thread::id errorThreadId) noexcept;
63 
64 private:
65  const string_view m_svHeader;
66  const string_view m_svFooter;
67  const verbosity m_eMinCatchVerbosity;
68  const std::thread::id m_ThreadId = std::this_thread::get_id();
69  string m_sMessages;
70  delegate_token_type m_OnMessagesToken;
71  delegate_token_type m_OnErrorToken;
72 };
73 
74 } // namespace qx
75 
Collects all log messages in the scope of its lifetime and outputs them when any assertion is trigger...
Definition: error_context.h:33
error_context(verbosity eMinCaptureVerbosity, string_view svHeader=QXT("Error context start"), string_view svFooter=QXT("Error context end")) noexcept
error_context object constructor
A class that can be used as a key in ordered containers so that items are ordered in descending order...
Definition: priority.h:47