2 * Copyright Lingxi Li 2015.
3 * Copyright Andrey Semashev 2016.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
9 * \file text_ipc_message_queue_backend.hpp
11 * \author Andrey Semashev
14 * The header contains implementation of a text interprocess message queue sink
15 * backend along with implementation of a supporting interprocess message queue.
18 #ifndef BOOST_LOG_SINKS_TEXT_IPC_MESSAGE_QUEUE_BACKEND_HPP_INCLUDED_
19 #define BOOST_LOG_SINKS_TEXT_IPC_MESSAGE_QUEUE_BACKEND_HPP_INCLUDED_
23 #include <boost/cstdint.hpp>
24 #include <boost/move/core.hpp>
25 #include <boost/preprocessor/control/if.hpp>
26 #include <boost/preprocessor/comparison/equal.hpp>
27 #include <boost/log/detail/config.hpp>
28 #include <boost/log/detail/parameter_tools.hpp>
29 #include <boost/log/core/record_view.hpp>
30 #include <boost/log/sinks/basic_sink_backend.hpp>
31 #include <boost/log/sinks/frontend_requirements.hpp>
32 #include <boost/log/exceptions.hpp>
33 #include <boost/log/detail/header.hpp>
35 #ifdef BOOST_HAS_PRAGMA_ONCE
41 BOOST_LOG_OPEN_NAMESPACE
45 #ifndef BOOST_LOG_DOXYGEN_PASS
47 #define BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL_1(z, n, data)\
48 template< typename T0 >\
49 explicit text_ipc_message_queue_backend(T0 const& arg0, typename boost::log::aux::enable_if_named_parameters< T0, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) :\
52 #define BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL_N(z, n, data)\
53 template< BOOST_PP_ENUM_PARAMS_Z(z, n, typename T) >\
54 explicit text_ipc_message_queue_backend(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, T, const& arg)) :\
55 m_queue(BOOST_PP_ENUM_PARAMS_Z(z, n, arg)) {}
57 #define BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL(z, n, data)\
58 BOOST_PP_IF(BOOST_PP_EQUAL(n, 1), BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL_1, BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL_N)(z, n, data)
60 #endif // BOOST_LOG_DOXYGEN_PASS
63 * \brief An implementation of a text interprocess message queue sink backend and
64 * a supporting interprocess message queue.
66 * The sink backend sends formatted log messages to an interprocess message queue
67 * which can be extracted by a viewer process. Methods of this class are not
68 * thread-safe, unless otherwise specified.
70 template< typename QueueT >
71 class text_ipc_message_queue_backend :
72 public basic_formatted_sink_backend< char, concurrent_feeding >
75 typedef basic_formatted_sink_backend< char, concurrent_feeding > base_type;
79 typedef base_type::char_type char_type;
80 //! String type to be used as a message text holder
81 typedef base_type::string_type string_type;
82 //! Interprocess message queue type
83 typedef QueueT queue_type;
86 //! Interprocess queue
91 * Default constructor. The method constructs the backend using the default-constructed
92 * interprocess message queue. The queue may need additional setup in order to be able
95 text_ipc_message_queue_backend() BOOST_NOEXCEPT
100 * Initializing constructor. The method constructs the backend using the provided
101 * interprocess message queue. The constructor moves from the provided queue.
103 explicit text_ipc_message_queue_backend(BOOST_RV_REF(queue_type) queue) BOOST_NOEXCEPT :
104 m_queue(static_cast< BOOST_RV_REF(queue_type) >(queue))
109 * Constructor that passes arbitrary named parameters to the interprocess queue constructor.
110 * Refer to the queue documentation for the list of supported parameters.
112 #ifndef BOOST_LOG_DOXYGEN_PASS
113 BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL, ~)
115 template< typename... Args >
116 explicit text_ipc_message_queue_backend(Args&&... args);
120 * The method returns a reference to the managed \c queue_type object.
122 * \return A reference to the managed \c queue_type object.
124 queue_type& message_queue() BOOST_NOEXCEPT { return m_queue; }
127 * The method returns a constant reference to the managed \c queue_type object.
129 * \return A constant reference to the managed \c queue_type object.
131 queue_type const& message_queue() const BOOST_NOEXCEPT { return m_queue; }
134 * Tests whether the object is associated with any message queue. Only when the backend has
135 * an associated message queue, will any message be sent.
137 * \return \c true if the object is associated with a message queue, and \c false otherwise.
139 bool is_open() const BOOST_NOEXCEPT { return m_queue.is_open(); }
142 * The method writes the message to the backend. Concurrent calls to this method
143 * are allowed. Therefore, the backend may be used with unlocked frontend. <tt>stop_local()</tt>
144 * can be used to have a blocked <tt>consume()</tt> call return and prevent future
145 * calls to <tt>consume()</tt> from blocking.
147 void consume(record_view const&, string_type const& formatted_message)
149 if (m_queue.is_open())
151 typedef typename queue_type::size_type size_type;
152 const string_type::size_type size = formatted_message.size();
153 if (BOOST_UNLIKELY(size > static_cast< string_type::size_type >((std::numeric_limits< size_type >::max)())))
154 BOOST_LOG_THROW_DESCR(limitation_error, "Message too long to send to an interprocess queue");
155 m_queue.send(formatted_message.data(), static_cast< size_type >(size));
160 #undef BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL_1
161 #undef BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL_N
162 #undef BOOST_LOG_IPC_BACKEND_CTOR_FORWARD_INTERNAL
166 BOOST_LOG_CLOSE_NAMESPACE // namespace log
170 #include <boost/log/detail/footer.hpp>
172 #endif // BOOST_LOG_SINKS_TEXT_IPC_MESSAGE_QUEUE_BACKEND_HPP_INCLUDED_