this_thread.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright Antony Polukhin, 2023-2024.
  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. #ifndef BOOST_STACKTRACE_THIS_THREAD_HPP
  7. #define BOOST_STACKTRACE_THIS_THREAD_HPP
  8. #include <boost/config.hpp>
  9. #ifdef BOOST_HAS_PRAGMA_ONCE
  10. # pragma once
  11. #endif
  12. #include <boost/stacktrace/stacktrace.hpp>
  13. namespace boost { namespace stacktrace { namespace this_thread {
  14. /// @brief Invoking the function with the enable parameter equal to `true`
  15. /// enables capturing of stacktraces by the current thread of execution at
  16. /// exception object construction if the `boost_stacktrace_from_exception`
  17. /// library is linked to the current binary; disables otherwise.
  18. ///
  19. /// Implements https://wg21.link/p2370r1
  20. inline void set_capture_stacktraces_at_throw(bool enable = true) noexcept {
  21. #if defined(__GNUC__) && defined(__ELF__)
  22. if (impl::ref_capture_stacktraces_at_throw) {
  23. impl::ref_capture_stacktraces_at_throw() = enable;
  24. }
  25. #elif defined(BOOST_MSVC)
  26. if (bool* p = boost_stacktrace_impl_ref_capture_stacktraces_at_throw()) {
  27. *p = enable;
  28. }
  29. #endif
  30. (void)enable;
  31. }
  32. /// @return whether the capturing of stacktraces by the current thread of
  33. /// execution is enabled and
  34. /// boost::stacktrace::basic_stacktrace::from_current_exception may return a
  35. /// non empty stacktrace.
  36. ///
  37. /// Returns true if set_capture_stacktraces_at_throw(false) was not called
  38. /// and the `boost_stacktrace_from_exception` is linked to the current binary.
  39. ///
  40. /// Implements https://wg21.link/p2370r1
  41. inline bool get_capture_stacktraces_at_throw() noexcept {
  42. #if defined(__GNUC__) && defined(__ELF__)
  43. if (impl::ref_capture_stacktraces_at_throw) {
  44. return impl::ref_capture_stacktraces_at_throw();
  45. }
  46. #elif defined(BOOST_MSVC)
  47. if (bool* p = boost_stacktrace_impl_ref_capture_stacktraces_at_throw()) {
  48. return *p;
  49. }
  50. #endif
  51. return false;
  52. }
  53. }}} // namespace boost::stacktrace::this_thread
  54. #endif // BOOST_STACKTRACE_THIS_THREAD_HPP