mirror of
https://github.com/boostorg/stacktrace.git
synced 2026-01-27 19:32:09 +00:00
More examples and removed inclusion of some unused headers
This commit is contained in:
@@ -4,55 +4,118 @@
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/function.hpp>
|
||||
struct events {
|
||||
typedef boost::function<void (float)> callback_type;
|
||||
#include <boost/array.hpp>
|
||||
BOOST_NOINLINE void oops(std::size_t i);
|
||||
BOOST_NOINLINE void foo(int i);
|
||||
BOOST_NOINLINE void bar(int i);
|
||||
|
||||
static void from_keyboard(callback_type callback);
|
||||
static void from_network(callback_type callback);
|
||||
};
|
||||
BOOST_NOINLINE void oops(std::size_t i) {
|
||||
boost::array<int, 5> a = {{0, 1, 2, 3, 4}};
|
||||
foo(a[i]);
|
||||
}
|
||||
|
||||
|
||||
#include <boost/stacktrace.hpp>
|
||||
#include <iostream>
|
||||
BOOST_NOINLINE void validate_positive(float f) {
|
||||
if (f < 0.f) {
|
||||
std::cerr << "Negative number " << f << " detected. Call stack:\n"
|
||||
<< boost::stacktrace::stacktrace() << '\n';
|
||||
BOOST_NOINLINE void bar(int i) {
|
||||
boost::array<int, 5> a = {{0, 1, 2, 3, 4}};
|
||||
if (i < 5) {
|
||||
if (i >= 0) {
|
||||
foo(a[i]);
|
||||
} else {
|
||||
oops(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
boost::function<void (float)> on_keyboard;
|
||||
boost::function<void (float)> on_network;
|
||||
|
||||
BOOST_NOINLINE void keyboard_event() {
|
||||
on_keyboard(-0.1f);
|
||||
BOOST_NOINLINE void foo(int i) {
|
||||
bar(--i);
|
||||
}
|
||||
|
||||
BOOST_NOINLINE void network_event() {
|
||||
on_network(1.0f);
|
||||
/*
|
||||
../../../boost/array.hpp:123: T& boost::array<T, N>::operator[](boost::array<T, N>::size_type) [with T = int; long unsigned int N = 5ul; boost::array<T, N>::reference = int&; boost::array<T, N>::size_type = long unsigned int]: Assertion `(i < N)&&("out of range")' failed.
|
||||
Aborted (core dumped)
|
||||
*/
|
||||
|
||||
/*
|
||||
Expression 'i < N' is false in function 'T& boost::array<T, N>::operator[](boost::array<T, N>::size_type) [with T = int; long unsigned int N = 5ul; boost::array<T, N>::reference = int&; boost::array<T, N>::size_type = long unsigned int]': out of range
|
||||
Backtrace:
|
||||
0# boost::assertion_failed_msg(char const*, char const*, char const*, char const*, long)
|
||||
1# boost::array<int, 5ul>::operator[](unsigned long)
|
||||
2# oops(unsigned long)
|
||||
3# bar(int)
|
||||
4# foo(int)
|
||||
5# bar(int)
|
||||
6# foo(int)
|
||||
7# bar(int)
|
||||
8# foo(int)
|
||||
9# bar(int)
|
||||
10# foo(int)
|
||||
11# bar(int)
|
||||
12# foo(int)
|
||||
13# bar(int)
|
||||
14# foo(int)
|
||||
15# main
|
||||
16# __libc_start_main
|
||||
17# _start
|
||||
18# ??
|
||||
*/
|
||||
|
||||
// BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined for the whole project
|
||||
#include <boost/assert.hpp>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <boost/stacktrace.hpp>
|
||||
|
||||
namespace boost {
|
||||
void assertion_failed_msg(char const* expr, char const* msg, char const* function, char const* file, long line) {
|
||||
std::cerr << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n"
|
||||
<< "Backtrace:\n" << boost::stacktrace::stacktrace() << '\n';
|
||||
throw std::logic_error("assertion");
|
||||
}
|
||||
|
||||
void assertion_failed(char const* expr, char const* function, char const* file, long line) {
|
||||
::boost::assertion_failed_msg(expr, 0 /*nullptr*/, function, file, line);
|
||||
}
|
||||
} // namespace boost
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <exception> // std::set_terminate
|
||||
#include <signal.h> // ::signal
|
||||
#include <boost/stacktrace.hpp>
|
||||
#include <iostream>
|
||||
|
||||
void my_terminate_handler() {
|
||||
std::cerr << "Terminate called: " << boost::stacktrace::stacktrace() << '\n';
|
||||
std::abort();
|
||||
}
|
||||
|
||||
void events::from_keyboard(events::callback_type callback) {
|
||||
on_keyboard = callback;
|
||||
}
|
||||
|
||||
void events::from_network(events::callback_type callback) {
|
||||
on_network = callback;
|
||||
void my_signal_handler(int signum) {
|
||||
std::cerr << "Signal " << signum << ", backtrace:\n" << boost::stacktrace::stacktrace() << '\n';
|
||||
std::abort();
|
||||
}
|
||||
|
||||
|
||||
void setup_handlers() {
|
||||
std::set_terminate(&my_terminate_handler);
|
||||
::signal(SIGSEGV, &my_signal_handler);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <boost/exception/error_info.hpp>
|
||||
typedef boost::error_info<struct tag_stacktrace, boost::stacktrace::stacktrace> stacktrace_info;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <boost/core/no_exceptions_support.hpp>
|
||||
int main() {
|
||||
events::from_keyboard(&validate_positive);
|
||||
events::from_network(&validate_positive);
|
||||
setup_handlers();
|
||||
|
||||
keyboard_event();
|
||||
network_event();
|
||||
BOOST_TRY {
|
||||
foo(5); // testing assert handler
|
||||
} BOOST_CATCH(...) {
|
||||
} BOOST_CATCH_END
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user