comparison DEPENDENCIES/generic/include/boost/log/sinks/sync_frontend.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
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 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/static_assert.hpp>
29 #include <boost/smart_ptr/shared_ptr.hpp>
30 #include <boost/smart_ptr/make_shared_object.hpp>
31 #include <boost/thread/mutex.hpp>
32 #include <boost/log/detail/locking_ptr.hpp>
33 #include <boost/log/detail/parameter_tools.hpp>
34 #include <boost/log/core/record_view.hpp>
35 #include <boost/log/sinks/basic_sink_frontend.hpp>
36 #include <boost/log/sinks/frontend_requirements.hpp>
37 #include <boost/log/detail/header.hpp>
38
39 namespace boost {
40
41 BOOST_LOG_OPEN_NAMESPACE
42
43 namespace sinks {
44
45 #ifndef BOOST_LOG_DOXYGEN_PASS
46
47 #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL(z, n, data)\
48 template< BOOST_PP_ENUM_PARAMS(n, typename T) >\
49 explicit synchronous_sink(BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& arg)) :\
50 base_type(false),\
51 m_pBackend(boost::make_shared< sink_backend_type >(BOOST_PP_ENUM_PARAMS(n, arg))) {}
52
53 #endif // BOOST_LOG_DOXYGEN_PASS
54
55 /*!
56 * \brief Synchronous logging sink frontend
57 *
58 * The sink frontend serializes threads before passing logging records to the backend
59 */
60 template< typename SinkBackendT >
61 class synchronous_sink :
62 public aux::make_sink_frontend_base< SinkBackendT >::type,
63 private boost::log::aux::locking_ptr_counter_base
64 {
65 typedef typename aux::make_sink_frontend_base< SinkBackendT >::type base_type;
66
67 private:
68 //! Synchronization mutex type
69 typedef boost::mutex backend_mutex_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, synchronized_feeding >::value), "Synchronous sink frontend is incompatible with the specified backend: thread synchronization requirements are not met");
76 //! \endcond
77
78 #ifndef BOOST_LOG_DOXYGEN_PASS
79
80 //! A pointer type that locks the backend until it's destroyed
81 typedef boost::log::aux::locking_ptr< sink_backend_type > locked_backend_ptr;
82
83 #else // BOOST_LOG_DOXYGEN_PASS
84
85 //! A pointer type that locks the backend until it's destroyed
86 typedef implementation_defined locked_backend_ptr;
87
88 #endif // BOOST_LOG_DOXYGEN_PASS
89
90 private:
91 //! Synchronization mutex
92 backend_mutex_type m_BackendMutex;
93 //! Pointer to the backend
94 const shared_ptr< sink_backend_type > m_pBackend;
95
96 public:
97 /*!
98 * Default constructor. Constructs the sink backend instance.
99 * Requires the backend to be default-constructible.
100 */
101 synchronous_sink() :
102 base_type(false),
103 m_pBackend(boost::make_shared< sink_backend_type >())
104 {
105 }
106 /*!
107 * Constructor attaches user-constructed backend instance
108 *
109 * \param backend Pointer to the backend instance
110 *
111 * \pre \a backend is not \c NULL.
112 */
113 explicit synchronous_sink(shared_ptr< sink_backend_type > const& backend) :
114 base_type(false),
115 m_pBackend(backend)
116 {
117 }
118
119 // Constructors that pass arbitrary parameters to the backend constructor
120 BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL, ~)
121
122 /*!
123 * Locking accessor to the attached backend
124 */
125 locked_backend_ptr locked_backend()
126 {
127 return locked_backend_ptr(
128 m_pBackend,
129 static_cast< boost::log::aux::locking_ptr_counter_base& >(*this));
130 }
131
132 /*!
133 * Passes the log record to the backend
134 */
135 void consume(record_view const& rec)
136 {
137 base_type::feed_record(rec, m_BackendMutex, *m_pBackend);
138 }
139
140 /*!
141 * The method attempts to pass logging record to the backend
142 */
143 bool try_consume(record_view const& rec)
144 {
145 return base_type::try_feed_record(rec, m_BackendMutex, *m_pBackend);
146 }
147
148 /*!
149 * The method performs flushing of any internal buffers that may hold log records. The method
150 * may take considerable time to complete and may block both the calling thread and threads
151 * attempting to put new records into the sink while this call is in progress.
152 */
153 void flush()
154 {
155 base_type::flush_backend(m_BackendMutex, *m_pBackend);
156 }
157
158 private:
159 #ifndef BOOST_LOG_DOXYGEN_PASS
160 // locking_ptr_counter_base methods
161 void lock() { m_BackendMutex.lock(); }
162 bool try_lock() { return m_BackendMutex.try_lock(); }
163 void unlock() { m_BackendMutex.unlock(); }
164 #endif // BOOST_LOG_DOXYGEN_PASS
165 };
166
167 #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL
168
169 } // namespace sinks
170
171 BOOST_LOG_CLOSE_NAMESPACE // namespace log
172
173 } // namespace boost
174
175 #include <boost/log/detail/footer.hpp>
176
177 #endif // BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_