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