]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/stacktrace/example/throwing_st.cpp
1 // Copyright Antony Polukhin, 2016-2019.
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)
7 #include <boost/config.hpp>
9 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11 //[getting_started_class_traced
12 #include <boost/stacktrace.hpp>
13 #include <boost/exception/all.hpp>
15 typedef boost::error_info
<struct tag_stacktrace
, boost::stacktrace::stacktrace
> traced
;
18 //[getting_started_class_with_trace
20 void throw_with_trace(const E
& e
) {
21 throw boost::enable_error_info(e
)
22 << traced(boost::stacktrace::stacktrace());
26 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 BOOST_NOINLINE
void oops(int i
);
29 BOOST_NOINLINE
void foo(int i
);
30 BOOST_NOINLINE
void bar(int i
);
33 BOOST_NOINLINE
void oops(int i
) {
34 //[getting_started_throwing_with_trace
36 throw_with_trace(std::out_of_range("'i' must be less than 4 in oops()"));
38 throw_with_trace(std::logic_error("'i' must be greater than zero in oops()"));
44 #include <boost/array.hpp>
45 BOOST_NOINLINE
void bar(int i
) {
46 boost::array
<int, 5> a
= {{0, 0, 0, 0, 0}};
57 BOOST_NOINLINE
void foo(int i
) {
64 //[getting_started_catching_trace
66 foo(5); // testing assert handler
67 } catch (const std::exception
& e
) {
68 std::cerr
<< e
.what() << '\n';
69 const boost::stacktrace::stacktrace
* st
= boost::get_error_info
<traced
>(e
);
71 std::cerr
<< *st
<< '\n'; /*<-*/ return 0; /*->*/
72 } /*<-*/ return 3; /*->*/