Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Assert.hpp
1#pragma once
2
3#include <boost/log/core/core.hpp>
4#include <fmt/base.h>
5
6#include <functional>
7#include <string_view>
8#ifndef CLIO_WITHOUT_STACKTRACE
9#include <boost/stacktrace.hpp>
10#include <boost/stacktrace/stacktrace.hpp>
11#endif // CLIO_WITHOUT_STACKTRACE
12#include <fmt/format.h>
13
14#include <cstdlib>
15
16namespace util::impl {
17
18class OnAssert {
19public:
20 using ActionType = std::function<void(std::string_view)>;
21
22private:
23 static ActionType action;
24
25public:
26 static void
27 call(std::string_view message);
28
29 static void
30 setAction(ActionType newAction);
31
32 static void
33 resetAction();
34
35private:
36 static void
37 defaultAction(std::string_view message);
38};
39
51template <typename... Args>
52constexpr void
53assertImpl(
54 std::source_location const location,
55 char const* expression,
56 bool const condition,
57 fmt::format_string<Args...> format,
58 Args&&... args
59)
60{
61 if (!condition) {
62#ifndef CLIO_WITHOUT_STACKTRACE
63 auto const resultMessage = fmt::format(
64 "Assertion '{}' failed at {}:{}:\n{}\nStacktrace:\n{}",
65 expression,
66 location.file_name(),
67 location.line(),
68 fmt::format(format, std::forward<Args>(args)...),
69 boost::stacktrace::to_string(boost::stacktrace::stacktrace())
70 );
71#else
72 auto const resultMessage = fmt::format(
73 "Assertion '{}' failed at {}:{}:\n{}",
74 expression,
75 location.file_name(),
76 location.line(),
77 fmt::format(format, std::forward<Args>(args)...)
78 );
79#endif
80
81 OnAssert::call(resultMessage);
82 }
83}
84
85} // namespace util::impl
86
87#define ASSERT(condition, ...) \
88 util::impl::assertImpl( \
89 std::source_location::current(), #condition, static_cast<bool>(condition), __VA_ARGS__ \
90 )
Definition Assert.hpp:18