unlocked_frontend.hpp 5.2 KB

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