exception_handler_feature.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file exception_handler_feature.hpp
  9. * \author Andrey Semashev
  10. * \date 17.07.2009
  11. *
  12. * The header contains implementation of an exception handler support feature.
  13. */
  14. #ifndef BOOST_LOG_SOURCES_EXCEPTION_HANDLER_FEATURE_HPP_INCLUDED_
  15. #define BOOST_LOG_SOURCES_EXCEPTION_HANDLER_FEATURE_HPP_INCLUDED_
  16. #include <boost/mpl/if.hpp>
  17. #include <boost/move/core.hpp>
  18. #include <boost/move/utility_core.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <boost/type_traits/is_nothrow_move_constructible.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/detail/light_function.hpp>
  23. #include <boost/log/detail/locks.hpp>
  24. #include <boost/log/core/record.hpp>
  25. #include <boost/log/sources/threading_models.hpp>
  26. #include <boost/log/utility/strictest_lock.hpp>
  27. #if !defined(BOOST_LOG_NO_THREADS)
  28. #include <boost/thread/exceptions.hpp>
  29. #endif
  30. #include <boost/log/detail/header.hpp>
  31. #ifdef BOOST_HAS_PRAGMA_ONCE
  32. #pragma once
  33. #endif
  34. namespace boost {
  35. BOOST_LOG_OPEN_NAMESPACE
  36. namespace sources {
  37. /*!
  38. * \brief Exception handler feature implementation
  39. */
  40. template< typename BaseT >
  41. class basic_exception_handler_logger :
  42. public BaseT
  43. {
  44. //! Base type
  45. typedef BaseT base_type;
  46. typedef basic_exception_handler_logger this_type;
  47. BOOST_COPYABLE_AND_MOVABLE_ALT(this_type)
  48. public:
  49. //! Threading model being used
  50. typedef typename base_type::threading_model threading_model;
  51. //! Final logger type
  52. typedef typename base_type::final_type final_type;
  53. //! Exception handler function type
  54. typedef boost::log::aux::light_function< void () > exception_handler_type;
  55. #if defined(BOOST_LOG_DOXYGEN_PASS)
  56. //! Lock requirement for the open_record_unlocked method
  57. typedef typename strictest_lock<
  58. typename base_type::open_record_lock,
  59. no_lock< threading_model >
  60. >::type open_record_lock;
  61. //! Lock requirement for the push_record_unlocked method
  62. typedef typename strictest_lock<
  63. typename base_type::push_record_lock,
  64. no_lock< threading_model >
  65. >::type push_record_lock;
  66. #endif // defined(BOOST_LOG_DOXYGEN_PASS)
  67. //! Lock requirement for the swap_unlocked method
  68. typedef typename strictest_lock<
  69. typename base_type::swap_lock,
  70. #ifndef BOOST_LOG_NO_THREADS
  71. boost::log::aux::multiple_unique_lock2< threading_model, threading_model >
  72. #else
  73. no_lock< threading_model >
  74. #endif // !defined(BOOST_LOG_NO_THREADS)
  75. >::type swap_lock;
  76. private:
  77. //! Exception handler
  78. exception_handler_type m_ExceptionHandler;
  79. public:
  80. /*!
  81. * Default constructor. The constructed logger does not have an exception handler.
  82. */
  83. basic_exception_handler_logger() : base_type()
  84. {
  85. }
  86. /*!
  87. * Copy constructor
  88. */
  89. basic_exception_handler_logger(basic_exception_handler_logger const& that) :
  90. base_type(static_cast< base_type const& >(that)),
  91. m_ExceptionHandler(that.m_ExceptionHandler)
  92. {
  93. }
  94. /*!
  95. * Move constructor
  96. */
  97. basic_exception_handler_logger(BOOST_RV_REF(basic_exception_handler_logger) that) BOOST_NOEXCEPT_IF(boost::is_nothrow_move_constructible< base_type >::value && boost::is_nothrow_move_constructible< exception_handler_type >::value) :
  98. base_type(boost::move(static_cast< base_type& >(that))),
  99. m_ExceptionHandler(boost::move(that.m_ExceptionHandler))
  100. {
  101. }
  102. /*!
  103. * Constructor with arguments. Passes arguments to other features.
  104. */
  105. template< typename ArgsT >
  106. explicit basic_exception_handler_logger(ArgsT const& args) :
  107. base_type(args)
  108. {
  109. }
  110. /*!
  111. * The method sets exception handler function. The function will be called with no arguments
  112. * in case if an exception occurs during either \c open_record or \c push_record method
  113. * execution. Since exception handler is called from a \c catch statement, the exception
  114. * can be rethrown in order to determine its type.
  115. *
  116. * By default no handler is installed, thus any exception is propagated as usual.
  117. *
  118. * \sa <tt>utility/exception_handler.hpp</tt>
  119. * \param handler Exception handling function
  120. *
  121. * \note The exception handler can be invoked in several threads concurrently.
  122. *
  123. * \note Thread interruptions are not affected by exception handlers.
  124. */
  125. template< typename HandlerT >
  126. void set_exception_handler(HandlerT const& handler)
  127. {
  128. #ifndef BOOST_LOG_NO_THREADS
  129. boost::log::aux::exclusive_lock_guard< threading_model > lock(this->get_threading_model());
  130. #endif
  131. m_ExceptionHandler = handler;
  132. }
  133. protected:
  134. /*!
  135. * Unlocked \c open_record
  136. */
  137. template< typename ArgsT >
  138. record open_record_unlocked(ArgsT const& args)
  139. {
  140. try
  141. {
  142. return base_type::open_record_unlocked(args);
  143. }
  144. #ifndef BOOST_LOG_NO_THREADS
  145. catch (thread_interrupted&)
  146. {
  147. throw;
  148. }
  149. #endif
  150. catch (...)
  151. {
  152. handle_exception();
  153. return record();
  154. }
  155. }
  156. /*!
  157. * Unlocked \c push_record
  158. */
  159. void push_record_unlocked(BOOST_RV_REF(record) rec)
  160. {
  161. try
  162. {
  163. base_type::push_record_unlocked(boost::move(rec));
  164. }
  165. #ifndef BOOST_LOG_NO_THREADS
  166. catch (thread_interrupted&)
  167. {
  168. throw;
  169. }
  170. #endif
  171. catch (...)
  172. {
  173. handle_exception();
  174. }
  175. }
  176. /*!
  177. * Unlocked swap
  178. */
  179. void swap_unlocked(basic_exception_handler_logger& that)
  180. {
  181. base_type::swap_unlocked(static_cast< base_type& >(that));
  182. m_ExceptionHandler.swap(that.m_ExceptionHandler);
  183. }
  184. private:
  185. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  186. //! The function handles the intercepted exception
  187. void handle_exception()
  188. {
  189. #ifndef BOOST_LOG_NO_THREADS
  190. // Here's the trick with the lock type. Since the lock
  191. // is only needed when an exception is caught, we indicate
  192. // no locking requirements in the push_record_lock type.
  193. // However, if other features don't require locking either,
  194. // we shall acquire a read lock here, when an exception is caught.
  195. // If other features do require locking, the thread model is
  196. // already locked by now, and we don't do locking at all.
  197. typedef typename mpl::if_<
  198. is_same< no_lock< threading_model >, typename final_type::push_record_lock >,
  199. boost::log::aux::shared_lock_guard< threading_model >,
  200. no_lock< threading_model >
  201. >::type lock_type;
  202. lock_type lock(base_type::get_threading_model());
  203. #endif // !defined(BOOST_LOG_NO_THREADS)
  204. if (m_ExceptionHandler.empty())
  205. throw;
  206. m_ExceptionHandler();
  207. }
  208. #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
  209. };
  210. /*!
  211. * \brief Exception handler support feature
  212. *
  213. * The logger with this feature will provide an additional method to
  214. * install an exception handler functional object. This functional
  215. * object will be called if during either opening or pushing a record
  216. * an exception is thrown from the logging core.
  217. */
  218. struct exception_handler
  219. {
  220. template< typename BaseT >
  221. struct apply
  222. {
  223. typedef basic_exception_handler_logger< BaseT > type;
  224. };
  225. };
  226. } // namespace sources
  227. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  228. } // namespace boost
  229. #include <boost/log/detail/footer.hpp>
  230. #endif // BOOST_LOG_SOURCES_EXCEPTION_HANDLER_FEATURE_HPP_INCLUDED_