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)
8 * \file attribute_mapping.hpp
9 * \author Andrey Semashev
12 * The header contains facilities that are used in different sinks to map attribute values
13 * used throughout the application to values used with the specific native logging API.
14 * These tools are mostly needed to map application severity levels on native levels,
15 * required by OS-specific sink backends.
18 #ifndef BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_
19 #define BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_
22 #include <boost/log/detail/config.hpp>
23 #include <boost/log/detail/tagged_integer.hpp>
24 #include <boost/log/core/record_view.hpp>
25 #include <boost/log/attributes/attribute_name.hpp>
26 #include <boost/log/attributes/attribute_value_set.hpp>
27 #include <boost/log/attributes/value_visitation.hpp>
28 #include <boost/log/detail/header.hpp>
30 #ifdef BOOST_HAS_PRAGMA_ONCE
36 BOOST_LOG_OPEN_NAMESPACE
40 //! Base class for attribute mapping function objects
41 template< typename MappedT >
45 typedef MappedT mapped_type;
47 typedef mapped_type result_type;
52 //! Attribute value visitor
53 template< typename MappedT >
54 struct direct_mapping_visitor
56 typedef void result_type;
57 typedef MappedT mapped_type;
59 explicit direct_mapping_visitor(mapped_type& extracted) :
60 m_Extracted(extracted)
63 template< typename T >
64 void operator() (T const& val) const
66 m_Extracted = mapped_type(val);
70 mapped_type& m_Extracted;
72 // Specialization for the tagged integer
73 template< typename IntT, typename TagT >
74 struct direct_mapping_visitor< boost::log::aux::tagged_integer< IntT, TagT > >
76 typedef void result_type;
77 typedef boost::log::aux::tagged_integer< IntT, TagT > mapped_type;
79 explicit direct_mapping_visitor(mapped_type& extracted) :
80 m_Extracted(extracted)
83 template< typename T >
84 void operator() (T const& val) const
86 mapped_type v = { static_cast< IntT >(val) };
91 mapped_type& m_Extracted;
97 * \brief Straightforward mapping
99 * This type of mapping assumes that attribute with a particular name always
100 * provides values that map directly onto the native values. The mapping
101 * simply returns the extracted attribute value converted to the native value.
103 template< typename MappedT, typename AttributeValueT = int >
104 class basic_direct_mapping :
105 public basic_mapping< MappedT >
108 typedef basic_direct_mapping< MappedT > base_type;
111 //! Attribute contained value type
112 typedef AttributeValueT attribute_value_type;
113 //! Mapped value type
114 typedef typename base_type::mapped_type mapped_type;
118 const attribute_name m_Name;
119 //! Visitor invoker for the attribute value
120 value_visitor_invoker< attribute_value_type > m_Invoker;
121 //! Default native value
122 mapped_type m_DefaultValue;
128 * \param name Attribute name
129 * \param default_value The default native value that is returned if the attribute value is not found
131 explicit basic_direct_mapping(attribute_name const& name, mapped_type const& default_value) :
133 m_DefaultValue(default_value)
138 * Extraction operator
140 * \param rec A log record to extract value from
141 * \return An extracted attribute value
143 mapped_type operator() (record_view const& rec) const
145 mapped_type res = m_DefaultValue;
146 aux::direct_mapping_visitor< mapped_type > vis(res);
147 m_Invoker(m_Name, rec.attribute_values(), vis);
153 * \brief Customizable mapping
155 * The class allows to setup a custom mapping between an attribute and native values.
156 * The mapping should be initialized similarly to the standard \c map container, by using
157 * indexing operator and assignment.
159 * \note Unlike many other components of the library, exact type of the attribute value
160 * must be specified in the template parameter \c AttributeValueT. Type sequences
163 template< typename MappedT, typename AttributeValueT = int >
164 class basic_custom_mapping :
165 public basic_mapping< MappedT >
168 typedef basic_mapping< MappedT > base_type;
171 //! Attribute contained value type
172 typedef AttributeValueT attribute_value_type;
173 //! Mapped value type
174 typedef typename base_type::mapped_type mapped_type;
180 typedef std::map< attribute_value_type, mapped_type > mapping_type;
181 //! Smart reference class for implementing insertion into the map
182 class reference_proxy;
183 friend class reference_proxy;
184 class reference_proxy
186 mapping_type& m_Mapping;
187 attribute_value_type m_Key;
191 reference_proxy(mapping_type& mapping, attribute_value_type const& key) : m_Mapping(mapping), m_Key(key) {}
193 reference_proxy const& operator= (mapped_type const& val) const
195 m_Mapping[m_Key] = val;
200 //! Attribute value visitor
202 friend struct visitor;
205 typedef void result_type;
207 visitor(mapping_type const& mapping, mapped_type& extracted) :
209 m_Extracted(extracted)
212 template< typename T >
213 void operator() (T const& val) const
215 typename mapping_type::const_iterator it = m_Mapping.find(val);
216 if (it != m_Mapping.end())
217 m_Extracted = it->second;
221 mapping_type const& m_Mapping;
222 mapped_type& m_Extracted;
229 const attribute_name m_Name;
230 //! Visitor invoker for the attribute value
231 value_visitor_invoker< attribute_value_type > m_Invoker;
232 //! Default native value
233 mapped_type m_DefaultValue;
234 //! Conversion mapping
235 mapping_type m_Mapping;
241 * \param name Attribute name
242 * \param default_value The default native value that is returned if the conversion cannot be performed
244 explicit basic_custom_mapping(attribute_name const& name, mapped_type const& default_value) :
246 m_DefaultValue(default_value)
250 * Extraction operator. Extracts the attribute value and attempts to map it onto
253 * \param rec A log record to extract value from
254 * \return A mapped value, if mapping was successful, or the default value if
255 * mapping did not succeed.
257 mapped_type operator() (record_view const& rec) const
259 mapped_type res = m_DefaultValue;
260 visitor vis(m_Mapping, res);
261 m_Invoker(m_Name, rec.attribute_values(), vis);
267 * \param key Attribute value to be mapped
268 * \return An object of unspecified type that allows to insert a new mapping through assignment.
269 * The \a key argument becomes the key attribute value, and the assigned value becomes the
270 * mapped native value.
272 #ifndef BOOST_LOG_DOXYGEN_PASS
273 reference_proxy operator[] (attribute_value_type const& key)
275 implementation_defined operator[] (attribute_value_type const& key)
278 return reference_proxy(m_Mapping, key);
284 BOOST_LOG_CLOSE_NAMESPACE // namespace log
288 #include <boost/log/detail/footer.hpp>
290 #endif // BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_