max_size_decorator.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright Andrey Semashev 2016.
  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/max_size_decorator.hpp
  9. * \author Andrey Semashev
  10. * \date 06.07.2016
  11. *
  12. * The header contains implementation of a string length limiting decorator.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_MAX_SIZE_DECORATOR_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_FORMATTERS_MAX_SIZE_DECORATOR_HPP_INCLUDED_
  16. #include <cstddef>
  17. #include <string>
  18. #include <boost/assert.hpp>
  19. #include <boost/mpl/bool.hpp>
  20. #include <boost/move/core.hpp>
  21. #include <boost/move/utility_core.hpp>
  22. #include <boost/core/addressof.hpp>
  23. #include <boost/phoenix/core/actor.hpp>
  24. #include <boost/phoenix/core/meta_grammar.hpp>
  25. #include <boost/phoenix/core/terminal_fwd.hpp>
  26. #include <boost/phoenix/core/is_nullary.hpp>
  27. #include <boost/phoenix/core/environment.hpp>
  28. #include <boost/phoenix/support/vector.hpp>
  29. #include <boost/fusion/sequence/intrinsic/at_c.hpp>
  30. #include <boost/type_traits/remove_cv.hpp>
  31. #include <boost/type_traits/remove_reference.hpp>
  32. #include <boost/log/detail/config.hpp>
  33. #include <boost/log/detail/custom_terminal_spec.hpp>
  34. #include <boost/log/utility/formatting_ostream.hpp>
  35. #include <boost/log/detail/header.hpp>
  36. #ifdef BOOST_HAS_PRAGMA_ONCE
  37. #pragma once
  38. #endif
  39. namespace boost {
  40. BOOST_LOG_OPEN_NAMESPACE
  41. namespace expressions {
  42. namespace aux {
  43. //! String size limiting decorator stream output terminal
  44. template< typename LeftT, typename SubactorT, typename CharT >
  45. class max_size_decorator_output_terminal
  46. {
  47. private:
  48. //! Self type
  49. typedef max_size_decorator_output_terminal< LeftT, SubactorT, CharT > this_type;
  50. public:
  51. #ifndef BOOST_LOG_DOXYGEN_PASS
  52. //! Internal typedef for type categorization
  53. typedef void _is_boost_log_terminal;
  54. #endif
  55. //! Character type
  56. typedef CharT char_type;
  57. //! String type
  58. typedef std::basic_string< char_type > string_type;
  59. //! String size type
  60. typedef std::size_t size_type;
  61. //! Adopted actor type
  62. typedef SubactorT subactor_type;
  63. //! Result type definition
  64. template< typename >
  65. struct result;
  66. template< typename ThisT, typename ContextT >
  67. struct result< ThisT(ContextT) >
  68. {
  69. typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
  70. typedef typename phoenix::evaluator::impl<
  71. typename LeftT::proto_base_expr&,
  72. context_type,
  73. phoenix::unused
  74. >::result_type type;
  75. };
  76. private:
  77. //! Left argument actor
  78. LeftT m_left;
  79. //! Adopted formatter actor
  80. subactor_type m_subactor;
  81. //! Max size of the formatted string produced by the adopted formatter
  82. size_type m_max_size;
  83. //! Overflow marker
  84. string_type m_overflow_marker;
  85. public:
  86. /*!
  87. * Initializing constructor. Creates decorator of the \a fmt formatter with the specified \a decorations.
  88. */
  89. max_size_decorator_output_terminal(LeftT const& left, subactor_type const& sub, size_type max_size, string_type const& overflow_marker = string_type()) :
  90. m_left(left), m_subactor(sub), m_max_size(max_size), m_overflow_marker(overflow_marker)
  91. {
  92. BOOST_ASSERT(overflow_marker.size() <= max_size);
  93. }
  94. /*!
  95. * Copy constructor
  96. */
  97. max_size_decorator_output_terminal(max_size_decorator_output_terminal const& that) :
  98. m_left(that.m_left), m_subactor(that.m_subactor), m_max_size(that.m_max_size), m_overflow_marker(that.m_overflow_marker)
  99. {
  100. }
  101. /*!
  102. * Invokation operator
  103. */
  104. template< typename ContextT >
  105. typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
  106. {
  107. // Flush the stream and keep the current write position in the target string
  108. typedef typename result< this_type(ContextT const&) >::type result_type;
  109. result_type strm = phoenix::eval(m_left, ctx);
  110. strm.flush();
  111. if (!strm.rdbuf()->storage_overflow())
  112. {
  113. const size_type old_max_size = strm.rdbuf()->max_size();
  114. const size_type start_pos = strm.rdbuf()->storage()->size(), max_size_left = strm.rdbuf()->storage()->max_size() - start_pos;
  115. strm.rdbuf()->max_size(start_pos + (m_max_size <= max_size_left ? m_max_size : max_size_left));
  116. try
  117. {
  118. // Invoke the adopted formatter
  119. phoenix::eval(m_subactor, ctx);
  120. // Flush the buffered characters and apply decorations
  121. strm.flush();
  122. if (strm.rdbuf()->storage_overflow())
  123. {
  124. if (!m_overflow_marker.empty())
  125. {
  126. // Free up space for the overflow marker
  127. strm.rdbuf()->max_size(strm.rdbuf()->max_size() - m_overflow_marker.size());
  128. // Append the marker
  129. strm.rdbuf()->max_size(strm.rdbuf()->storage()->max_size());
  130. strm.rdbuf()->storage_overflow(false);
  131. strm.rdbuf()->append(m_overflow_marker.data(), m_overflow_marker.size());
  132. }
  133. else
  134. {
  135. strm.rdbuf()->storage_overflow(false);
  136. }
  137. }
  138. // Restore the original size limit
  139. strm.rdbuf()->max_size(old_max_size);
  140. }
  141. catch (...)
  142. {
  143. strm.rdbuf()->storage_overflow(false);
  144. strm.rdbuf()->max_size(old_max_size);
  145. throw;
  146. }
  147. }
  148. return strm;
  149. }
  150. /*!
  151. * Invokation operator
  152. */
  153. template< typename ContextT >
  154. typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
  155. {
  156. // Flush the stream and keep the current write position in the target string
  157. typedef typename result< const this_type(ContextT const&) >::type result_type;
  158. result_type strm = phoenix::eval(m_left, ctx);
  159. strm.flush();
  160. if (!strm.rdbuf()->storage_overflow())
  161. {
  162. const size_type old_max_size = strm.rdbuf()->max_size();
  163. const size_type start_pos = strm.rdbuf()->storage()->size(), max_size_left = strm.rdbuf()->storage()->max_size() - start_pos;
  164. strm.rdbuf()->max_size(start_pos + (m_max_size <= max_size_left ? m_max_size : max_size_left));
  165. try
  166. {
  167. // Invoke the adopted formatter
  168. phoenix::eval(m_subactor, ctx);
  169. // Flush the buffered characters and apply decorations
  170. strm.flush();
  171. if (strm.rdbuf()->storage_overflow())
  172. {
  173. if (!m_overflow_marker.empty())
  174. {
  175. // Free up space for the overflow marker
  176. strm.rdbuf()->max_size(strm.rdbuf()->max_size() - m_overflow_marker.size());
  177. // Append the marker
  178. strm.rdbuf()->max_size(strm.rdbuf()->storage()->max_size());
  179. strm.rdbuf()->storage_overflow(false);
  180. strm.rdbuf()->append(m_overflow_marker.data(), m_overflow_marker.size());
  181. }
  182. else
  183. {
  184. strm.rdbuf()->storage_overflow(false);
  185. }
  186. }
  187. // Restore the original size limit
  188. strm.rdbuf()->max_size(old_max_size);
  189. }
  190. catch (...)
  191. {
  192. strm.rdbuf()->storage_overflow(false);
  193. strm.rdbuf()->max_size(old_max_size);
  194. throw;
  195. }
  196. }
  197. return strm;
  198. }
  199. BOOST_DELETED_FUNCTION(max_size_decorator_output_terminal())
  200. };
  201. } // namespace aux
  202. /*!
  203. * String size limiting decorator terminal class. This formatter allows to limit the maximum total length
  204. * of the strings generated by other formatters.
  205. *
  206. * The \c max_size_decorator_terminal class aggregates the formatter being decorated, the maximum string length
  207. * it can produce and an optional truncation marker string, which will be put at the end of the output if the limit is exceeded. Note that
  208. * the marker length is included in the limit and as such must not exceed it.
  209. * The \c max_size_decorator_terminal class is a formatter itself, so it can be used to construct
  210. * more complex formatters, including nesting decorators.
  211. */
  212. template< typename SubactorT, typename CharT >
  213. class max_size_decorator_terminal
  214. {
  215. private:
  216. //! Self type
  217. typedef max_size_decorator_terminal< SubactorT, CharT > this_type;
  218. public:
  219. #ifndef BOOST_LOG_DOXYGEN_PASS
  220. //! Internal typedef for type categorization
  221. typedef void _is_boost_log_terminal;
  222. #endif
  223. //! Character type
  224. typedef CharT char_type;
  225. //! String type
  226. typedef std::basic_string< char_type > string_type;
  227. //! String size type
  228. typedef std::size_t size_type;
  229. //! Stream type
  230. typedef basic_formatting_ostream< char_type > stream_type;
  231. //! Adopted actor type
  232. typedef SubactorT subactor_type;
  233. //! Result type definition
  234. typedef string_type result_type;
  235. private:
  236. //! Adopted formatter actor
  237. subactor_type m_subactor;
  238. //! Max size of the formatted string produced by the adopted formatter
  239. size_type m_max_size;
  240. //! Overflow marker
  241. string_type m_overflow_marker;
  242. public:
  243. /*!
  244. * Initializing constructor.
  245. */
  246. max_size_decorator_terminal(subactor_type const& sub, size_type max_size, string_type const& overflow_marker = string_type()) :
  247. m_subactor(sub), m_max_size(max_size), m_overflow_marker(overflow_marker)
  248. {
  249. BOOST_ASSERT(overflow_marker.size() <= max_size);
  250. }
  251. /*!
  252. * Copy constructor
  253. */
  254. max_size_decorator_terminal(max_size_decorator_terminal const& that) :
  255. m_subactor(that.m_subactor), m_max_size(that.m_max_size), m_overflow_marker(that.m_overflow_marker)
  256. {
  257. }
  258. /*!
  259. * \returns Adopted subactor
  260. */
  261. subactor_type const& get_subactor() const
  262. {
  263. return m_subactor;
  264. }
  265. /*!
  266. * \returns Max string size limit
  267. */
  268. size_type get_max_size() const
  269. {
  270. return m_max_size;
  271. }
  272. /*!
  273. * \returns Max string size limit
  274. */
  275. string_type const& get_overflow_marker() const
  276. {
  277. return m_overflow_marker;
  278. }
  279. /*!
  280. * Invokation operator
  281. */
  282. template< typename ContextT >
  283. result_type operator() (ContextT const& ctx)
  284. {
  285. string_type str;
  286. stream_type strm(str);
  287. strm.rdbuf()->max_size(m_max_size);
  288. // Invoke the adopted formatter
  289. typedef phoenix::vector3<
  290. subactor_type*,
  291. typename fusion::result_of::at_c<
  292. typename remove_cv<
  293. typename remove_reference<
  294. typename phoenix::result_of::env< ContextT const& >::type
  295. >::type
  296. >::type::args_type,
  297. 0
  298. >::type,
  299. stream_type&
  300. > env_type;
  301. env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm };
  302. phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx)));
  303. // Flush the buffered characters and see of overflow happened
  304. strm.flush();
  305. if (strm.rdbuf()->storage_overflow() && !m_overflow_marker.empty())
  306. {
  307. strm.rdbuf()->max_size(strm.rdbuf()->max_size() - m_overflow_marker.size());
  308. strm.rdbuf()->max_size(str.max_size());
  309. strm.rdbuf()->storage_overflow(false);
  310. strm.rdbuf()->append(m_overflow_marker.data(), m_overflow_marker.size());
  311. strm.flush();
  312. }
  313. return BOOST_LOG_NRVO_RESULT(str);
  314. }
  315. /*!
  316. * Invokation operator
  317. */
  318. template< typename ContextT >
  319. result_type operator() (ContextT const& ctx) const
  320. {
  321. string_type str;
  322. stream_type strm(str);
  323. strm.rdbuf()->max_size(m_max_size);
  324. // Invoke the adopted formatter
  325. typedef phoenix::vector3<
  326. const subactor_type*,
  327. typename fusion::result_of::at_c<
  328. typename remove_cv<
  329. typename remove_reference<
  330. typename phoenix::result_of::env< ContextT const& >::type
  331. >::type
  332. >::type::args_type,
  333. 0
  334. >::type,
  335. stream_type&
  336. > env_type;
  337. env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm };
  338. phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx)));
  339. // Flush the buffered characters and see of overflow happened
  340. strm.flush();
  341. if (strm.rdbuf()->storage_overflow())
  342. {
  343. strm.rdbuf()->max_size(strm.rdbuf()->max_size() - m_overflow_marker.size());
  344. strm.rdbuf()->max_size(str.max_size());
  345. strm.rdbuf()->storage_overflow(false);
  346. strm.rdbuf()->append(m_overflow_marker.data(), m_overflow_marker.size());
  347. strm.flush();
  348. }
  349. return BOOST_LOG_NRVO_RESULT(str);
  350. }
  351. BOOST_DELETED_FUNCTION(max_size_decorator_terminal())
  352. };
  353. /*!
  354. * Character decorator actor
  355. */
  356. template< typename SubactorT, typename CharT, template< typename > class ActorT = phoenix::actor >
  357. class max_size_decorator_actor :
  358. public ActorT< max_size_decorator_terminal< SubactorT, CharT > >
  359. {
  360. public:
  361. //! Base terminal type
  362. typedef max_size_decorator_terminal< SubactorT, CharT > terminal_type;
  363. //! Character type
  364. typedef typename terminal_type::char_type char_type;
  365. //! Base actor type
  366. typedef ActorT< terminal_type > base_type;
  367. public:
  368. //! Initializing constructor
  369. explicit max_size_decorator_actor(base_type const& act) : base_type(act)
  370. {
  371. }
  372. //! Returns reference to the terminal
  373. terminal_type const& get_terminal() const
  374. {
  375. return this->proto_expr_.child0;
  376. }
  377. };
  378. #ifndef BOOST_LOG_DOXYGEN_PASS
  379. #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
  380. template< typename LeftExprT, typename SubactorT, typename CharT, template< typename > class ActorT >\
  381. BOOST_FORCEINLINE phoenix::actor< aux::max_size_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, CharT > >\
  382. operator<< (phoenix::actor< LeftExprT > left_ref left, max_size_decorator_actor< SubactorT, CharT, ActorT > right_ref right)\
  383. {\
  384. typedef aux::max_size_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, CharT > terminal_type;\
  385. phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_terminal().get_subactor(), right.get_terminal().get_max_size(), right.get_terminal().get_overflow_marker()) }};\
  386. return actor;\
  387. }
  388. #include <boost/log/detail/generate_overloads.hpp>
  389. #undef BOOST_LOG_AUX_OVERLOAD
  390. #endif // BOOST_LOG_DOXYGEN_PASS
  391. namespace aux {
  392. template< typename CharT >
  393. class max_size_decorator_gen
  394. {
  395. private:
  396. typedef CharT char_type;
  397. typedef std::basic_string< char_type > string_type;
  398. typedef std::size_t size_type;
  399. private:
  400. size_type m_max_size;
  401. string_type m_overflow_marker;
  402. public:
  403. explicit max_size_decorator_gen(size_type max_size, string_type const& overflow_marker = string_type()) :
  404. m_max_size(max_size), m_overflow_marker(overflow_marker)
  405. {
  406. BOOST_ASSERT(overflow_marker.size() <= max_size);
  407. }
  408. template< typename SubactorT >
  409. BOOST_FORCEINLINE max_size_decorator_actor< SubactorT, char_type > operator[] (SubactorT const& subactor) const
  410. {
  411. typedef max_size_decorator_actor< SubactorT, char_type > result_type;
  412. typedef typename result_type::terminal_type terminal_type;
  413. typename result_type::base_type act = {{ terminal_type(subactor, m_max_size, m_overflow_marker) }};
  414. return result_type(act);
  415. }
  416. };
  417. } // namespace aux
  418. /*!
  419. * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
  420. * to construct the actual decorator.
  421. *
  422. * \param max_size The maximum number of characters (i.e. string element objects) that the decorated formatter can produce.
  423. */
  424. template< typename CharT >
  425. BOOST_FORCEINLINE aux::max_size_decorator_gen< CharT > max_size_decor(std::size_t max_size)
  426. {
  427. return aux::max_size_decorator_gen< CharT >(max_size);
  428. }
  429. /*!
  430. * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
  431. * to construct the actual decorator.
  432. *
  433. * \param max_size The maximum number of characters (i.e. string element objects) that the decorated formatter can produce.
  434. * \param overflow_marker The marker string which is appended to the output if the \a max_size limit is exceeded. Must be
  435. * a non-null pointer to a zero-terminated string.
  436. *
  437. * \pre The \a overflow_marker length must not exceed the \a max_size limit.
  438. */
  439. template< typename CharT >
  440. BOOST_FORCEINLINE aux::max_size_decorator_gen< CharT > max_size_decor(std::size_t max_size, const CharT* overflow_marker)
  441. {
  442. return aux::max_size_decorator_gen< CharT >(max_size, overflow_marker);
  443. }
  444. /*!
  445. * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
  446. * to construct the actual decorator.
  447. *
  448. * \param max_size The maximum number of characters (i.e. string element objects) that the decorated formatter can produce.
  449. * \param overflow_marker The marker string which is appended to the output if the \a max_size limit is exceeded.
  450. *
  451. * \pre The \a overflow_marker length must not exceed the \a max_size limit.
  452. */
  453. template< typename CharT >
  454. BOOST_FORCEINLINE aux::max_size_decorator_gen< CharT > max_size_decor(std::size_t max_size, std::basic_string< CharT > const& overflow_marker)
  455. {
  456. return aux::max_size_decorator_gen< CharT >(max_size, overflow_marker);
  457. }
  458. } // namespace expressions
  459. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  460. #ifndef BOOST_LOG_DOXYGEN_PASS
  461. namespace phoenix {
  462. namespace result_of {
  463. template< typename SubactorT, typename CharT >
  464. struct is_nullary< custom_terminal< boost::log::expressions::max_size_decorator_terminal< SubactorT, CharT > > > :
  465. public mpl::false_
  466. {
  467. };
  468. template< typename LeftT, typename SubactorT, typename CharT >
  469. struct is_nullary< custom_terminal< boost::log::expressions::aux::max_size_decorator_output_terminal< LeftT, SubactorT, CharT > > > :
  470. public mpl::false_
  471. {
  472. };
  473. } // namespace result_of
  474. } // namespace phoenix
  475. #endif
  476. } // namespace boost
  477. #include <boost/log/detail/footer.hpp>
  478. #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_MAX_SIZE_DECORATOR_HPP_INCLUDED_