sync_frontend.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 sync_frontend.hpp
  9. * \author Andrey Semashev
  10. * \date 14.07.2009
  11. *
  12. * The header contains implementation of synchronous sink frontend.
  13. */
  14. #ifndef BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_
  15. #define BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #ifdef BOOST_HAS_PRAGMA_ONCE
  18. #pragma once
  19. #endif
  20. #if defined(BOOST_LOG_NO_THREADS)
  21. #error Boost.Log: Synchronous sink frontend is only supported in multithreaded environment
  22. #endif
  23. #include <boost/static_assert.hpp>
  24. #include <boost/smart_ptr/shared_ptr.hpp>
  25. #include <boost/smart_ptr/make_shared_object.hpp>
  26. #include <boost/preprocessor/control/if.hpp>
  27. #include <boost/preprocessor/comparison/equal.hpp>
  28. #include <boost/thread/recursive_mutex.hpp>
  29. #include <boost/log/detail/locking_ptr.hpp>
  30. #include <boost/log/detail/parameter_tools.hpp>
  31. #include <boost/log/core/record_view.hpp>
  32. #include <boost/log/sinks/basic_sink_frontend.hpp>
  33. #include <boost/log/sinks/frontend_requirements.hpp>
  34. #include <boost/log/detail/header.hpp>
  35. namespace boost {
  36. BOOST_LOG_OPEN_NAMESPACE
  37. namespace sinks {
  38. #ifndef BOOST_LOG_DOXYGEN_PASS
  39. #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1(z, n, data)\
  40. template< typename T0 >\
  41. explicit synchronous_sink(T0 const& arg0, typename boost::log::aux::enable_if_named_parameters< T0, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :\
  42. base_type(false),\
  43. m_pBackend(boost::make_shared< sink_backend_type >(arg0)) {}
  44. #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N(z, n, data)\
  45. template< BOOST_PP_ENUM_PARAMS_Z(z, n, typename T) >\
  46. explicit synchronous_sink(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, T, const& arg)) :\
  47. base_type(false),\
  48. m_pBackend(boost::make_shared< sink_backend_type >(BOOST_PP_ENUM_PARAMS_Z(z, n, arg))) {}
  49. #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL(z, n, data)\
  50. BOOST_PP_IF(BOOST_PP_EQUAL(n, 1), BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1, BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N)(z, n, data)
  51. #endif // BOOST_LOG_DOXYGEN_PASS
  52. /*!
  53. * \brief Synchronous logging sink frontend
  54. *
  55. * The sink frontend serializes threads before passing logging records to the backend
  56. */
  57. template< typename SinkBackendT >
  58. class synchronous_sink :
  59. public aux::make_sink_frontend_base< SinkBackendT >::type
  60. {
  61. typedef typename aux::make_sink_frontend_base< SinkBackendT >::type base_type;
  62. private:
  63. //! Synchronization mutex type
  64. typedef boost::recursive_mutex backend_mutex_type;
  65. public:
  66. //! Sink implementation type
  67. typedef SinkBackendT sink_backend_type;
  68. //! \cond
  69. BOOST_STATIC_ASSERT_MSG((has_requirement< typename sink_backend_type::frontend_requirements, synchronized_feeding >::value), "Synchronous sink frontend is incompatible with the specified backend: thread synchronization requirements are not met");
  70. //! \endcond
  71. #ifndef BOOST_LOG_DOXYGEN_PASS
  72. //! A pointer type that locks the backend until it's destroyed
  73. typedef boost::log::aux::locking_ptr< sink_backend_type, backend_mutex_type > locked_backend_ptr;
  74. #else // BOOST_LOG_DOXYGEN_PASS
  75. //! A pointer type that locks the backend until it's destroyed
  76. typedef implementation_defined locked_backend_ptr;
  77. #endif // BOOST_LOG_DOXYGEN_PASS
  78. private:
  79. //! Synchronization mutex
  80. backend_mutex_type m_BackendMutex;
  81. //! Pointer to the backend
  82. const shared_ptr< sink_backend_type > m_pBackend;
  83. public:
  84. /*!
  85. * Default constructor. Constructs the sink backend instance.
  86. * Requires the backend to be default-constructible.
  87. */
  88. synchronous_sink() :
  89. base_type(false),
  90. m_pBackend(boost::make_shared< sink_backend_type >())
  91. {
  92. }
  93. /*!
  94. * Constructor attaches user-constructed backend instance
  95. *
  96. * \param backend Pointer to the backend instance
  97. *
  98. * \pre \a backend is not \c NULL.
  99. */
  100. explicit synchronous_sink(shared_ptr< sink_backend_type > const& backend) :
  101. base_type(false),
  102. m_pBackend(backend)
  103. {
  104. }
  105. /*!
  106. * Constructor that passes arbitrary named parameters to the interprocess sink backend constructor.
  107. * Refer to the backend documentation for the list of supported parameters.
  108. */
  109. #ifndef BOOST_LOG_DOXYGEN_PASS
  110. BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL, ~)
  111. #else
  112. template< typename... Args >
  113. explicit synchronous_sink(Args&&... args);
  114. #endif
  115. /*!
  116. * Locking accessor to the attached backend
  117. */
  118. locked_backend_ptr locked_backend()
  119. {
  120. return locked_backend_ptr(m_pBackend, m_BackendMutex);
  121. }
  122. /*!
  123. * Passes the log record to the backend
  124. */
  125. void consume(record_view const& rec) BOOST_OVERRIDE
  126. {
  127. base_type::feed_record(rec, m_BackendMutex, *m_pBackend);
  128. }
  129. /*!
  130. * The method attempts to pass logging record to the backend
  131. */
  132. bool try_consume(record_view const& rec) BOOST_OVERRIDE
  133. {
  134. return base_type::try_feed_record(rec, m_BackendMutex, *m_pBackend);
  135. }
  136. /*!
  137. * The method performs flushing of any internal buffers that may hold log records. The method
  138. * may take considerable time to complete and may block both the calling thread and threads
  139. * attempting to put new records into the sink while this call is in progress.
  140. */
  141. void flush() BOOST_OVERRIDE
  142. {
  143. base_type::flush_backend(m_BackendMutex, *m_pBackend);
  144. }
  145. };
  146. #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1
  147. #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N
  148. #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL
  149. } // namespace sinks
  150. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  151. } // namespace boost
  152. #include <boost/log/detail/footer.hpp>
  153. #endif // BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_