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)
8 * \file sync_frontend.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of synchronous sink frontend.
15 #ifndef BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_
16 #define BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_
18 #include <boost/log/detail/config.hpp>
20 #ifdef BOOST_HAS_PRAGMA_ONCE
24 #if defined(BOOST_LOG_NO_THREADS)
25 #error Boost.Log: Synchronous sink frontend is only supported in multithreaded environment
28 #include <boost/static_assert.hpp>
29 #include <boost/smart_ptr/shared_ptr.hpp>
30 #include <boost/smart_ptr/make_shared_object.hpp>
31 #include <boost/preprocessor/control/if.hpp>
32 #include <boost/preprocessor/comparison/equal.hpp>
33 #include <boost/thread/recursive_mutex.hpp>
34 #include <boost/log/detail/locking_ptr.hpp>
35 #include <boost/log/detail/parameter_tools.hpp>
36 #include <boost/log/core/record_view.hpp>
37 #include <boost/log/sinks/basic_sink_frontend.hpp>
38 #include <boost/log/sinks/frontend_requirements.hpp>
39 #include <boost/log/detail/header.hpp>
43 BOOST_LOG_OPEN_NAMESPACE
47 #ifndef BOOST_LOG_DOXYGEN_PASS
49 #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1(n, data)\
50 template< typename T0 >\
51 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()) :\
53 m_pBackend(boost::make_shared< sink_backend_type >(arg0)) {}
55 #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N(n, data)\
56 template< BOOST_PP_ENUM_PARAMS(n, typename T) >\
57 explicit synchronous_sink(BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& arg)) :\
59 m_pBackend(boost::make_shared< sink_backend_type >(BOOST_PP_ENUM_PARAMS(n, arg))) {}
61 #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL(z, n, data)\
62 BOOST_PP_IF(BOOST_PP_EQUAL(n, 1), BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1, BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N)(n, data)
64 #endif // BOOST_LOG_DOXYGEN_PASS
67 * \brief Synchronous logging sink frontend
69 * The sink frontend serializes threads before passing logging records to the backend
71 template< typename SinkBackendT >
72 class synchronous_sink :
73 public aux::make_sink_frontend_base< SinkBackendT >::type
75 typedef typename aux::make_sink_frontend_base< SinkBackendT >::type base_type;
78 //! Synchronization mutex type
79 typedef boost::recursive_mutex backend_mutex_type;
82 //! Sink implementation type
83 typedef SinkBackendT sink_backend_type;
85 BOOST_STATIC_ASSERT_MSG((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");
88 #ifndef BOOST_LOG_DOXYGEN_PASS
90 //! A pointer type that locks the backend until it's destroyed
91 typedef boost::log::aux::locking_ptr< sink_backend_type, backend_mutex_type > locked_backend_ptr;
93 #else // BOOST_LOG_DOXYGEN_PASS
95 //! A pointer type that locks the backend until it's destroyed
96 typedef implementation_defined locked_backend_ptr;
98 #endif // BOOST_LOG_DOXYGEN_PASS
101 //! Synchronization mutex
102 backend_mutex_type m_BackendMutex;
103 //! Pointer to the backend
104 const shared_ptr< sink_backend_type > m_pBackend;
108 * Default constructor. Constructs the sink backend instance.
109 * Requires the backend to be default-constructible.
113 m_pBackend(boost::make_shared< sink_backend_type >())
117 * Constructor attaches user-constructed backend instance
119 * \param backend Pointer to the backend instance
121 * \pre \a backend is not \c NULL.
123 explicit synchronous_sink(shared_ptr< sink_backend_type > const& backend) :
130 * Constructor that passes arbitrary named parameters to the interprocess sink backend constructor.
131 * Refer to the backend documentation for the list of supported parameters.
133 #ifndef BOOST_LOG_DOXYGEN_PASS
134 BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL, ~)
136 template< typename... Args >
137 explicit synchronous_sink(Args&&... args);
141 * Locking accessor to the attached backend
143 locked_backend_ptr locked_backend()
145 return locked_backend_ptr(m_pBackend, m_BackendMutex);
149 * Passes the log record to the backend
151 void consume(record_view const& rec)
153 base_type::feed_record(rec, m_BackendMutex, *m_pBackend);
157 * The method attempts to pass logging record to the backend
159 bool try_consume(record_view const& rec)
161 return base_type::try_feed_record(rec, m_BackendMutex, *m_pBackend);
165 * The method performs flushing of any internal buffers that may hold log records. The method
166 * may take considerable time to complete and may block both the calling thread and threads
167 * attempting to put new records into the sink while this call is in progress.
171 base_type::flush_backend(m_BackendMutex, *m_pBackend);
175 #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_1
176 #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL_N
177 #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL
181 BOOST_LOG_CLOSE_NAMESPACE // namespace log
185 #include <boost/log/detail/footer.hpp>
187 #endif // BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_