exception_handler_feature.hpp 7.3 KB

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