format.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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. //! Internal typedef for type categorization
  41. typedef void _is_boost_log_terminal;
  42. //! Character type
  43. typedef CharT char_type;
  44. //! Boost.Format formatter type
  45. typedef boost::log::aux::basic_format< char_type > format_type;
  46. //! String type
  47. typedef std::basic_string< char_type > string_type;
  48. //! Terminal result type
  49. typedef typename format_type::pump result_type;
  50. private:
  51. //! Formatter object
  52. mutable format_type m_format;
  53. public:
  54. //! Initializing constructor
  55. explicit format_terminal(const char_type* format) : m_format(format) {}
  56. //! Invokation operator
  57. template< typename ContextT >
  58. result_type operator() (ContextT const& ctx) const
  59. {
  60. return m_format.make_pump(fusion::at_c< 1 >(phoenix::env(ctx).args()));
  61. }
  62. BOOST_DELETED_FUNCTION(format_terminal())
  63. };
  64. /*!
  65. * The function generates a terminal node in a template expression. The node will perform log record formatting
  66. * according to the provided format string.
  67. */
  68. template< typename CharT >
  69. BOOST_FORCEINLINE phoenix::actor< format_terminal< CharT > > format(const CharT* fmt)
  70. {
  71. typedef format_terminal< CharT > terminal_type;
  72. phoenix::actor< terminal_type > act = {{ terminal_type(fmt) }};
  73. return act;
  74. }
  75. /*!
  76. * The function generates a terminal node in a template expression. The node will perform log record formatting
  77. * according to the provided format string.
  78. */
  79. template< typename CharT, typename TraitsT, typename AllocatorT >
  80. BOOST_FORCEINLINE phoenix::actor< format_terminal< CharT > > format(std::basic_string< CharT, TraitsT, AllocatorT > const& fmt)
  81. {
  82. typedef format_terminal< CharT > terminal_type;
  83. phoenix::actor< terminal_type > act = {{ terminal_type(fmt.c_str()) }};
  84. return act;
  85. }
  86. } // namespace expressions
  87. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  88. #ifndef BOOST_LOG_DOXYGEN_PASS
  89. namespace phoenix {
  90. namespace result_of {
  91. template< typename CharT >
  92. struct is_nullary< custom_terminal< boost::log::expressions::format_terminal< CharT > > > :
  93. public mpl::false_
  94. {
  95. };
  96. } // namespace result_of
  97. } // namespace phoenix
  98. #endif
  99. } // namespace boost
  100. #include <boost/log/detail/footer.hpp>
  101. #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_FORMAT_HPP_INCLUDED_