qxLib
asserts_manager.inl
Go to the documentation of this file.
1 /**
2 
3  @file asserts_manager.inl
4  @author Khrapov
5  @date 27.12.2025
6  @copyright © Nick Khrapov, 2025. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
13 inline bool is_debugger_present() noexcept
14 {
15 #if QX_WIN
16  return ::IsDebuggerPresent() != 0;
17 #elif QX_LINUX
18  std::FILE* pStatus = std::fopen("/proc/thread-self/status", "r");
19  if (!pStatus)
20  return false;
21 
22  char buffer[512];
23  int nTracerPid = 0;
24  while (std::fgets(buffer, sizeof(buffer), pStatus))
25  {
26  if (std::sscanf(buffer, "TracerPid: %d", &nTracerPid) == 1)
27  break;
28  }
29  std::fclose(pStatus);
30  return nTracerPid != 0;
31 #elif QX_MACOS && (QX_WITH_DEBUG_INFO)
32  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
33  kinfo_proc info {};
34  size_t nSize = sizeof(info);
35  return sysctl(mib, 4, &info, &nSize, nullptr, 0) == 0 && (info.kp_proc.p_flag & P_TRACED) != 0;
36 #else
37  return false;
38 #endif
39 }
40 
42  string_view svCondition,
43  const category& category,
44  assert_type eAssertType,
45  string sUserMessage,
46  string_view svFunction,
47  string_view svFile,
48  i32 nLine)
49 {
50  config currentConfig;
51  {
52  std::shared_lock lock(m_Mutex);
53  for (const interceptor_entry& interceptor : m_Interceptors)
54  if (interceptor.callback(svCondition, category, eAssertType, sUserMessage, svFunction, svFile, nLine))
55  return false;
56 
57  currentConfig = m_Config;
58  }
59 
60  if (currentConfig.bLogAssertion)
61  {
62  QX_LOG_REF(category, currentConfig.getVerbosity(eAssertType), "[{}] {}", svCondition, sUserMessage);
63 
64  // the error must present when debug break is called
65  get_logger().flush();
66 
67  std::shared_lock _(get_logger().get_streams_mutex());
68  if (auto* pStream = get_logger().get_stream<error_context_stream>())
69  pStream->on_error(std::this_thread::get_id());
70  }
71 
72  if (currentConfig.onAssertion)
73  currentConfig.onAssertion(svCondition, category, eAssertType, sUserMessage, svFunction, svFile, nLine);
74 
75  return is_debugger_present();
76 }
77 
78 inline void asserts_manager::exit(const category& category, assert_type eAssertType)
79 {
80  const config currentConfig = get_config();
81  if (currentConfig.onExit)
82  currentConfig.onExit(category, eAssertType);
83 }
84 
86 {
87  std::shared_lock lock(m_Mutex);
88  return m_Config;
89 }
90 
92 {
93  std::unique_lock lock(m_Mutex);
94  m_Config = std::move(config);
95 }
96 
97 inline void asserts_manager::add_interceptor(const void* pOwner, interceptor_func interceptor)
98 {
99  std::unique_lock lock(m_Mutex);
100  m_Interceptors.push_back({ pOwner, std::move(interceptor) });
101 }
102 
103 inline void asserts_manager::remove_interceptor(const void* pOwner)
104 {
105  std::unique_lock lock(m_Mutex);
106  std::erase_if(
107  m_Interceptors,
108  [pOwner](const interceptor_entry& entry)
109  {
110  return entry.pOwner == pOwner;
111  });
112 }
113 
114 } // namespace qx
bool is_debugger_present() noexcept
Check whether the current process or thread is being traced by a debugger.
void set_config(config config)
Set config.
void exit(const category &category, assert_type eAssertType)
Called in fatal assertions to exit the application.
void add_interceptor(const void *pOwner, interceptor_func interceptor)
Register an assertion interceptor.
void remove_interceptor(const void *pOwner)
Remove an interceptor, waiting for callbacks already in progress.
bool do_assert(string_view svCondition, const category &category, assert_type eAssertType, string sUserMessage, string_view svFunction, string_view svFile, i32 nLine)
An entry point for all assertions.
config get_config() const
Get config.
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:63
virtual void flush()
Flush all streams.
Definition: logger.inl:204
logger & get_logger() noexcept
Get the logger instance used in QX_LOG macros.
Definition: logger.h:293
#define QX_LOG_REF(category, eVerbosity,...)
Log with category and with non compile time category check.
Definition: logger.h:46
std::int32_t i32
− 9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807
Definition: typedefs.h:32