Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file formatter_parser.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 07.04.2008 Chris@16: * Chris@16: * The header contains definition of a formatter parser function, along with facilities to Chris@16: * add support for custom formatters. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: /*! Chris@16: * Formatter factory base interface. Chris@16: */ Chris@16: template< typename CharT > Chris@16: struct formatter_factory Chris@16: { Chris@16: //! Character type Chris@16: typedef CharT char_type; Chris@16: //! String type Chris@16: typedef std::basic_string< char_type > string_type; Chris@16: //! The formatter function object Chris@16: typedef basic_formatter< char_type > formatter_type; Chris@16: /*! Chris@16: * Type of the map of formatter factory arguments [argument name -> argument value]. Chris@16: * This type of maps will be passed to formatter factories on attempt to create a formatter. Chris@16: */ Chris@16: typedef std::map< string_type, string_type > args_map; Chris@16: Chris@16: /*! Chris@16: * Default constructor Chris@16: */ Chris@16: BOOST_DEFAULTED_FUNCTION(formatter_factory(), {}) Chris@16: Chris@16: /*! Chris@16: * Virtual destructor Chris@16: */ Chris@16: virtual ~formatter_factory() {} Chris@16: Chris@16: /*! Chris@16: * The function creates a formatter for the specified attribute. Chris@16: * Chris@16: * \param name Attribute name Chris@16: * \param args Formatter arguments Chris@16: */ Chris@16: virtual formatter_type create_formatter(attribute_name const& name, args_map const& args) = 0; Chris@16: Chris@16: BOOST_DELETED_FUNCTION(formatter_factory(formatter_factory const&)) Chris@16: BOOST_DELETED_FUNCTION(formatter_factory& operator= (formatter_factory const&)) Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * Base class for formatter factories. This class provides default implementation of formatter expressions for Chris@16: * types supporting stream output. The factory does not take into account any additional parameters that may be specified. Chris@16: */ Chris@16: template< typename CharT, typename AttributeValueT > Chris@16: class basic_formatter_factory : Chris@16: public formatter_factory< CharT > Chris@16: { Chris@16: private: Chris@16: typedef formatter_factory< CharT > base_type; Chris@16: Chris@16: public: Chris@16: //! Attribute value type Chris@16: typedef AttributeValueT value_type; Chris@16: // Type imports from the base class Chris@16: typedef typename base_type::formatter_type formatter_type; Chris@16: typedef typename base_type::args_map args_map; Chris@16: Chris@16: /*! Chris@16: * The function creates a formatter for the specified attribute. Chris@16: * Chris@16: * \param name Attribute name Chris@16: * \param args Formatter arguments Chris@16: */ Chris@16: formatter_type create_formatter(attribute_name const& name, args_map const& args) Chris@16: { Chris@16: return formatter_type(expressions::stream << expressions::attr< value_type >(name)); Chris@16: } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * \brief The function registers a user-defined formatter factory Chris@16: * Chris@16: * The function registers a user-defined formatter factory. The registered factory function will be Chris@16: * called when the formatter parser detects the specified attribute name in the formatter string. Chris@16: * Chris@16: * \pre !!attr_name && !!factory. Chris@16: * Chris@16: * \param attr_name Attribute name Chris@16: * \param factory Pointer to the formatter factory Chris@16: */ Chris@16: template< typename CharT > Chris@16: BOOST_LOG_SETUP_API void register_formatter_factory( Chris@16: attribute_name const& attr_name, shared_ptr< formatter_factory< CharT > > const& factory); Chris@16: Chris@16: /*! Chris@16: * \brief The function registers a user-defined formatter factory Chris@16: * Chris@16: * The function registers a user-defined formatter factory. The registered factory function will be Chris@16: * called when the formatter parser detects the specified attribute name in the formatter string. Chris@16: * Chris@16: * \pre !!attr_name && !!factory. Chris@16: * Chris@16: * \param attr_name Attribute name Chris@16: * \param factory Pointer to the formatter factory Chris@16: */ Chris@16: template< typename FactoryT > Chris@16: inline typename enable_if< Chris@16: is_base_and_derived< formatter_factory< typename FactoryT::char_type >, FactoryT > Chris@16: >::type register_formatter_factory(attribute_name const& attr_name, shared_ptr< FactoryT > const& factory) Chris@16: { Chris@16: typedef formatter_factory< typename FactoryT::char_type > factory_base; Chris@16: register_formatter_factory(attr_name, boost::static_pointer_cast< factory_base >(factory)); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \brief The function registers a simple formatter factory Chris@16: * Chris@16: * The function registers a simple formatter factory. The registered factory will generate formatters Chris@16: * that will be equivalent to the log::expressions::attr formatter (i.e. that will use the Chris@16: * native \c operator<< to format the attribute value). The factory does not use any arguments from the format string, Chris@16: * if specified. Chris@16: * Chris@16: * \pre !!attr_name. Chris@16: * Chris@16: * \param attr_name Attribute name Chris@16: */ Chris@16: template< typename AttributeValueT, typename CharT > Chris@16: inline void register_simple_formatter_factory(attribute_name const& attr_name) Chris@16: { Chris@16: shared_ptr< formatter_factory< CharT > > factory = Chris@16: boost::make_shared< basic_formatter_factory< CharT, AttributeValueT > >(); Chris@16: register_formatter_factory(attr_name, factory); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function parses a formatter from the sequence of characters Chris@16: * Chris@16: * \pre begin <= end, both pointers must not be NULL Chris@16: * \param begin Pointer to the first character of the sequence Chris@16: * \param end Pointer to the after-the-last character of the sequence Chris@16: * \return The parsed formatter. Chris@16: * Chris@16: * \b Throws: An std::exception-based exception, if a formatter cannot be recognized in the character sequence. Chris@16: */ Chris@16: template< typename CharT > Chris@16: BOOST_LOG_SETUP_API basic_formatter< CharT > parse_formatter(const CharT* begin, const CharT* end); Chris@16: Chris@16: /*! Chris@16: * The function parses a formatter from the string Chris@16: * Chris@16: * \param str A string that contains format description Chris@16: * \return The parsed formatter. Chris@16: * Chris@16: * \b Throws: An std::exception-based exception, if a formatter cannot be recognized in the character sequence. Chris@16: */ Chris@16: template< typename CharT, typename TraitsT, typename AllocatorT > Chris@16: inline basic_formatter< CharT > parse_formatter(std::basic_string< CharT, TraitsT, AllocatorT > const& str) Chris@16: { Chris@16: const CharT* p = str.c_str(); Chris@16: return parse_formatter(p, p + str.size()); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function parses a formatter from the string Chris@16: * Chris@16: * \pre str != NULL, str points to a zero-terminated string Chris@16: * \param str A string that contains format description. Chris@16: * \return The parsed formatter. Chris@16: * Chris@16: * \b Throws: An std::exception-based exception, if a formatter cannot be recognized in the character sequence. Chris@16: */ Chris@16: template< typename CharT > Chris@16: inline basic_formatter< CharT > parse_formatter(const CharT* str) Chris@16: { Chris@16: return parse_formatter(str, str + std::char_traits< CharT >::length(str)); Chris@16: } Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_