sync_frontend.hpp 5.2 KB

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