assert_handler.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright Antony Polukhin, 2016-2019.
  2. //
  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)
  6. #define BOOST_ENABLE_ASSERT_HANDLER
  7. #include <cstdlib> // std::exit
  8. #include <boost/array.hpp>
  9. BOOST_NOINLINE void foo(int i);
  10. BOOST_NOINLINE void bar(int i);
  11. BOOST_NOINLINE void bar(int i) {
  12. boost::array<int, 5> a = {{101, 100, 123, 23, 32}};
  13. if (i >= 0) {
  14. foo(a[i]);
  15. } else {
  16. std::exit(1);
  17. }
  18. }
  19. BOOST_NOINLINE void foo(int i) {
  20. bar(--i);
  21. }
  22. namespace std { inline void ignore_abort(){ std::exit(0); } }
  23. #define abort ignore_abort
  24. //[getting_started_assert_handlers
  25. // BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined for the whole project
  26. #include <stdexcept> // std::logic_error
  27. #include <iostream> // std::cerr
  28. #include <boost/stacktrace.hpp>
  29. namespace boost {
  30. inline void assertion_failed_msg(char const* expr, char const* msg, char const* function, char const* /*file*/, long /*line*/) {
  31. std::cerr << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n"
  32. << "Backtrace:\n" << boost::stacktrace::stacktrace() << '\n';
  33. std::abort();
  34. }
  35. inline void assertion_failed(char const* expr, char const* function, char const* file, long line) {
  36. ::boost::assertion_failed_msg(expr, 0 /*nullptr*/, function, file, line);
  37. }
  38. } // namespace boost
  39. //]
  40. int main() {
  41. foo(5);
  42. return 2;
  43. }