Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
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@101
|
31 #include <boost/thread/recursive_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@101
|
62 public aux::make_sink_frontend_base< SinkBackendT >::type
|
Chris@16
|
63 {
|
Chris@16
|
64 typedef typename aux::make_sink_frontend_base< SinkBackendT >::type base_type;
|
Chris@16
|
65
|
Chris@16
|
66 private:
|
Chris@16
|
67 //! Synchronization mutex type
|
Chris@101
|
68 typedef boost::recursive_mutex backend_mutex_type;
|
Chris@16
|
69
|
Chris@16
|
70 public:
|
Chris@16
|
71 //! Sink implementation type
|
Chris@16
|
72 typedef SinkBackendT sink_backend_type;
|
Chris@16
|
73 //! \cond
|
Chris@16
|
74 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
|
75 //! \endcond
|
Chris@16
|
76
|
Chris@16
|
77 #ifndef BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
78
|
Chris@16
|
79 //! A pointer type that locks the backend until it's destroyed
|
Chris@101
|
80 typedef boost::log::aux::locking_ptr< sink_backend_type, backend_mutex_type > locked_backend_ptr;
|
Chris@16
|
81
|
Chris@16
|
82 #else // BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
83
|
Chris@16
|
84 //! A pointer type that locks the backend until it's destroyed
|
Chris@16
|
85 typedef implementation_defined locked_backend_ptr;
|
Chris@16
|
86
|
Chris@16
|
87 #endif // BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
88
|
Chris@16
|
89 private:
|
Chris@16
|
90 //! Synchronization mutex
|
Chris@16
|
91 backend_mutex_type m_BackendMutex;
|
Chris@16
|
92 //! Pointer to the backend
|
Chris@16
|
93 const shared_ptr< sink_backend_type > m_pBackend;
|
Chris@16
|
94
|
Chris@16
|
95 public:
|
Chris@16
|
96 /*!
|
Chris@16
|
97 * Default constructor. Constructs the sink backend instance.
|
Chris@16
|
98 * Requires the backend to be default-constructible.
|
Chris@16
|
99 */
|
Chris@16
|
100 synchronous_sink() :
|
Chris@16
|
101 base_type(false),
|
Chris@16
|
102 m_pBackend(boost::make_shared< sink_backend_type >())
|
Chris@16
|
103 {
|
Chris@16
|
104 }
|
Chris@16
|
105 /*!
|
Chris@16
|
106 * Constructor attaches user-constructed backend instance
|
Chris@16
|
107 *
|
Chris@16
|
108 * \param backend Pointer to the backend instance
|
Chris@16
|
109 *
|
Chris@16
|
110 * \pre \a backend is not \c NULL.
|
Chris@16
|
111 */
|
Chris@16
|
112 explicit synchronous_sink(shared_ptr< sink_backend_type > const& backend) :
|
Chris@16
|
113 base_type(false),
|
Chris@16
|
114 m_pBackend(backend)
|
Chris@16
|
115 {
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 // Constructors that pass arbitrary parameters to the backend constructor
|
Chris@16
|
119 BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL, ~)
|
Chris@16
|
120
|
Chris@16
|
121 /*!
|
Chris@16
|
122 * Locking accessor to the attached backend
|
Chris@16
|
123 */
|
Chris@16
|
124 locked_backend_ptr locked_backend()
|
Chris@16
|
125 {
|
Chris@101
|
126 return locked_backend_ptr(m_pBackend, m_BackendMutex);
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 /*!
|
Chris@16
|
130 * Passes the log record to the backend
|
Chris@16
|
131 */
|
Chris@16
|
132 void consume(record_view const& rec)
|
Chris@16
|
133 {
|
Chris@16
|
134 base_type::feed_record(rec, m_BackendMutex, *m_pBackend);
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 /*!
|
Chris@16
|
138 * The method attempts to pass logging record to the backend
|
Chris@16
|
139 */
|
Chris@16
|
140 bool try_consume(record_view const& rec)
|
Chris@16
|
141 {
|
Chris@16
|
142 return base_type::try_feed_record(rec, m_BackendMutex, *m_pBackend);
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 /*!
|
Chris@16
|
146 * The method performs flushing of any internal buffers that may hold log records. The method
|
Chris@16
|
147 * may take considerable time to complete and may block both the calling thread and threads
|
Chris@16
|
148 * attempting to put new records into the sink while this call is in progress.
|
Chris@16
|
149 */
|
Chris@16
|
150 void flush()
|
Chris@16
|
151 {
|
Chris@16
|
152 base_type::flush_backend(m_BackendMutex, *m_pBackend);
|
Chris@16
|
153 }
|
Chris@16
|
154 };
|
Chris@16
|
155
|
Chris@16
|
156 #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL
|
Chris@16
|
157
|
Chris@16
|
158 } // namespace sinks
|
Chris@16
|
159
|
Chris@16
|
160 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
161
|
Chris@16
|
162 } // namespace boost
|
Chris@16
|
163
|
Chris@16
|
164 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
165
|
Chris@16
|
166 #endif // BOOST_LOG_SINKS_SYNC_FRONTEND_HPP_INCLUDED_
|