formatter_parser.hpp 7.3 KB

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