2 * Copyright Andrey Semashev 2007 - 2010.
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 formatter_parser.hpp
9 * \author Andrey Semashev
12 * The header contains definition of a formatter parser function, along with facilities to
13 * add support for custom formatters.
16 #if (defined(_MSC_VER) && _MSC_VER > 1000)
18 #endif // _MSC_VER > 1000
20 #ifndef BOOST_LOG_UTILITY_INIT_FORMATTER_PARSER_HPP_INCLUDED_
21 #define BOOST_LOG_UTILITY_INIT_FORMATTER_PARSER_HPP_INCLUDED_
23 #include <new> // std::nothrow
27 #include <boost/function/function2.hpp>
28 #include <boost/log/detail/setup_prologue.hpp>
29 #include <boost/log/core/record.hpp>
30 #include <boost/log/formatters/attr.hpp>
34 // 'm_A' : class 'A' needs to have dll-interface to be used by clients of class 'B'
35 #pragma warning(disable: 4251)
36 // non dll-interface class 'A' used as base for dll-interface class 'B'
37 #pragma warning(disable: 4275)
42 namespace BOOST_LOG_NAMESPACE {
45 * \brief Auxiliary formatter traits
47 * The structure generates commonly used types related to formatters and formatter factories.
49 template< typename CharT >
50 struct formatter_types
53 typedef CharT char_type;
55 typedef std::basic_string< char_type > string_type;
56 //! Output stream type
57 typedef std::basic_ostream< char_type > ostream_type;
59 typedef basic_record< char_type > record_type;
60 //! The formatter function object
61 typedef function2< void, ostream_type&, record_type const& > formatter_type;
64 * Type of the map of formatter factory arguments [argument name -> argument value].
65 * This type of maps will be passed to formatter factories on attempt to create a formatter.
67 typedef std::map< string_type, string_type > formatter_factory_args;
69 * \brief The type of a function object that constructs formatter instance
70 * \param name Attribute name
71 * \param args Formatter arguments
72 * \return The constructed formatter. The formatter must not be empty.
74 * \b Throws: An <tt>std::exception</tt>-based If an exception is thrown from the method,
75 * the exception is propagated to the parse_formatter caller
77 typedef function2< formatter_type, string_type const&, formatter_factory_args const& > formatter_factory;
78 //! Map of formatter factory function objects
79 typedef std::map< string_type, formatter_factory > factories_map;
85 * A simple formatter factory function. Provides an easy way to add user-defined types support to
86 * the formatter parser. The factory does not consider any formatter arguments, if specified,
87 * but produces the formatter that uses the native \c operator<< to format the attribute value.
89 * \param attr_name Attribute name to create formatter for.
91 template< typename CharT, typename AttributeValueT >
92 typename formatter_types< CharT >::formatter_type make_simple_formatter(
93 typename formatter_types< CharT >::string_type const& attr_name,
94 typename formatter_types< CharT >::formatter_factory_args const&)
96 typedef typename formatter_types< CharT >::formatter_type formatter_type;
97 return formatter_type(boost::log::formatters::attr< AttributeValueT >(attr_name, std::nothrow));
103 * \brief The function registers a user-defined formatter factory
105 * The function registers a user-defined formatter factory. The registered factory function will be
106 * called when the formatter parser detects the specified attribute name in the formatter string.
108 * \pre <tt>attr_name != NULL && !factory.empty()</tt>, \c attr_name must point to a zero-terminated sequence of characters.
110 * \param attr_name Attribute name
111 * \param factory Formatter factory function
113 template< typename CharT >
114 BOOST_LOG_SETUP_EXPORT void register_formatter_factory(
115 const CharT* attr_name,
116 #ifndef BOOST_LOG_BROKEN_TEMPLATE_DEFINITION_MATCHING
117 typename formatter_types< CharT >::formatter_factory const& factory
120 function2< void, std::basic_ostream< CharT >&, basic_record< CharT > const& >,
121 std::basic_string< CharT > const&,
122 std::map< std::basic_string< CharT >, std::basic_string< CharT > > const&
124 #endif // BOOST_LOG_BROKEN_TEMPLATE_DEFINITION_MATCHING
128 * \brief The function registers a user-defined formatter factory
130 * The function registers a user-defined formatter factory. The registered factory function will be
131 * called when the formatter parser detects the specified attribute name in the formatter string.
133 * \pre <tt>!factory.empty()</tt>
135 * \param attr_name Attribute name
136 * \param factory Formatter factory function
138 template< typename CharT, typename TraitsT, typename AllocatorT >
139 inline void register_formatter_factory(
140 std::basic_string< CharT, TraitsT, AllocatorT > const& attr_name,
141 typename formatter_types< CharT >::formatter_factory const& factory)
143 register_formatter_factory(attr_name.c_str(), factory);
147 * \brief The function registers a simple formatter factory
149 * The function registers a simple formatter factory. The registered factory will generate formatters
150 * that will be equivalent to the <tt>log::formatters::attr</tt> formatter (i.e. that will use the
151 * native \c operator<< to format the attribute value). The factory does not use any arguments,
154 * \pre <tt>attr_name != NULL</tt>, \c attr_name must point to a zero-terminated sequence of characters.
156 * \param attr_name Attribute name
158 template< typename AttributeValueT, typename CharT >
159 inline void register_simple_formatter_factory(const CharT* attr_name)
161 register_formatter_factory(attr_name, &boost::log::aux::make_simple_formatter< CharT, AttributeValueT >);
165 * \brief The function registers a simple formatter factory
167 * The function registers a simple formatter factory. The registered factory will generate formatters
168 * that will be equivalent to the <tt>log::formatters::attr</tt> formatter (i.e. that will use the
169 * native \c operator<< to format the attribute value). The factory does not use any arguments,
172 * \param attr_name Attribute name
174 template< typename AttributeValueT, typename CharT, typename TraitsT, typename AllocatorT >
175 inline void register_simple_formatter_factory(std::basic_string< CharT, TraitsT, AllocatorT > const& attr_name)
177 register_formatter_factory(attr_name.c_str(), &boost::log::aux::make_simple_formatter< CharT, AttributeValueT >);
181 * The function parses a formatter from the sequence of characters
183 * \pre <tt>begin <= end</tt>, both pointers must not be NULL
184 * \param begin Pointer to the first character of the sequence
185 * \param end Pointer to the after-the-last character of the sequence
186 * \return A function object that can be used as a formatter.
188 * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
190 template< typename CharT >
191 BOOST_LOG_SETUP_EXPORT
192 #ifndef BOOST_LOG_BROKEN_TEMPLATE_DEFINITION_MATCHING
193 typename formatter_types< CharT >::formatter_type
195 function2< void, std::basic_ostream< CharT >&, basic_record< CharT > const& >
197 parse_formatter(const CharT* begin, const CharT* end);
200 * The function parses a formatter from the string
202 * \param str A string that contains format description
203 * \return A function object that can be used as a formatter.
205 * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
207 template< typename CharT, typename TraitsT, typename AllocatorT >
208 inline typename formatter_types< CharT >::formatter_type
209 parse_formatter(std::basic_string< CharT, TraitsT, AllocatorT > const& str)
211 const CharT* p = str.c_str();
212 return parse_formatter(p, p + str.size());
216 * The function parses a formatter from the string
218 * \pre <tt>str != NULL</tt>, <tt>str</tt> points to a zero-terminated string
219 * \param str A string that contains format description.
220 * \return A function object that can be used as a formatter.
222 * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
224 template< typename CharT >
225 inline typename formatter_types< CharT >::formatter_type
226 parse_formatter(const CharT* str)
228 return parse_formatter(str, str + std::char_traits< CharT >::length(str));
239 #endif // BOOST_LOG_UTILITY_INIT_FORMATTER_PARSER_HPP_INCLUDED_