1// Copyright Antony Polukhin, 2016-2024.
2//
3// Distributed under the Boost Software License, Version 1.0. (See
4// accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#define BOOST_ENABLE_ASSERT_HANDLER
8
9#include <cstdlib> // std::exit
10#include <boost/array.hpp>
11BOOST_NOINLINE void foo(int i);
12BOOST_NOINLINE void bar(int i);
13
14BOOST_NOINLINE void bar(int i) {
15 boost::array<int, 5> a = {.elems: {101, 100, 123, 23, 32}};
16 if (i >= 0) {
17 foo(i: a[i]);
18 } else {
19 std::exit(status: 1);
20 }
21}
22
23BOOST_NOINLINE void foo(int i) {
24 bar(i: --i);
25}
26
27//[getting_started_assert_handlers
28
29// BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined for the whole project
30#include <stdexcept> // std::logic_error
31#include <iostream> // std::cerr
32#include <boost/stacktrace.hpp>
33
34namespace boost {
35 inline void assertion_failed_msg(char const* expr, char const* msg, char const* function, char const* /*file*/, long /*line*/) {
36 std::cerr << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n"
37 << "Backtrace:\n" << boost::stacktrace::stacktrace() << '\n';
38 /*<-*/ std::exit(status: 0); /*->*/
39 /*=std::abort();*/
40 }
41
42 inline void assertion_failed(char const* expr, char const* function, char const* file, long line) {
43 ::boost::assertion_failed_msg(expr, msg: 0 /*nullptr*/, function, file, line);
44 }
45} // namespace boost
46//]
47
48
49int main() {
50 foo(i: 5);
51
52 return 2;
53}
54
55
56

source code of boost/libs/stacktrace/example/assert_handler.cpp