Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:25

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   sync_frontend.hpp
0009  * \author Andrey Semashev
0010  * \date   14.07.2009
0011  *
0012  * The header contains implementation of synchronous sink frontend.
0013  */
0014 
0015 #ifndef BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_
0016 #define BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_
0017 
0018 #include <boost/log/detail/config.hpp>
0019 
0020 #ifdef BOOST_HAS_PRAGMA_ONCE
0021 #pragma once
0022 #endif
0023 
0024 #if defined(BOOST_LOG_NO_THREADS)
0025 #error Boost.Log: Synchronous sink frontend is only supported in multithreaded environment
0026 #endif
0027 
0028 #include <boost/smart_ptr/shared_ptr.hpp>
0029 #include <boost/smart_ptr/make_shared_object.hpp>
0030 #include <boost/preprocessor/control/if.hpp>
0031 #include <boost/preprocessor/comparison/equal.hpp>
0032 #include <boost/thread/recursive_mutex.hpp>
0033 #include <boost/log/detail/locking_ptr.hpp>
0034 #include <boost/log/detail/parameter_tools.hpp>
0035 #include <boost/log/core/record_view.hpp>
0036 #include <boost/log/sinks/basic_sink_frontend.hpp>
0037 #include <boost/log/sinks/frontend_requirements.hpp>
0038 #include <boost/log/detail/header.hpp>
0039 
0040 namespace boost {
0041 
0042 BOOST_LOG_OPEN_NAMESPACE
0043 
0044 namespace sinks {
0045 
0046 #ifndef BOOST_LOG_DOXYGEN_PASS
0047 
0048 #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1(z, n, data)\
0049     template< typename T0 >\
0050     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()) :\
0051         base_type(false),\
0052         m_pBackend(boost::make_shared< sink_backend_type >(arg0)) {}
0053 
0054 #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N(z, n, data)\
0055     template< BOOST_PP_ENUM_PARAMS_Z(z, n, typename T) >\
0056     explicit synchronous_sink(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, T, const& arg)) :\
0057         base_type(false),\
0058         m_pBackend(boost::make_shared< sink_backend_type >(BOOST_PP_ENUM_PARAMS_Z(z, n, arg))) {}
0059 
0060 #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL(z, n, data)\
0061     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)
0062 
0063 #endif // BOOST_LOG_DOXYGEN_PASS
0064 
0065 /*!
0066  * \brief Synchronous logging sink frontend
0067  *
0068  * The sink frontend serializes threads before passing logging records to the backend
0069  */
0070 template< typename SinkBackendT >
0071 class synchronous_sink :
0072     public aux::make_sink_frontend_base< SinkBackendT >::type
0073 {
0074     typedef typename aux::make_sink_frontend_base< SinkBackendT >::type base_type;
0075 
0076 private:
0077     //! Synchronization mutex type
0078     typedef boost::recursive_mutex backend_mutex_type;
0079 
0080 public:
0081     //! Sink implementation type
0082     typedef SinkBackendT sink_backend_type;
0083     //! \cond
0084     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");
0085     //! \endcond
0086 
0087 #ifndef BOOST_LOG_DOXYGEN_PASS
0088 
0089     //! A pointer type that locks the backend until it's destroyed
0090     typedef boost::log::aux::locking_ptr< sink_backend_type, backend_mutex_type > locked_backend_ptr;
0091 
0092 #else // BOOST_LOG_DOXYGEN_PASS
0093 
0094     //! A pointer type that locks the backend until it's destroyed
0095     typedef implementation_defined locked_backend_ptr;
0096 
0097 #endif // BOOST_LOG_DOXYGEN_PASS
0098 
0099 private:
0100     //! Synchronization mutex
0101     backend_mutex_type m_BackendMutex;
0102     //! Pointer to the backend
0103     const shared_ptr< sink_backend_type > m_pBackend;
0104 
0105 public:
0106     /*!
0107      * Default constructor. Constructs the sink backend instance.
0108      * Requires the backend to be default-constructible.
0109      */
0110     synchronous_sink() :
0111         base_type(false),
0112         m_pBackend(boost::make_shared< sink_backend_type >())
0113     {
0114     }
0115     /*!
0116      * Constructor attaches user-constructed backend instance
0117      *
0118      * \param backend Pointer to the backend instance
0119      *
0120      * \pre \a backend is not \c NULL.
0121      */
0122     explicit synchronous_sink(shared_ptr< sink_backend_type > const& backend) :
0123         base_type(false),
0124         m_pBackend(backend)
0125     {
0126     }
0127 
0128     /*!
0129      * Constructor that passes arbitrary named parameters to the interprocess sink backend constructor.
0130      * Refer to the backend documentation for the list of supported parameters.
0131      */
0132 #ifndef BOOST_LOG_DOXYGEN_PASS
0133     BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL, ~)
0134 #else
0135     template< typename... Args >
0136     explicit synchronous_sink(Args&&... args);
0137 #endif
0138 
0139     /*!
0140      * Locking accessor to the attached backend
0141      */
0142     locked_backend_ptr locked_backend()
0143     {
0144         return locked_backend_ptr(m_pBackend, m_BackendMutex);
0145     }
0146 
0147     /*!
0148      * Passes the log record to the backend
0149      */
0150     void consume(record_view const& rec) BOOST_OVERRIDE
0151     {
0152         base_type::feed_record(rec, m_BackendMutex, *m_pBackend);
0153     }
0154 
0155     /*!
0156      * The method attempts to pass logging record to the backend
0157      */
0158     bool try_consume(record_view const& rec) BOOST_OVERRIDE
0159     {
0160         return base_type::try_feed_record(rec, m_BackendMutex, *m_pBackend);
0161     }
0162 
0163     /*!
0164      * The method performs flushing of any internal buffers that may hold log records. The method
0165      * may take considerable time to complete and may block both the calling thread and threads
0166      * attempting to put new records into the sink while this call is in progress.
0167      */
0168     void flush() BOOST_OVERRIDE
0169     {
0170         base_type::flush_backend(m_BackendMutex, *m_pBackend);
0171     }
0172 };
0173 
0174 #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1
0175 #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N
0176 #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL
0177 
0178 } // namespace sinks
0179 
0180 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0181 
0182 } // namespace boost
0183 
0184 #include <boost/log/detail/footer.hpp>
0185 
0186 #endif // BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_