date_time.hpp 12 KB

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