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 formatter_parser.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 07.04.2008
|
Chris@16
|
11 *
|
Chris@16
|
12 * The header contains definition of a formatter parser function, along with facilities to
|
Chris@16
|
13 * add support for custom formatters.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@16
|
19 #include <iosfwd>
|
Chris@16
|
20 #include <map>
|
Chris@16
|
21 #include <string>
|
Chris@16
|
22 #include <boost/smart_ptr/shared_ptr.hpp>
|
Chris@16
|
23 #include <boost/smart_ptr/make_shared_object.hpp>
|
Chris@16
|
24 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
25 #include <boost/type_traits/is_base_and_derived.hpp>
|
Chris@16
|
26 #include <boost/log/detail/setup_config.hpp>
|
Chris@16
|
27 #include <boost/log/attributes/attribute_name.hpp>
|
Chris@16
|
28 #include <boost/log/core/record.hpp>
|
Chris@16
|
29 #include <boost/log/expressions/formatter.hpp>
|
Chris@16
|
30 #include <boost/log/expressions/attr.hpp>
|
Chris@16
|
31 #include <boost/log/expressions/formatters/stream.hpp>
|
Chris@16
|
32 #include <boost/log/detail/header.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
35 #pragma once
|
Chris@16
|
36 #endif
|
Chris@16
|
37
|
Chris@16
|
38 namespace boost {
|
Chris@16
|
39
|
Chris@16
|
40 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
41
|
Chris@16
|
42 /*!
|
Chris@16
|
43 * Formatter factory base interface.
|
Chris@16
|
44 */
|
Chris@16
|
45 template< typename CharT >
|
Chris@16
|
46 struct formatter_factory
|
Chris@16
|
47 {
|
Chris@16
|
48 //! Character type
|
Chris@16
|
49 typedef CharT char_type;
|
Chris@16
|
50 //! String type
|
Chris@16
|
51 typedef std::basic_string< char_type > string_type;
|
Chris@16
|
52 //! The formatter function object
|
Chris@16
|
53 typedef basic_formatter< char_type > formatter_type;
|
Chris@16
|
54 /*!
|
Chris@16
|
55 * Type of the map of formatter factory arguments [argument name -> argument value].
|
Chris@16
|
56 * This type of maps will be passed to formatter factories on attempt to create a formatter.
|
Chris@16
|
57 */
|
Chris@16
|
58 typedef std::map< string_type, string_type > args_map;
|
Chris@16
|
59
|
Chris@16
|
60 /*!
|
Chris@16
|
61 * Default constructor
|
Chris@16
|
62 */
|
Chris@16
|
63 BOOST_DEFAULTED_FUNCTION(formatter_factory(), {})
|
Chris@16
|
64
|
Chris@16
|
65 /*!
|
Chris@16
|
66 * Virtual destructor
|
Chris@16
|
67 */
|
Chris@16
|
68 virtual ~formatter_factory() {}
|
Chris@16
|
69
|
Chris@16
|
70 /*!
|
Chris@16
|
71 * The function creates a formatter for the specified attribute.
|
Chris@16
|
72 *
|
Chris@16
|
73 * \param name Attribute name
|
Chris@16
|
74 * \param args Formatter arguments
|
Chris@16
|
75 */
|
Chris@16
|
76 virtual formatter_type create_formatter(attribute_name const& name, args_map const& args) = 0;
|
Chris@16
|
77
|
Chris@16
|
78 BOOST_DELETED_FUNCTION(formatter_factory(formatter_factory const&))
|
Chris@16
|
79 BOOST_DELETED_FUNCTION(formatter_factory& operator= (formatter_factory const&))
|
Chris@16
|
80 };
|
Chris@16
|
81
|
Chris@16
|
82 /*!
|
Chris@16
|
83 * Base class for formatter factories. This class provides default implementation of formatter expressions for
|
Chris@16
|
84 * types supporting stream output. The factory does not take into account any additional parameters that may be specified.
|
Chris@16
|
85 */
|
Chris@16
|
86 template< typename CharT, typename AttributeValueT >
|
Chris@16
|
87 class basic_formatter_factory :
|
Chris@16
|
88 public formatter_factory< CharT >
|
Chris@16
|
89 {
|
Chris@16
|
90 private:
|
Chris@16
|
91 typedef formatter_factory< CharT > base_type;
|
Chris@16
|
92
|
Chris@16
|
93 public:
|
Chris@16
|
94 //! Attribute value type
|
Chris@16
|
95 typedef AttributeValueT value_type;
|
Chris@16
|
96 // Type imports from the base class
|
Chris@16
|
97 typedef typename base_type::formatter_type formatter_type;
|
Chris@16
|
98 typedef typename base_type::args_map args_map;
|
Chris@16
|
99
|
Chris@16
|
100 /*!
|
Chris@16
|
101 * The function creates a formatter for the specified attribute.
|
Chris@16
|
102 *
|
Chris@16
|
103 * \param name Attribute name
|
Chris@16
|
104 * \param args Formatter arguments
|
Chris@16
|
105 */
|
Chris@16
|
106 formatter_type create_formatter(attribute_name const& name, args_map const& args)
|
Chris@16
|
107 {
|
Chris@16
|
108 return formatter_type(expressions::stream << expressions::attr< value_type >(name));
|
Chris@16
|
109 }
|
Chris@16
|
110 };
|
Chris@16
|
111
|
Chris@16
|
112 /*!
|
Chris@16
|
113 * \brief The function registers a user-defined formatter factory
|
Chris@16
|
114 *
|
Chris@16
|
115 * The function registers a user-defined formatter factory. The registered factory function will be
|
Chris@16
|
116 * called when the formatter parser detects the specified attribute name in the formatter string.
|
Chris@16
|
117 *
|
Chris@16
|
118 * \pre <tt>!!attr_name && !!factory</tt>.
|
Chris@16
|
119 *
|
Chris@16
|
120 * \param attr_name Attribute name
|
Chris@16
|
121 * \param factory Pointer to the formatter factory
|
Chris@16
|
122 */
|
Chris@16
|
123 template< typename CharT >
|
Chris@16
|
124 BOOST_LOG_SETUP_API void register_formatter_factory(
|
Chris@16
|
125 attribute_name const& attr_name, shared_ptr< formatter_factory< CharT > > const& factory);
|
Chris@16
|
126
|
Chris@16
|
127 /*!
|
Chris@16
|
128 * \brief The function registers a user-defined formatter factory
|
Chris@16
|
129 *
|
Chris@16
|
130 * The function registers a user-defined formatter factory. The registered factory function will be
|
Chris@16
|
131 * called when the formatter parser detects the specified attribute name in the formatter string.
|
Chris@16
|
132 *
|
Chris@16
|
133 * \pre <tt>!!attr_name && !!factory</tt>.
|
Chris@16
|
134 *
|
Chris@16
|
135 * \param attr_name Attribute name
|
Chris@16
|
136 * \param factory Pointer to the formatter factory
|
Chris@16
|
137 */
|
Chris@16
|
138 template< typename FactoryT >
|
Chris@16
|
139 inline typename enable_if<
|
Chris@16
|
140 is_base_and_derived< formatter_factory< typename FactoryT::char_type >, FactoryT >
|
Chris@16
|
141 >::type register_formatter_factory(attribute_name const& attr_name, shared_ptr< FactoryT > const& factory)
|
Chris@16
|
142 {
|
Chris@16
|
143 typedef formatter_factory< typename FactoryT::char_type > factory_base;
|
Chris@16
|
144 register_formatter_factory(attr_name, boost::static_pointer_cast< factory_base >(factory));
|
Chris@16
|
145 }
|
Chris@16
|
146
|
Chris@16
|
147 /*!
|
Chris@16
|
148 * \brief The function registers a simple formatter factory
|
Chris@16
|
149 *
|
Chris@16
|
150 * The function registers a simple formatter factory. The registered factory will generate formatters
|
Chris@16
|
151 * that will be equivalent to the <tt>log::expressions::attr</tt> formatter (i.e. that will use the
|
Chris@16
|
152 * native \c operator<< to format the attribute value). The factory does not use any arguments from the format string,
|
Chris@16
|
153 * if specified.
|
Chris@16
|
154 *
|
Chris@16
|
155 * \pre <tt>!!attr_name</tt>.
|
Chris@16
|
156 *
|
Chris@16
|
157 * \param attr_name Attribute name
|
Chris@16
|
158 */
|
Chris@16
|
159 template< typename AttributeValueT, typename CharT >
|
Chris@16
|
160 inline void register_simple_formatter_factory(attribute_name const& attr_name)
|
Chris@16
|
161 {
|
Chris@16
|
162 shared_ptr< formatter_factory< CharT > > factory =
|
Chris@16
|
163 boost::make_shared< basic_formatter_factory< CharT, AttributeValueT > >();
|
Chris@16
|
164 register_formatter_factory(attr_name, factory);
|
Chris@16
|
165 }
|
Chris@16
|
166
|
Chris@16
|
167 /*!
|
Chris@16
|
168 * The function parses a formatter from the sequence of characters
|
Chris@16
|
169 *
|
Chris@16
|
170 * \pre <tt>begin <= end</tt>, both pointers must not be NULL
|
Chris@16
|
171 * \param begin Pointer to the first character of the sequence
|
Chris@16
|
172 * \param end Pointer to the after-the-last character of the sequence
|
Chris@16
|
173 * \return The parsed formatter.
|
Chris@16
|
174 *
|
Chris@16
|
175 * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
|
Chris@16
|
176 */
|
Chris@16
|
177 template< typename CharT >
|
Chris@16
|
178 BOOST_LOG_SETUP_API basic_formatter< CharT > parse_formatter(const CharT* begin, const CharT* end);
|
Chris@16
|
179
|
Chris@16
|
180 /*!
|
Chris@16
|
181 * The function parses a formatter from the string
|
Chris@16
|
182 *
|
Chris@16
|
183 * \param str A string that contains format description
|
Chris@16
|
184 * \return The parsed formatter.
|
Chris@16
|
185 *
|
Chris@16
|
186 * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
|
Chris@16
|
187 */
|
Chris@16
|
188 template< typename CharT, typename TraitsT, typename AllocatorT >
|
Chris@16
|
189 inline basic_formatter< CharT > parse_formatter(std::basic_string< CharT, TraitsT, AllocatorT > const& str)
|
Chris@16
|
190 {
|
Chris@16
|
191 const CharT* p = str.c_str();
|
Chris@16
|
192 return parse_formatter(p, p + str.size());
|
Chris@16
|
193 }
|
Chris@16
|
194
|
Chris@16
|
195 /*!
|
Chris@16
|
196 * The function parses a formatter from the string
|
Chris@16
|
197 *
|
Chris@16
|
198 * \pre <tt>str != NULL</tt>, <tt>str</tt> points to a zero-terminated string
|
Chris@16
|
199 * \param str A string that contains format description.
|
Chris@16
|
200 * \return The parsed formatter.
|
Chris@16
|
201 *
|
Chris@16
|
202 * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
|
Chris@16
|
203 */
|
Chris@16
|
204 template< typename CharT >
|
Chris@16
|
205 inline basic_formatter< CharT > parse_formatter(const CharT* str)
|
Chris@16
|
206 {
|
Chris@16
|
207 return parse_formatter(str, str + std::char_traits< CharT >::length(str));
|
Chris@16
|
208 }
|
Chris@16
|
209
|
Chris@16
|
210 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
211
|
Chris@16
|
212 } // namespace boost
|
Chris@16
|
213
|
Chris@16
|
214 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
215
|
Chris@16
|
216 #endif // BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_
|