format.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 formatters/format.hpp
  9. * \author Andrey Semashev
  10. * \date 15.11.2012
  11. *
  12. * The header contains a generic log record formatter function.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_FORMAT_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_FORMATTERS_FORMAT_HPP_INCLUDED_
  16. #include <string>
  17. #include <boost/mpl/bool.hpp>
  18. #include <boost/phoenix/core/actor.hpp>
  19. #include <boost/phoenix/core/terminal_fwd.hpp>
  20. #include <boost/phoenix/core/is_nullary.hpp>
  21. #include <boost/phoenix/core/environment.hpp>
  22. #include <boost/fusion/sequence/intrinsic/at_c.hpp>
  23. #include <boost/log/detail/config.hpp>
  24. #include <boost/log/detail/format.hpp>
  25. #include <boost/log/detail/custom_terminal_spec.hpp>
  26. #include <boost/log/detail/header.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. namespace boost {
  31. BOOST_LOG_OPEN_NAMESPACE
  32. namespace expressions {
  33. /*!
  34. * \brief Template expressions terminal node with Boost.Format-like formatter
  35. */
  36. template< typename CharT >
  37. class format_terminal
  38. {
  39. public:
  40. #ifndef BOOST_LOG_DOXYGEN_PASS
  41. //! Internal typedef for type categorization
  42. typedef void _is_boost_log_terminal;
  43. #endif
  44. //! Character type
  45. typedef CharT char_type;
  46. //! Boost.Format formatter type
  47. typedef boost::log::aux::basic_format< char_type > format_type;
  48. //! String type
  49. typedef std::basic_string< char_type > string_type;
  50. //! Terminal result type
  51. typedef typename format_type::pump result_type;
  52. private:
  53. //! Formatter object
  54. mutable format_type m_format;
  55. public:
  56. //! Initializing constructor
  57. explicit format_terminal(const char_type* format) : m_format(format) {}
  58. //! Invokation operator
  59. template< typename ContextT >
  60. result_type operator() (ContextT const& ctx) const
  61. {
  62. return m_format.make_pump(fusion::at_c< 1 >(phoenix::env(ctx).args()));
  63. }
  64. BOOST_DELETED_FUNCTION(format_terminal())
  65. };
  66. /*!
  67. * The function generates a terminal node in a template expression. The node will perform log record formatting
  68. * according to the provided format string.
  69. */
  70. template< typename CharT >
  71. BOOST_FORCEINLINE phoenix::actor< format_terminal< CharT > > format(const CharT* fmt)
  72. {
  73. typedef format_terminal< CharT > terminal_type;
  74. phoenix::actor< terminal_type > act = {{ terminal_type(fmt) }};
  75. return act;
  76. }
  77. /*!
  78. * The function generates a terminal node in a template expression. The node will perform log record formatting
  79. * according to the provided format string.
  80. */
  81. template< typename CharT, typename TraitsT, typename AllocatorT >
  82. BOOST_FORCEINLINE phoenix::actor< format_terminal< CharT > > format(std::basic_string< CharT, TraitsT, AllocatorT > const& fmt)
  83. {
  84. typedef format_terminal< CharT > terminal_type;
  85. phoenix::actor< terminal_type > act = {{ terminal_type(fmt.c_str()) }};
  86. return act;
  87. }
  88. } // namespace expressions
  89. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  90. #ifndef BOOST_LOG_DOXYGEN_PASS
  91. namespace phoenix {
  92. namespace result_of {
  93. template< typename CharT >
  94. struct is_nullary< custom_terminal< boost::log::expressions::format_terminal< CharT > > > :
  95. public mpl::false_
  96. {
  97. };
  98. } // namespace result_of
  99. } // namespace phoenix
  100. #endif
  101. } // namespace boost
  102. #include <boost/log/detail/footer.hpp>
  103. #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_FORMAT_HPP_INCLUDED_