Added stack trace logging for exceptions.

This commit is contained in:
ravinsp
2019-12-15 23:28:49 +05:30
parent 7a7aa6d5b3
commit fc108523ca
4 changed files with 63 additions and 9 deletions

View File

@@ -75,15 +75,32 @@ void signal_handler(int signum)
exit(signum);
}
namespace boost
{
/**
* Global exception handler for boost exceptions.
*/
void boost::throw_exception(std::exception const &e)
void throw_exception(std::exception const &e)
{
std::cerr << "Boost error:" << e.what() << "\n";
LOG_ERR << "Boost error: " << e.what() << "\n"
<< boost::stacktrace::stacktrace();
exit(1);
}
inline void assertion_failed_msg(char const *expr, char const *msg, char const *function, char const * /*file*/, long /*line*/)
{
LOG_ERR << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n"
<< "Backtrace:\n"
<< boost::stacktrace::stacktrace() << '\n';
std::abort();
}
inline 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
/**
* Global exception handler for std exceptions.
*/
@@ -102,14 +119,18 @@ void std_terminate() noexcept
}
catch (...)
{
std::cerr << "std error: Terminated due to unknown exception" << "\n";
LOG_ERR << "std error: Terminated due to unknown exception"
<< "\n";
}
}
else
{
std::cerr << "std error: Terminated due to unknown reason" << "\n";
LOG_ERR << "std error: Terminated due to unknown reason"
<< "\n";
}
LOG_ERR << boost::stacktrace::stacktrace();
exit(1);
}

View File

@@ -3,6 +3,10 @@
// This will direct all boost exceptions to our error handler.
#define BOOST_NO_EXCEPTIONS
// Enable boost strack trace.
#define BOOST_STACKTRACE_USE_BACKTRACE
// Enable custom handlers for boost assertion failures.
#define BOOST_ENABLE_ASSERT_DEBUG_HANDLER
#include <bitset>
#include <boost/algorithm/string.hpp>
@@ -25,6 +29,7 @@
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/stacktrace.hpp>
#include <boost/thread/thread.hpp>
#include <chrono>
#include <cstdarg>

View File

@@ -1354,4 +1354,30 @@ int main(int argc, char *argv[])
}
return fusefs::start(argv[0], argv[1], argv[2]);
}
}
namespace boost
{
/**
* Global exception handler for boost exceptions.
*/
void throw_exception(std::exception const &e)
{
std::cerr << "Boost error: " << e.what() << "\n"
<< boost::stacktrace::stacktrace();
exit(1);
}
inline 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';
std::abort();
}
inline 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