exception_handler_feature.hpp 7.3 KB

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