Getting backtrace – current call sequence
When reporting errors or failures, it is more important to report the steps that lead to the error rather than the error itself. Consider the naive trading simulator:
int main() { int money = 1000; start_trading(money); }
All it reports is a line:
Sorry, you're bankrupt!
That's a no go. We want to know how did it happened, what were the steps that led to bankruptcy!
Okay. Let's fix the following function and make it report the steps that led to bankruptcy:
void report_bankruptcy() { std::cout << "Sorry, you're bankrupt!\n"; std::exit(0); }
Getting started
You will need a Boost 1.65 or newer for this recipe. Basic knowledge of C++ is also a requirement.
How to do it...
For this recipe, we will need only to construct a single class and output it:
#include <iostream> #include <boost/stacktrace.hpp> void report_bankruptcy() { std::cout << "Sorry, you're bankrupt!\n"; std::cout << "Here's how it happened:\n...