]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
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) | |
6 | */ | |
7 | /*! | |
8 | * \file channel_feature.hpp | |
9 | * \author Andrey Semashev | |
10 | * \date 28.02.2008 | |
11 | * | |
12 | * The header contains implementation of a channel support feature. | |
13 | */ | |
14 | ||
15 | #ifndef BOOST_LOG_SOURCES_CHANNEL_FEATURE_HPP_INCLUDED_ | |
16 | #define BOOST_LOG_SOURCES_CHANNEL_FEATURE_HPP_INCLUDED_ | |
17 | ||
18 | #include <string> | |
19 | #include <boost/move/core.hpp> | |
20 | #include <boost/move/utility_core.hpp> | |
21 | #include <boost/log/detail/config.hpp> | |
22 | #include <boost/log/detail/locks.hpp> | |
23 | #include <boost/log/detail/default_attribute_names.hpp> | |
24 | #include <boost/log/keywords/channel.hpp> | |
25 | #include <boost/log/attributes/mutable_constant.hpp> | |
26 | #include <boost/log/utility/strictest_lock.hpp> | |
27 | #include <boost/log/core/record.hpp> | |
28 | #include <boost/log/detail/header.hpp> | |
29 | ||
30 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
31 | #pragma once | |
32 | #endif | |
33 | ||
34 | namespace boost { | |
35 | ||
36 | BOOST_LOG_OPEN_NAMESPACE | |
37 | ||
38 | namespace sources { | |
39 | ||
40 | /*! | |
41 | * \brief Channel feature implementation | |
42 | */ | |
43 | template< typename BaseT, typename ChannelT > | |
44 | class basic_channel_logger : | |
45 | public BaseT | |
46 | { | |
47 | //! Base type | |
48 | typedef BaseT base_type; | |
49 | typedef basic_channel_logger this_type; | |
50 | BOOST_COPYABLE_AND_MOVABLE_ALT(this_type) | |
51 | ||
52 | public: | |
53 | //! Character type | |
54 | typedef typename base_type::char_type char_type; | |
55 | //! Final type | |
56 | typedef typename base_type::final_type final_type; | |
57 | //! Threading model being used | |
58 | typedef typename base_type::threading_model threading_model; | |
59 | ||
60 | //! Channel type | |
61 | typedef ChannelT channel_type; | |
62 | //! Channel attribute type | |
63 | typedef attributes::mutable_constant< channel_type > channel_attribute; | |
64 | ||
65 | //! Lock requirement for the \c open_record_unlocked method | |
66 | typedef typename strictest_lock< | |
67 | typename base_type::open_record_lock, | |
68 | #ifndef BOOST_LOG_NO_THREADS | |
69 | boost::log::aux::exclusive_lock_guard< threading_model > | |
70 | #else | |
71 | no_lock< threading_model > | |
72 | #endif // !defined(BOOST_LOG_NO_THREADS) | |
73 | >::type open_record_lock; | |
74 | ||
75 | //! Lock requirement for the \c swap_unlocked method | |
76 | typedef typename strictest_lock< | |
77 | typename base_type::swap_lock, | |
78 | #ifndef BOOST_LOG_NO_THREADS | |
79 | boost::log::aux::exclusive_lock_guard< threading_model > | |
80 | #else | |
81 | no_lock< threading_model > | |
82 | #endif // !defined(BOOST_LOG_NO_THREADS) | |
83 | >::type swap_lock; | |
84 | ||
85 | private: | |
86 | //! Default channel name generator | |
87 | struct make_default_channel_name | |
88 | { | |
89 | typedef channel_type result_type; | |
90 | result_type operator() () const { return result_type(); } | |
91 | }; | |
92 | ||
93 | private: | |
94 | //! Channel attribute | |
95 | channel_attribute m_ChannelAttr; | |
96 | ||
97 | public: | |
98 | /*! | |
99 | * Default constructor. The constructed logger has the default-constructed channel name. | |
100 | */ | |
101 | basic_channel_logger() : base_type(), m_ChannelAttr(channel_type()) | |
102 | { | |
103 | base_type::add_attribute_unlocked(boost::log::aux::default_attribute_names::channel(), m_ChannelAttr); | |
104 | } | |
105 | /*! | |
106 | * Copy constructor | |
107 | */ | |
108 | basic_channel_logger(basic_channel_logger const& that) : | |
109 | base_type(static_cast< base_type const& >(that)), | |
110 | m_ChannelAttr(that.m_ChannelAttr) | |
111 | { | |
112 | base_type::attributes()[boost::log::aux::default_attribute_names::channel()] = m_ChannelAttr; | |
113 | } | |
114 | /*! | |
115 | * Move constructor | |
116 | */ | |
117 | basic_channel_logger(BOOST_RV_REF(basic_channel_logger) that) : | |
118 | base_type(boost::move(static_cast< base_type& >(that))), | |
119 | m_ChannelAttr(boost::move(that.m_ChannelAttr)) | |
120 | { | |
121 | base_type::attributes()[boost::log::aux::default_attribute_names::channel()] = m_ChannelAttr; | |
122 | } | |
123 | /*! | |
124 | * Constructor with arguments. Allows to register a channel name attribute on construction. | |
125 | * | |
126 | * \param args A set of named arguments. The following arguments are supported: | |
127 | * \li \c channel - a string that represents the channel name | |
128 | */ | |
129 | template< typename ArgsT > | |
130 | explicit basic_channel_logger(ArgsT const& args) : | |
131 | base_type(args), | |
132 | m_ChannelAttr(args[keywords::channel || make_default_channel_name()]) | |
133 | { | |
134 | base_type::add_attribute_unlocked(boost::log::aux::default_attribute_names::channel(), m_ChannelAttr); | |
135 | } | |
136 | ||
137 | /*! | |
138 | * The observer of the channel name | |
139 | * | |
140 | * \return The channel name that was set by the logger | |
141 | */ | |
142 | channel_type channel() const | |
143 | { | |
144 | BOOST_LOG_EXPR_IF_MT(boost::log::aux::shared_lock_guard< const threading_model > lock(this->get_threading_model());) | |
145 | return m_ChannelAttr.get(); | |
146 | } | |
147 | ||
148 | /*! | |
149 | * The setter of the channel name | |
150 | * | |
151 | * \param ch The channel name to be set for the logger | |
152 | */ | |
153 | void channel(channel_type const& ch) | |
154 | { | |
155 | BOOST_LOG_EXPR_IF_MT(boost::log::aux::exclusive_lock_guard< threading_model > lock(this->get_threading_model());) | |
156 | m_ChannelAttr.set(ch); | |
157 | } | |
158 | ||
159 | protected: | |
160 | /*! | |
161 | * Channel attribute accessor | |
162 | */ | |
163 | channel_attribute const& get_channel_attribute() const { return m_ChannelAttr; } | |
164 | ||
165 | /*! | |
166 | * Unlocked \c open_record | |
167 | */ | |
168 | template< typename ArgsT > | |
169 | record open_record_unlocked(ArgsT const& args) | |
170 | { | |
171 | return open_record_with_channel_unlocked(args, args[keywords::channel | parameter::void_()]); | |
172 | } | |
173 | ||
174 | /*! | |
175 | * Unlocked swap | |
176 | */ | |
177 | void swap_unlocked(basic_channel_logger& that) | |
178 | { | |
179 | base_type::swap_unlocked(static_cast< base_type& >(that)); | |
180 | m_ChannelAttr.swap(that.m_ChannelAttr); | |
181 | } | |
182 | ||
183 | private: | |
184 | //! The \c open_record implementation for the case when the channel is specified in log statement | |
185 | template< typename ArgsT, typename T > | |
186 | record open_record_with_channel_unlocked(ArgsT const& args, T const& ch) | |
187 | { | |
188 | m_ChannelAttr.set(ch); | |
189 | return base_type::open_record_unlocked(args); | |
190 | } | |
191 | //! The \c open_record implementation for the case when the channel is not specified in log statement | |
192 | template< typename ArgsT > | |
193 | record open_record_with_channel_unlocked(ArgsT const& args, parameter::void_) | |
194 | { | |
195 | return base_type::open_record_unlocked(args); | |
196 | } | |
197 | }; | |
198 | ||
199 | /*! | |
200 | * \brief Channel support feature | |
201 | * | |
202 | * The logger with this feature automatically registers an attribute with the specified | |
203 | * on construction value, which is a channel name. The channel name can be modified | |
204 | * through the logger life time, either by calling the \c channel method or by specifying | |
205 | * the name in the logging statement. | |
206 | * | |
207 | * The type of the channel name can be customized by providing it as a template parameter | |
208 | * to the feature template. By default, a string will be used. | |
209 | */ | |
210 | template< typename ChannelT = std::string > | |
211 | struct channel | |
212 | { | |
213 | template< typename BaseT > | |
214 | struct apply | |
215 | { | |
216 | typedef basic_channel_logger< | |
217 | BaseT, | |
218 | ChannelT | |
219 | > type; | |
220 | }; | |
221 | }; | |
222 | ||
223 | } // namespace sources | |
224 | ||
225 | BOOST_LOG_CLOSE_NAMESPACE // namespace log | |
226 | ||
227 | } // namespace boost | |
228 | ||
229 | //! The macro allows to put a record with a specific channel name into log | |
230 | #define BOOST_LOG_STREAM_CHANNEL(logger, chan)\ | |
231 | BOOST_LOG_STREAM_WITH_PARAMS((logger), (::boost::log::keywords::channel = (chan))) | |
232 | ||
233 | #ifndef BOOST_LOG_NO_SHORTHAND_NAMES | |
234 | ||
235 | //! An equivalent to BOOST_LOG_STREAM_CHANNEL(logger, chan) | |
236 | #define BOOST_LOG_CHANNEL(logger, chan) BOOST_LOG_STREAM_CHANNEL(logger, chan) | |
237 | ||
238 | #endif // BOOST_LOG_NO_SHORTHAND_NAMES | |
239 | ||
240 | #include <boost/log/detail/footer.hpp> | |
241 | ||
242 | #endif // BOOST_LOG_SOURCES_CHANNEL_FEATURE_HPP_INCLUDED_ |