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