exception_handler_feature.hpp 7.3 KB

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