verbose_terminate_handler.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef BOOST_CORE_VERBOSE_TERMINATE_HANDLER_HPP_INCLUDED
  2. #define BOOST_CORE_VERBOSE_TERMINATE_HANDLER_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // Copyright 2022 Peter Dimov
  8. // Distributed under the Boost Software License, Version 1.0.
  9. // https://www.boost.org/LICENSE_1_0.txt
  10. #include <boost/core/demangle.hpp>
  11. #include <boost/throw_exception.hpp>
  12. #include <boost/config.hpp>
  13. #include <exception>
  14. #include <typeinfo>
  15. #include <cstdlib>
  16. #include <cstdio>
  17. namespace boost
  18. {
  19. namespace core
  20. {
  21. BOOST_NORETURN inline void verbose_terminate_handler()
  22. {
  23. std::set_terminate( 0 );
  24. #if defined(BOOST_NO_EXCEPTIONS)
  25. std::fputs( "std::terminate called with exceptions disabled\n", stderr );
  26. #else
  27. try
  28. {
  29. throw;
  30. }
  31. catch( std::exception const& x )
  32. {
  33. #if defined(BOOST_NO_RTTI)
  34. char const * typeid_name = "unknown (RTTI is disabled)";
  35. #else
  36. char const * typeid_name = typeid( x ).name();
  37. boost::core::scoped_demangled_name typeid_demangled_name( typeid_name );
  38. if( typeid_demangled_name.get() != 0 )
  39. {
  40. typeid_name = typeid_demangled_name.get();
  41. }
  42. #endif
  43. boost::source_location loc = boost::get_throw_location( x );
  44. std::fprintf( stderr,
  45. "std::terminate called after throwing an exception:\n\n"
  46. " type: %s\n"
  47. " what(): %s\n"
  48. " location: %s:%lu:%lu in function '%s'\n",
  49. typeid_name,
  50. x.what(),
  51. loc.file_name(), static_cast<unsigned long>( loc.line() ),
  52. static_cast<unsigned long>( loc.column() ), loc.function_name()
  53. );
  54. }
  55. catch( ... )
  56. {
  57. std::fputs( "std::terminate called after throwing an unknown exception\n", stderr );
  58. }
  59. #endif
  60. std::fflush( stdout );
  61. std::abort();
  62. }
  63. } // namespace core
  64. } // namespace boost
  65. #endif // #ifndef BOOST_CORE_VERBOSE_TERMINATE_HANDLER_HPP_INCLUDED