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)
8 * \file formatters/date_time.hpp
9 * \author Andrey Semashev
12 * The header contains a formatter function for date and time attribute values.
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_
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>
40 #ifdef BOOST_HAS_PRAGMA_ONCE
46 BOOST_LOG_OPEN_NAMESPACE
48 namespace expressions {
51 * Date and time formatter terminal.
53 template< typename T, typename FallbackPolicyT, typename CharT >
54 class format_date_time_terminal
57 #ifndef BOOST_LOG_DOXYGEN_PASS
58 //! Internal typedef for type categorization
59 typedef void _is_boost_log_terminal;
62 //! Attribute value type
65 typedef FallbackPolicyT fallback_policy;
67 typedef CharT char_type;
69 typedef std::basic_string< char_type > string_type;
70 //! Formatting stream type
71 typedef basic_formatting_ostream< char_type > stream_type;
73 //! Formatter function
74 typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
76 //! Function result type
77 typedef string_type result_type;
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;
87 attribute_name m_name;
89 formatter_function_type m_formatter;
90 //! Attribute value visitor invoker
91 visitor_invoker_type m_visitor_invoker;
94 //! Initializing constructor
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)
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)
105 //! Returns attribute name
106 attribute_name get_name() const
111 //! Returns fallback policy
112 fallback_policy const& get_fallback_policy() const
114 return m_visitor_invoker.get_fallback_policy();
117 //! Retruns formatter function
118 formatter_function_type const& get_formatter_function() const
123 //! Invokation operator
124 template< typename ContextT >
125 result_type operator() (ContextT const& ctx)
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));
131 return BOOST_LOG_NRVO_RESULT(str);
134 //! Invokation operator
135 template< typename ContextT >
136 result_type operator() (ContextT const& ctx) const
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));
142 return BOOST_LOG_NRVO_RESULT(str);
145 BOOST_DELETED_FUNCTION(format_date_time_terminal())
149 * Date and time formatter actor.
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 > >
156 //! Attribute value type
157 typedef T value_type;
159 typedef CharT char_type;
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;
168 typedef ActorT< terminal_type > base_type;
171 //! Initializing constructor
172 explicit format_date_time_actor(base_type const& act) : base_type(act)
177 * \returns The attribute name
179 attribute_name get_name() const
181 return this->proto_expr_.child0.get_name();
185 * \returns Fallback policy
187 fallback_policy const& get_fallback_policy() const
189 return this->proto_expr_.child0.get_fallback_policy();
193 * \returns Formatter function
195 formatter_function_type const& get_formatter_function() const
197 return this->proto_expr_.child0.get_formatter_function();
201 #ifndef BOOST_LOG_DOXYGEN_PASS
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)\
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()) }};\
213 #include <boost/log/detail/generate_overloads.hpp>
215 #undef BOOST_LOG_AUX_OVERLOAD
217 #endif // BOOST_LOG_DOXYGEN_PASS
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).
223 * \param name Attribute name
224 * \param format Format string
226 template< typename AttributeValueT, typename CharT >
227 BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, const CharT* format)
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);
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).
239 * \param name Attribute name
240 * \param format Format string
242 template< typename AttributeValueT, typename CharT >
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)
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);
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).
255 * \param keyword Attribute keyword
256 * \param format Format string
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 >
260 format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, const CharT* format)
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);
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).
272 * \param keyword Attribute keyword
273 * \param format Format string
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 >
277 format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, std::basic_string< CharT > const& format)
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);
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).
289 * \param placeholder Attribute placeholder
290 * \param format Format string
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 >
294 format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, const CharT* format)
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);
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).
306 * \param placeholder Attribute placeholder
307 * \param format Format string
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 >
311 format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, std::basic_string< CharT > const& format)
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);
319 } // namespace expressions
321 BOOST_LOG_CLOSE_NAMESPACE // namespace log
323 #ifndef BOOST_LOG_DOXYGEN_PASS
327 namespace result_of {
329 template< typename T, typename FallbackPolicyT, typename CharT >
330 struct is_nullary< custom_terminal< boost::log::expressions::format_date_time_terminal< T, FallbackPolicyT, CharT > > > :
335 } // namespace result_of
337 } // namespace phoenix
339 #endif // BOOST_LOG_DOXYGEN_PASS
343 #include <boost/log/detail/footer.hpp>
345 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_