Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Assert.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/SourceLocation.hpp"
23#include "util/log/Logger.hpp"
24
25#include <boost/log/core/core.hpp>
26#ifndef CLIO_WITHOUT_STACKTRACE
27#include <boost/stacktrace.hpp>
28#include <boost/stacktrace/stacktrace.hpp>
29#endif // CLIO_WITHOUT_STACKTRACE
30#include <fmt/core.h>
31#include <fmt/format.h>
32
33#include <cstdlib>
34#include <iostream>
35
36namespace util {
37
49template <typename... Args>
50constexpr void
52 SourceLocationType const location,
53 char const* expression,
54 bool const condition,
55 fmt::format_string<Args...> format,
56 Args&&... args
57)
58{
59 if (!condition) {
60#ifndef CLIO_WITHOUT_STACKTRACE
61 auto const resultMessage = fmt::format(
62 "Assertion '{}' failed at {}:{}:\n{}\nStacktrace:\n{}",
63 expression,
64 location.file_name(),
65 location.line(),
66 fmt::format(format, std::forward<Args>(args)...),
67 boost::stacktrace::to_string(boost::stacktrace::stacktrace())
68 );
69#else
70 auto const resultMessage = fmt::format(
71 "Assertion '{}' failed at {}:{}:\n{}",
72 expression,
73 location.file_name(),
74 location.line(),
75 fmt::format(format, std::forward<Args>(args)...)
76 );
77#endif
78 if (boost::log::core::get()->get_logging_enabled()) {
79 LOG(LogService::fatal()) << resultMessage;
80 } else {
81 std::cerr << resultMessage;
82 }
83 std::exit(EXIT_FAILURE); // std::abort does not flush gcovr output and causes uncovered lines
84 }
85}
86
87} // namespace util
88
89#define ASSERT(condition, ...) \
90 util::assertImpl(CURRENT_SRC_LOCATION, #condition, static_cast<bool>(condition), __VA_ARGS__)
static Logger::Pump fatal(SourceLocationType const &loc=CURRENT_SRC_LOCATION)
Globally accesible General logger at Severity::FTL severity.
Definition Logger.hpp:352
A class representing the source location of the current code.
Definition SourceLocation.hpp:52
constexpr std::string_view file_name() const
Get the file name.
Definition SourceLocation.hpp:73
constexpr std::size_t line() const
Get the line number.
Definition SourceLocation.hpp:84
This namespace contains various utilities.
Definition AccountUtils.hpp:30
constexpr void assertImpl(SourceLocationType const location, char const *expression, bool const condition, fmt::format_string< Args... > format, Args &&... args)
Assert that a condition is true.
Definition Assert.hpp:51