• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/date_time.hpp
9  * \author Andrey Semashev
10  * \date   16.09.2012
11  *
12  * The header contains a formatter function for date and time attribute values.
13  */
14 
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_
17 
18 #include <string>
19 #include <boost/move/core.hpp>
20 #include <boost/move/utility_core.hpp>
21 #include <boost/phoenix/core/actor.hpp>
22 #include <boost/phoenix/core/terminal_fwd.hpp>
23 #include <boost/phoenix/core/is_nullary.hpp>
24 #include <boost/phoenix/core/environment.hpp>
25 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
26 #include <boost/log/detail/config.hpp>
27 #include <boost/log/attributes/attribute_name.hpp>
28 #include <boost/log/attributes/fallback_policy.hpp>
29 #include <boost/log/attributes/value_visitation.hpp>
30 #include <boost/log/detail/light_function.hpp>
31 #include <boost/log/detail/date_time_fmt_gen_traits_fwd.hpp>
32 #include <boost/log/detail/custom_terminal_spec.hpp>
33 #include <boost/log/detail/attr_output_terminal.hpp>
34 #include <boost/log/expressions/attr_fwd.hpp>
35 #include <boost/log/expressions/keyword_fwd.hpp>
36 #include <boost/log/utility/formatting_ostream.hpp>
37 #include <boost/log/utility/functional/bind.hpp>
38 #include <boost/log/detail/header.hpp>
39 
40 #ifdef BOOST_HAS_PRAGMA_ONCE
41 #pragma once
42 #endif
43 
44 namespace boost {
45 
46 BOOST_LOG_OPEN_NAMESPACE
47 
48 namespace expressions {
49 
50 /*!
51  * Date and time formatter terminal.
52  */
53 template< typename T, typename FallbackPolicyT, typename CharT >
54 class format_date_time_terminal
55 {
56 public:
57 #ifndef BOOST_LOG_DOXYGEN_PASS
58     //! Internal typedef for type categorization
59     typedef void _is_boost_log_terminal;
60 #endif
61 
62     //! Attribute value type
63     typedef T value_type;
64     //! Fallback policy
65     typedef FallbackPolicyT fallback_policy;
66     //! Character type
67     typedef CharT char_type;
68     //! String type
69     typedef std::basic_string< char_type > string_type;
70     //! Formatting stream type
71     typedef basic_formatting_ostream< char_type > stream_type;
72 
73     //! Formatter function
74     typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
75 
76     //! Function result type
77     typedef string_type result_type;
78 
79 private:
80     //! Formatter generator traits
81     typedef aux::date_time_formatter_generator_traits< value_type, char_type > formatter_generator;
82     //! Attribute value visitor invoker
83     typedef value_visitor_invoker< value_type, fallback_policy > visitor_invoker_type;
84 
85 private:
86     //! Attribute name
87     attribute_name m_name;
88     //! Formattr function
89     formatter_function_type m_formatter;
90     //! Attribute value visitor invoker
91     visitor_invoker_type m_visitor_invoker;
92 
93 public:
94     //! Initializing constructor
format_date_time_terminal(attribute_name const & name,fallback_policy const & fallback,string_type const & format)95     format_date_time_terminal(attribute_name const& name, fallback_policy const& fallback, string_type const& format) :
96         m_name(name), m_formatter(formatter_generator::parse(format)), m_visitor_invoker(fallback)
97     {
98     }
99     //! Copy constructor
format_date_time_terminal(format_date_time_terminal const & that)100     format_date_time_terminal(format_date_time_terminal const& that) :
101         m_name(that.m_name), m_formatter(that.m_formatter), m_visitor_invoker(that.m_visitor_invoker)
102     {
103     }
104 
105     //! Returns attribute name
get_name() const106     attribute_name get_name() const
107     {
108         return m_name;
109     }
110 
111     //! Returns fallback policy
get_fallback_policy() const112     fallback_policy const& get_fallback_policy() const
113     {
114         return m_visitor_invoker.get_fallback_policy();
115     }
116 
117     //! Retruns formatter function
get_formatter_function() const118     formatter_function_type const& get_formatter_function() const
119     {
120         return m_formatter;
121     }
122 
123     //! Invokation operator
124     template< typename ContextT >
operator ()(ContextT const & ctx)125     result_type operator() (ContextT const& ctx)
126     {
127         string_type str;
128         stream_type strm(str);
129         m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type&, stream_type& >(m_formatter, strm));
130         strm.flush();
131         return BOOST_LOG_NRVO_RESULT(str);
132     }
133 
134     //! Invokation operator
135     template< typename ContextT >
operator ()(ContextT const & ctx) const136     result_type operator() (ContextT const& ctx) const
137     {
138         string_type str;
139         stream_type strm(str);
140         m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type const&, stream_type& >(m_formatter, strm));
141         strm.flush();
142         return BOOST_LOG_NRVO_RESULT(str);
143     }
144 
145     BOOST_DELETED_FUNCTION(format_date_time_terminal())
146 };
147 
148 /*!
149  * Date and time formatter actor.
150  */
151 template< typename T, typename FallbackPolicyT, typename CharT, template< typename > class ActorT = phoenix::actor >
152 class format_date_time_actor :
153     public ActorT< format_date_time_terminal< T, FallbackPolicyT, CharT > >
154 {
155 public:
156     //! Attribute value type
157     typedef T value_type;
158     //! Character type
159     typedef CharT char_type;
160     //! Fallback policy
161     typedef FallbackPolicyT fallback_policy;
162     //! Base terminal type
163     typedef format_date_time_terminal< value_type, fallback_policy, char_type > terminal_type;
164     //! Formatter function
165     typedef typename terminal_type::formatter_function_type formatter_function_type;
166 
167     //! Base actor type
168     typedef ActorT< terminal_type > base_type;
169 
170 public:
171     //! Initializing constructor
format_date_time_actor(base_type const & act)172     explicit format_date_time_actor(base_type const& act) : base_type(act)
173     {
174     }
175 
176     /*!
177      * \returns The attribute name
178      */
get_name() const179     attribute_name get_name() const
180     {
181         return this->proto_expr_.child0.get_name();
182     }
183 
184     /*!
185      * \returns Fallback policy
186      */
get_fallback_policy() const187     fallback_policy const& get_fallback_policy() const
188     {
189         return this->proto_expr_.child0.get_fallback_policy();
190     }
191 
192     /*!
193      * \returns Formatter function
194      */
get_formatter_function() const195     formatter_function_type const& get_formatter_function() const
196     {
197         return this->proto_expr_.child0.get_formatter_function();
198     }
199 };
200 
201 #ifndef BOOST_LOG_DOXYGEN_PASS
202 
203 #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
204     template< typename LeftExprT, typename T, typename FallbackPolicyT, typename CharT >\
205     BOOST_FORCEINLINE phoenix::actor< aux::attribute_output_terminal< phoenix::actor< LeftExprT >, T, FallbackPolicyT, typename format_date_time_actor< T, FallbackPolicyT, CharT >::formatter_function_type > >\
206     operator<< (phoenix::actor< LeftExprT > left_ref left, format_date_time_actor< T, FallbackPolicyT, CharT > right_ref right)\
207     {\
208         typedef aux::attribute_output_terminal< phoenix::actor< LeftExprT >, T, FallbackPolicyT, typename format_date_time_actor< T, FallbackPolicyT, CharT >::formatter_function_type > terminal_type;\
209         phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_name(), right.get_formatter_function(), right.get_fallback_policy()) }};\
210         return actor;\
211     }
212 
213 #include <boost/log/detail/generate_overloads.hpp>
214 
215 #undef BOOST_LOG_AUX_OVERLOAD
216 
217 #endif // BOOST_LOG_DOXYGEN_PASS
218 
219 /*!
220  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
221  * expression (stream output or \c format placeholder filler).
222  *
223  * \param name Attribute name
224  * \param format Format string
225  */
226 template< typename AttributeValueT, typename CharT >
format_date_time(attribute_name const & name,const CharT * format)227 BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, const CharT* format)
228 {
229     typedef format_date_time_actor< AttributeValueT, fallback_to_none, CharT > actor_type;
230     typedef typename actor_type::terminal_type terminal_type;
231     typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), format) }};
232     return actor_type(act);
233 }
234 
235 /*!
236  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
237  * expression (stream output or \c format placeholder filler).
238  *
239  * \param name Attribute name
240  * \param format Format string
241  */
242 template< typename AttributeValueT, typename CharT >
format_date_time(attribute_name const & name,std::basic_string<CharT> const & format)243 BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, std::basic_string< CharT > const& format)
244 {
245     typedef format_date_time_actor< AttributeValueT, fallback_to_none, CharT > actor_type;
246     typedef typename actor_type::terminal_type terminal_type;
247     typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), format) }};
248     return actor_type(act);
249 }
250 
251 /*!
252  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
253  * expression (stream output or \c format placeholder filler).
254  *
255  * \param keyword Attribute keyword
256  * \param format Format string
257  */
258 template< typename DescriptorT, template< typename > class ActorT, typename CharT >
259 BOOST_FORCEINLINE format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT >
format_date_time(attribute_keyword<DescriptorT,ActorT> const & keyword,const CharT * format)260 format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, const CharT* format)
261 {
262     typedef format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type;
263     typedef typename actor_type::terminal_type terminal_type;
264     typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), format) }};
265     return actor_type(act);
266 }
267 
268 /*!
269  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
270  * expression (stream output or \c format placeholder filler).
271  *
272  * \param keyword Attribute keyword
273  * \param format Format string
274  */
275 template< typename DescriptorT, template< typename > class ActorT, typename CharT >
276 BOOST_FORCEINLINE format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT >
format_date_time(attribute_keyword<DescriptorT,ActorT> const & keyword,std::basic_string<CharT> const & format)277 format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, std::basic_string< CharT > const& format)
278 {
279     typedef format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type;
280     typedef typename actor_type::terminal_type terminal_type;
281     typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), format) }};
282     return actor_type(act);
283 }
284 
285 /*!
286  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
287  * expression (stream output or \c format placeholder filler).
288  *
289  * \param placeholder Attribute placeholder
290  * \param format Format string
291  */
292 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT >
293 BOOST_FORCEINLINE format_date_time_actor< T, FallbackPolicyT, CharT, ActorT >
format_date_time(attribute_actor<T,FallbackPolicyT,TagT,ActorT> const & placeholder,const CharT * format)294 format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, const CharT* format)
295 {
296     typedef format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > actor_type;
297     typedef typename actor_type::terminal_type terminal_type;
298     typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), format) }};
299     return actor_type(act);
300 }
301 
302 /*!
303  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
304  * expression (stream output or \c format placeholder filler).
305  *
306  * \param placeholder Attribute placeholder
307  * \param format Format string
308  */
309 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT >
310 BOOST_FORCEINLINE format_date_time_actor< T, FallbackPolicyT, CharT, ActorT >
format_date_time(attribute_actor<T,FallbackPolicyT,TagT,ActorT> const & placeholder,std::basic_string<CharT> const & format)311 format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, std::basic_string< CharT > const& format)
312 {
313     typedef format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > actor_type;
314     typedef typename actor_type::terminal_type terminal_type;
315     typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), format) }};
316     return actor_type(act);
317 }
318 
319 } // namespace expressions
320 
321 BOOST_LOG_CLOSE_NAMESPACE // namespace log
322 
323 #ifndef BOOST_LOG_DOXYGEN_PASS
324 
325 namespace phoenix {
326 
327 namespace result_of {
328 
329 template< typename T, typename FallbackPolicyT, typename CharT >
330 struct is_nullary< custom_terminal< boost::log::expressions::format_date_time_terminal< T, FallbackPolicyT, CharT > > > :
331     public mpl::false_
332 {
333 };
334 
335 } // namespace result_of
336 
337 } // namespace phoenix
338 
339 #endif // BOOST_LOG_DOXYGEN_PASS
340 
341 } // namespace boost
342 
343 #include <boost/log/detail/footer.hpp>
344 
345 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_
346