date_time.hpp 12 KB

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