unlocked_frontend.hpp 4.1 KB

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