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