userver: userver/utils/assert.hpp Source File
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <string_view>
8
9#include <userver/utils/impl/source_location.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace utils {
14
15namespace impl {
16
17// NOLINTNEXTLINE(readability-identifier-naming)
18[[noreturn]] void UASSERT_failed(
19 std::string_view expr,
20 const char* file,
21 unsigned int line,
22 const char* function,
23 std::string_view msg
24) noexcept;
25
26[[noreturn]] void LogAndThrowInvariantError(
27 std::string_view condition,
28 std::string_view message,
29 utils::impl::SourceLocation source_location
30);
31
32#ifdef NDEBUG
33inline constexpr bool kEnableAssert = false;
34#else
35inline constexpr bool kEnableAssert = true;
36#endif
37
38extern bool dump_stacktrace_on_assert_failure;
39
40} // namespace impl
41
45[[noreturn]] void AbortWithStacktrace(std::string_view message) noexcept;
46
47} // namespace utils
48
49USERVER_NAMESPACE_END
50
55// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
56#define UASSERT_MSG(expr, msg) \
57 do { \
58 if (USERVER_NAMESPACE::utils::impl::kEnableAssert) { \
59 if (expr) { \
60 } else { \
61 USERVER_NAMESPACE::utils::impl::UASSERT_failed(#expr, __FILE__, __LINE__, __func__, msg); \
62 } \
63 } \
64 } while (0)
65
70// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
71#define UASSERT(expr) UASSERT_MSG(expr, std::string_view{})
72
76// NOLINTNEXTLINE (cppcoreguidelines-macro-usage)
77#define UINVARIANT(condition, message) \
78 do { \
79 if (condition) { \
80 } else { \
81 if constexpr (USERVER_NAMESPACE::utils::impl::kEnableAssert) { \
82 USERVER_NAMESPACE::utils::impl::UASSERT_failed(#condition, __FILE__, __LINE__, __func__, message); \
83 } else { \
84 USERVER_NAMESPACE::utils::impl::LogAndThrowInvariantError( \
85 #condition, \
86 message, \
87 USERVER_NAMESPACE::utils::impl::SourceLocation::Current() \
88 ); \
89 } \
90 } \
91 } while (0)