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