unlocked_frontend.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 unlocked_frontend.hpp
  9. * \author Andrey Semashev
  10. * \date 14.07.2009
  11. *
  12. * The header contains declaration of an unlocked sink frontend.
  13. */
  14. #ifndef BOOST_LOG_SINKS_UNLOCKED_FRONTEND_HPP_INCLUDED_
  15. #define BOOST_LOG_SINKS_UNLOCKED_FRONTEND_HPP_INCLUDED_
  16. #include <boost/static_assert.hpp>
  17. #include <boost/smart_ptr/shared_ptr.hpp>
  18. #include <boost/smart_ptr/make_shared_object.hpp>
  19. #include <boost/preprocessor/control/if.hpp>
  20. #include <boost/preprocessor/comparison/equal.hpp>
  21. #include <boost/log/detail/config.hpp>
  22. #include <boost/log/detail/parameter_tools.hpp>
  23. #include <boost/log/detail/fake_mutex.hpp>
  24. #include <boost/log/sinks/basic_sink_frontend.hpp>
  25. #include <boost/log/sinks/frontend_requirements.hpp>
  26. #include <boost/log/detail/header.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. namespace boost {
  31. BOOST_LOG_OPEN_NAMESPACE
  32. namespace sinks {
  33. #ifndef BOOST_LOG_DOXYGEN_PASS
  34. #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1(z, n, data)\
  35. template< typename T0 >\
  36. explicit unlocked_sink(T0 const& arg0, typename boost::log::aux::enable_if_named_parameters< T0, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :\
  37. base_type(false),\
  38. m_pBackend(boost::make_shared< sink_backend_type >(arg0)) {}
  39. #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N(z, n, data)\
  40. template< BOOST_PP_ENUM_PARAMS_Z(z, n, typename T) >\
  41. explicit unlocked_sink(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, T, const& arg)) :\
  42. base_type(false),\
  43. m_pBackend(boost::make_shared< sink_backend_type >(BOOST_PP_ENUM_PARAMS_Z(z, n, arg))) {}
  44. #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL(z, n, data)\
  45. 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)
  46. #endif // BOOST_LOG_DOXYGEN_PASS
  47. /*!
  48. * \brief Non-blocking logging sink frontend
  49. *
  50. * The sink frontend does not perform thread synchronization and
  51. * simply passes logging records to the sink backend.
  52. */
  53. template< typename SinkBackendT >
  54. class unlocked_sink :
  55. public aux::make_sink_frontend_base< SinkBackendT >::type
  56. {
  57. typedef typename aux::make_sink_frontend_base< SinkBackendT >::type base_type;
  58. public:
  59. //! Sink implementation type
  60. typedef SinkBackendT sink_backend_type;
  61. //! \cond
  62. BOOST_STATIC_ASSERT_MSG((has_requirement< typename sink_backend_type::frontend_requirements, concurrent_feeding >::value), "Unlocked sink frontend is incompatible with the specified backend: thread synchronization requirements are not met");
  63. //! \endcond
  64. //! Type of pointer to the backend
  65. typedef shared_ptr< sink_backend_type > locked_backend_ptr;
  66. private:
  67. //! Pointer to the backend
  68. const shared_ptr< sink_backend_type > m_pBackend;
  69. public:
  70. /*!
  71. * Default constructor. Constructs the sink backend instance.
  72. * Requires the backend to be default-constructible.
  73. */
  74. unlocked_sink() :
  75. base_type(false),
  76. m_pBackend(boost::make_shared< sink_backend_type >())
  77. {
  78. }
  79. /*!
  80. * Constructor attaches user-constructed backend instance
  81. *
  82. * \param backend Pointer to the backend instance
  83. *
  84. * \pre \a backend is not \c NULL.
  85. */
  86. explicit unlocked_sink(shared_ptr< sink_backend_type > const& backend) :
  87. base_type(false),
  88. m_pBackend(backend)
  89. {
  90. }
  91. /*!
  92. * Constructor that passes arbitrary named parameters to the interprocess sink backend constructor.
  93. * Refer to the backend documentation for the list of supported parameters.
  94. */
  95. #ifndef BOOST_LOG_DOXYGEN_PASS
  96. BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL, ~)
  97. #else
  98. template< typename... Args >
  99. explicit unlocked_sink(Args&&... args);
  100. #endif
  101. /*!
  102. * Locking accessor to the attached backend.
  103. *
  104. * \note Does not do any actual locking, provided only for interface consistency
  105. * with other frontends.
  106. */
  107. locked_backend_ptr locked_backend()
  108. {
  109. return m_pBackend;
  110. }
  111. /*!
  112. * Passes the log record to the backend
  113. */
  114. void consume(record_view const& rec)
  115. {
  116. boost::log::aux::fake_mutex m;
  117. base_type::feed_record(rec, m, *m_pBackend);
  118. }
  119. /*!
  120. * The method performs flushing of any internal buffers that may hold log records. The method
  121. * may take considerable time to complete and may block both the calling thread and threads
  122. * attempting to put new records into the sink while this call is in progress.
  123. */
  124. void flush()
  125. {
  126. boost::log::aux::fake_mutex m;
  127. base_type::flush_backend(m, *m_pBackend);
  128. }
  129. };
  130. #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1
  131. #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N
  132. #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL
  133. } // namespace sinks
  134. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  135. } // namespace boost
  136. #include <boost/log/detail/footer.hpp>
  137. #endif // BOOST_LOG_SINKS_UNLOCKED_FRONTEND_HPP_INCLUDED_