sync_frontend.hpp 6.0 KB

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