| 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 sync_frontend.hpp |
| 9 | * \author Andrey Semashev |
| 10 | * \date 14.07.2009 |
| 11 | * |
| 12 | * The header contains implementation of synchronous sink frontend. |
| 13 | */ |
| 14 | |
| 15 | #ifndef BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_ |
| 16 | #define BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_ |
| 17 | |
| 18 | #include <boost/log/detail/config.hpp> |
| 19 | |
| 20 | #ifdef BOOST_HAS_PRAGMA_ONCE |
| 21 | #pragma once |
| 22 | #endif |
| 23 | |
| 24 | #if defined(BOOST_LOG_NO_THREADS) |
| 25 | #error Boost.Log: Synchronous sink frontend is only supported in multithreaded environment |
| 26 | #endif |
| 27 | |
| 28 | #include <boost/smart_ptr/shared_ptr.hpp> |
| 29 | #include <boost/smart_ptr/make_shared_object.hpp> |
| 30 | #include <boost/preprocessor/control/if.hpp> |
| 31 | #include <boost/preprocessor/comparison/equal.hpp> |
| 32 | #include <boost/thread/recursive_mutex.hpp> |
| 33 | #include <boost/log/detail/locking_ptr.hpp> |
| 34 | #include <boost/log/detail/parameter_tools.hpp> |
| 35 | #include <boost/log/core/record_view.hpp> |
| 36 | #include <boost/log/sinks/basic_sink_frontend.hpp> |
| 37 | #include <boost/log/sinks/frontend_requirements.hpp> |
| 38 | #include <boost/log/detail/header.hpp> |
| 39 | |
| 40 | namespace boost { |
| 41 | |
| 42 | BOOST_LOG_OPEN_NAMESPACE |
| 43 | |
| 44 | namespace sinks { |
| 45 | |
| 46 | #ifndef BOOST_LOG_DOXYGEN_PASS |
| 47 | |
| 48 | #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1(z, n, data)\ |
| 49 | template< typename T0 >\ |
| 50 | explicit synchronous_sink(T0 const& arg0, typename boost::log::aux::enable_if_named_parameters< T0, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :\ |
| 51 | base_type(false),\ |
| 52 | m_pBackend(boost::make_shared< sink_backend_type >(arg0)) {} |
| 53 | |
| 54 | #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N(z, n, data)\ |
| 55 | template< BOOST_PP_ENUM_PARAMS_Z(z, n, typename T) >\ |
| 56 | explicit synchronous_sink(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, T, const& arg)) :\ |
| 57 | base_type(false),\ |
| 58 | m_pBackend(boost::make_shared< sink_backend_type >(BOOST_PP_ENUM_PARAMS_Z(z, n, arg))) {} |
| 59 | |
| 60 | #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL(z, n, data)\ |
| 61 | 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) |
| 62 | |
| 63 | #endif // BOOST_LOG_DOXYGEN_PASS |
| 64 | |
| 65 | /*! |
| 66 | * \brief Synchronous logging sink frontend |
| 67 | * |
| 68 | * The sink frontend serializes threads before passing logging records to the backend |
| 69 | */ |
| 70 | template< typename SinkBackendT > |
| 71 | class synchronous_sink : |
| 72 | public aux::make_sink_frontend_base< SinkBackendT >::type |
| 73 | { |
| 74 | typedef typename aux::make_sink_frontend_base< SinkBackendT >::type base_type; |
| 75 | |
| 76 | private: |
| 77 | //! Synchronization mutex type |
| 78 | typedef boost::recursive_mutex backend_mutex_type; |
| 79 | |
| 80 | public: |
| 81 | //! Sink implementation type |
| 82 | typedef SinkBackendT sink_backend_type; |
| 83 | //! \cond |
| 84 | static_assert(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" ); |
| 85 | //! \endcond |
| 86 | |
| 87 | #ifndef BOOST_LOG_DOXYGEN_PASS |
| 88 | |
| 89 | //! A pointer type that locks the backend until it's destroyed |
| 90 | typedef boost::log::aux::locking_ptr< sink_backend_type, backend_mutex_type > locked_backend_ptr; |
| 91 | |
| 92 | #else // BOOST_LOG_DOXYGEN_PASS |
| 93 | |
| 94 | //! A pointer type that locks the backend until it's destroyed |
| 95 | typedef implementation_defined locked_backend_ptr; |
| 96 | |
| 97 | #endif // BOOST_LOG_DOXYGEN_PASS |
| 98 | |
| 99 | private: |
| 100 | //! Synchronization mutex |
| 101 | backend_mutex_type m_BackendMutex; |
| 102 | //! Pointer to the backend |
| 103 | const shared_ptr< sink_backend_type > m_pBackend; |
| 104 | |
| 105 | public: |
| 106 | /*! |
| 107 | * Default constructor. Constructs the sink backend instance. |
| 108 | * Requires the backend to be default-constructible. |
| 109 | */ |
| 110 | synchronous_sink() : |
| 111 | base_type(false), |
| 112 | m_pBackend(boost::make_shared< sink_backend_type >()) |
| 113 | { |
| 114 | } |
| 115 | /*! |
| 116 | * Constructor attaches user-constructed backend instance |
| 117 | * |
| 118 | * \param backend Pointer to the backend instance |
| 119 | * |
| 120 | * \pre \a backend is not \c NULL. |
| 121 | */ |
| 122 | explicit synchronous_sink(shared_ptr< sink_backend_type > const& backend) : |
| 123 | base_type(false), |
| 124 | m_pBackend(backend) |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | /*! |
| 129 | * Constructor that passes arbitrary named parameters to the interprocess sink backend constructor. |
| 130 | * Refer to the backend documentation for the list of supported parameters. |
| 131 | */ |
| 132 | #ifndef BOOST_LOG_DOXYGEN_PASS |
| 133 | BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL, ~) |
| 134 | #else |
| 135 | template< typename... Args > |
| 136 | explicit synchronous_sink(Args&&... args); |
| 137 | #endif |
| 138 | |
| 139 | /*! |
| 140 | * Locking accessor to the attached backend |
| 141 | */ |
| 142 | locked_backend_ptr locked_backend() |
| 143 | { |
| 144 | return locked_backend_ptr(m_pBackend, m_BackendMutex); |
| 145 | } |
| 146 | |
| 147 | /*! |
| 148 | * Passes the log record to the backend |
| 149 | */ |
| 150 | void consume(record_view const& rec) BOOST_OVERRIDE |
| 151 | { |
| 152 | base_type::feed_record(rec, m_BackendMutex, *m_pBackend); |
| 153 | } |
| 154 | |
| 155 | /*! |
| 156 | * The method attempts to pass logging record to the backend |
| 157 | */ |
| 158 | bool try_consume(record_view const& rec) BOOST_OVERRIDE |
| 159 | { |
| 160 | return base_type::try_feed_record(rec, m_BackendMutex, *m_pBackend); |
| 161 | } |
| 162 | |
| 163 | /*! |
| 164 | * The method performs flushing of any internal buffers that may hold log records. The method |
| 165 | * may take considerable time to complete and may block both the calling thread and threads |
| 166 | * attempting to put new records into the sink while this call is in progress. |
| 167 | */ |
| 168 | void flush() BOOST_OVERRIDE |
| 169 | { |
| 170 | base_type::flush_backend(m_BackendMutex, *m_pBackend); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1 |
| 175 | #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N |
| 176 | #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL |
| 177 | |
| 178 | } // namespace sinks |
| 179 | |
| 180 | BOOST_LOG_CLOSE_NAMESPACE // namespace log |
| 181 | |
| 182 | } // namespace boost |
| 183 | |
| 184 | #include <boost/log/detail/footer.hpp> |
| 185 | |
| 186 | #endif // BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_ |
| 187 | |