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)
8 * \file formatters/max_size_decorator.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of a string length limiting decorator.
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_MAX_SIZE_DECORATOR_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_MAX_SIZE_DECORATOR_HPP_INCLUDED_
20 #include <boost/assert.hpp>
21 #include <boost/mpl/bool.hpp>
22 #include <boost/move/core.hpp>
23 #include <boost/move/utility_core.hpp>
24 #include <boost/core/addressof.hpp>
25 #include <boost/phoenix/core/actor.hpp>
26 #include <boost/phoenix/core/meta_grammar.hpp>
27 #include <boost/phoenix/core/terminal_fwd.hpp>
28 #include <boost/phoenix/core/is_nullary.hpp>
29 #include <boost/phoenix/core/environment.hpp>
30 #include <boost/phoenix/support/vector.hpp>
31 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
32 #include <boost/type_traits/remove_cv.hpp>
33 #include <boost/type_traits/remove_reference.hpp>
34 #include <boost/log/detail/config.hpp>
35 #include <boost/log/detail/custom_terminal_spec.hpp>
36 #include <boost/log/utility/formatting_ostream.hpp>
37 #include <boost/log/detail/header.hpp>
39 #ifdef BOOST_HAS_PRAGMA_ONCE
45 BOOST_LOG_OPEN_NAMESPACE
47 namespace expressions {
51 //! String size limiting decorator stream output terminal
52 template< typename LeftT, typename SubactorT, typename CharT >
53 class max_size_decorator_output_terminal
57 typedef max_size_decorator_output_terminal< LeftT, SubactorT, CharT > this_type;
60 #ifndef BOOST_LOG_DOXYGEN_PASS
61 //! Internal typedef for type categorization
62 typedef void _is_boost_log_terminal;
66 typedef CharT char_type;
68 typedef std::basic_string< char_type > string_type;
70 typedef std::size_t size_type;
71 //! Adopted actor type
72 typedef SubactorT subactor_type;
74 //! Result type definition
78 template< typename ThisT, typename ContextT >
79 struct result< ThisT(ContextT) >
81 typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
82 typedef typename phoenix::evaluator::impl<
83 typename LeftT::proto_base_expr&,
90 //! Left argument actor
92 //! Adopted formatter actor
93 subactor_type m_subactor;
94 //! Max size of the formatted string produced by the adopted formatter
97 string_type m_overflow_marker;
101 * Initializing constructor. Creates decorator of the \a fmt formatter with the specified \a decorations.
103 max_size_decorator_output_terminal(LeftT const& left, subactor_type const& sub, size_type max_size, string_type const& overflow_marker = string_type()) :
104 m_left(left), m_subactor(sub), m_max_size(max_size), m_overflow_marker(overflow_marker)
106 BOOST_ASSERT(overflow_marker.size() <= max_size);
111 max_size_decorator_output_terminal(max_size_decorator_output_terminal const& that) :
112 m_left(that.m_left), m_subactor(that.m_subactor), m_max_size(that.m_max_size), m_overflow_marker(that.m_overflow_marker)
117 * Invokation operator
119 template< typename ContextT >
120 typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
122 // Flush the stream and keep the current write position in the target string
123 typedef typename result< this_type(ContextT const&) >::type result_type;
124 result_type strm = phoenix::eval(m_left, ctx);
127 if (!strm.rdbuf()->storage_overflow())
129 const size_type old_max_size = strm.rdbuf()->max_size();
130 const size_type start_pos = strm.rdbuf()->storage()->size(), max_size_left = strm.rdbuf()->storage()->max_size() - start_pos;
131 strm.rdbuf()->max_size(start_pos + (m_max_size <= max_size_left ? m_max_size : max_size_left));
135 // Invoke the adopted formatter
136 phoenix::eval(m_subactor, ctx);
138 // Flush the buffered characters and apply decorations
141 if (strm.rdbuf()->storage_overflow())
143 if (!m_overflow_marker.empty())
145 // Free up space for the overflow marker
146 strm.rdbuf()->max_size(strm.rdbuf()->max_size() - m_overflow_marker.size());
149 strm.rdbuf()->max_size(strm.rdbuf()->storage()->max_size());
150 strm.rdbuf()->storage_overflow(false);
151 strm.rdbuf()->append(m_overflow_marker.data(), m_overflow_marker.size());
155 strm.rdbuf()->storage_overflow(false);
159 // Restore the original size limit
160 strm.rdbuf()->max_size(old_max_size);
164 strm.rdbuf()->storage_overflow(false);
165 strm.rdbuf()->max_size(old_max_size);
174 * Invokation operator
176 template< typename ContextT >
177 typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
179 // Flush the stream and keep the current write position in the target string
180 typedef typename result< const this_type(ContextT const&) >::type result_type;
181 result_type strm = phoenix::eval(m_left, ctx);
184 if (!strm.rdbuf()->storage_overflow())
186 const size_type old_max_size = strm.rdbuf()->max_size();
187 const size_type start_pos = strm.rdbuf()->storage()->size(), max_size_left = strm.rdbuf()->storage()->max_size() - start_pos;
188 strm.rdbuf()->max_size(start_pos + (m_max_size <= max_size_left ? m_max_size : max_size_left));
192 // Invoke the adopted formatter
193 phoenix::eval(m_subactor, ctx);
195 // Flush the buffered characters and apply decorations
198 if (strm.rdbuf()->storage_overflow())
200 if (!m_overflow_marker.empty())
202 // Free up space for the overflow marker
203 strm.rdbuf()->max_size(strm.rdbuf()->max_size() - m_overflow_marker.size());
206 strm.rdbuf()->max_size(strm.rdbuf()->storage()->max_size());
207 strm.rdbuf()->storage_overflow(false);
208 strm.rdbuf()->append(m_overflow_marker.data(), m_overflow_marker.size());
212 strm.rdbuf()->storage_overflow(false);
216 // Restore the original size limit
217 strm.rdbuf()->max_size(old_max_size);
221 strm.rdbuf()->storage_overflow(false);
222 strm.rdbuf()->max_size(old_max_size);
230 BOOST_DELETED_FUNCTION(max_size_decorator_output_terminal())
236 * String size limiting decorator terminal class. This formatter allows to limit the maximum total length
237 * of the strings generated by other formatters.
239 * The \c max_size_decorator_terminal class aggregates the formatter being decorated, the maximum string length
240 * 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
241 * the marker length is included in the limit and as such must not exceed it.
242 * The \c max_size_decorator_terminal class is a formatter itself, so it can be used to construct
243 * more complex formatters, including nesting decorators.
245 template< typename SubactorT, typename CharT >
246 class max_size_decorator_terminal
250 typedef max_size_decorator_terminal< SubactorT, CharT > this_type;
253 #ifndef BOOST_LOG_DOXYGEN_PASS
254 //! Internal typedef for type categorization
255 typedef void _is_boost_log_terminal;
259 typedef CharT char_type;
261 typedef std::basic_string< char_type > string_type;
263 typedef std::size_t size_type;
265 typedef basic_formatting_ostream< char_type > stream_type;
266 //! Adopted actor type
267 typedef SubactorT subactor_type;
269 //! Result type definition
270 typedef string_type result_type;
273 //! Adopted formatter actor
274 subactor_type m_subactor;
275 //! Max size of the formatted string produced by the adopted formatter
276 size_type m_max_size;
278 string_type m_overflow_marker;
282 * Initializing constructor.
284 max_size_decorator_terminal(subactor_type const& sub, size_type max_size, string_type const& overflow_marker = string_type()) :
285 m_subactor(sub), m_max_size(max_size), m_overflow_marker(overflow_marker)
287 BOOST_ASSERT(overflow_marker.size() <= max_size);
292 max_size_decorator_terminal(max_size_decorator_terminal const& that) :
293 m_subactor(that.m_subactor), m_max_size(that.m_max_size), m_overflow_marker(that.m_overflow_marker)
298 * \returns Adopted subactor
300 subactor_type const& get_subactor() const
306 * \returns Max string size limit
308 size_type get_max_size() const
314 * \returns Max string size limit
316 string_type const& get_overflow_marker() const
318 return m_overflow_marker;
322 * Invokation operator
324 template< typename ContextT >
325 result_type operator() (ContextT const& ctx)
328 stream_type strm(str);
329 strm.rdbuf()->max_size(m_max_size);
331 // Invoke the adopted formatter
332 typedef phoenix::vector3<
334 typename fusion::result_of::at_c<
336 typename remove_reference<
337 typename phoenix::result_of::env< ContextT const& >::type
344 env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm };
345 phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx)));
347 // Flush the buffered characters and see of overflow happened
350 if (strm.rdbuf()->storage_overflow() && !m_overflow_marker.empty())
352 strm.rdbuf()->max_size(strm.rdbuf()->max_size() - m_overflow_marker.size());
353 strm.rdbuf()->max_size(str.max_size());
354 strm.rdbuf()->storage_overflow(false);
355 strm.rdbuf()->append(m_overflow_marker.data(), m_overflow_marker.size());
359 return BOOST_LOG_NRVO_RESULT(str);
363 * Invokation operator
365 template< typename ContextT >
366 result_type operator() (ContextT const& ctx) const
369 stream_type strm(str);
370 strm.rdbuf()->max_size(m_max_size);
372 // Invoke the adopted formatter
373 typedef phoenix::vector3<
374 const subactor_type*,
375 typename fusion::result_of::at_c<
377 typename remove_reference<
378 typename phoenix::result_of::env< ContextT const& >::type
385 env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm };
386 phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx)));
388 // Flush the buffered characters and see of overflow happened
391 if (strm.rdbuf()->storage_overflow())
393 strm.rdbuf()->max_size(strm.rdbuf()->max_size() - m_overflow_marker.size());
394 strm.rdbuf()->max_size(str.max_size());
395 strm.rdbuf()->storage_overflow(false);
396 strm.rdbuf()->append(m_overflow_marker.data(), m_overflow_marker.size());
400 return BOOST_LOG_NRVO_RESULT(str);
403 BOOST_DELETED_FUNCTION(max_size_decorator_terminal())
407 * Character decorator actor
409 template< typename SubactorT, typename CharT, template< typename > class ActorT = phoenix::actor >
410 class max_size_decorator_actor :
411 public ActorT< max_size_decorator_terminal< SubactorT, CharT > >
414 //! Base terminal type
415 typedef max_size_decorator_terminal< SubactorT, CharT > terminal_type;
417 typedef typename terminal_type::char_type char_type;
420 typedef ActorT< terminal_type > base_type;
423 //! Initializing constructor
424 explicit max_size_decorator_actor(base_type const& act) : base_type(act)
428 //! Returns reference to the terminal
429 terminal_type const& get_terminal() const
431 return this->proto_expr_.child0;
435 #ifndef BOOST_LOG_DOXYGEN_PASS
437 #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
438 template< typename LeftExprT, typename SubactorT, typename CharT, template< typename > class ActorT >\
439 BOOST_FORCEINLINE phoenix::actor< aux::max_size_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, CharT > >\
440 operator<< (phoenix::actor< LeftExprT > left_ref left, max_size_decorator_actor< SubactorT, CharT, ActorT > right_ref right)\
442 typedef aux::max_size_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, CharT > terminal_type;\
443 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()) }};\
447 #include <boost/log/detail/generate_overloads.hpp>
449 #undef BOOST_LOG_AUX_OVERLOAD
451 #endif // BOOST_LOG_DOXYGEN_PASS
455 template< typename CharT >
456 class max_size_decorator_gen
459 typedef CharT char_type;
460 typedef std::basic_string< char_type > string_type;
461 typedef std::size_t size_type;
464 size_type m_max_size;
465 string_type m_overflow_marker;
468 explicit max_size_decorator_gen(size_type max_size, string_type const& overflow_marker = string_type()) :
469 m_max_size(max_size), m_overflow_marker(overflow_marker)
471 BOOST_ASSERT(overflow_marker.size() <= max_size);
474 template< typename SubactorT >
475 BOOST_FORCEINLINE max_size_decorator_actor< SubactorT, char_type > operator[] (SubactorT const& subactor) const
477 typedef max_size_decorator_actor< SubactorT, char_type > result_type;
478 typedef typename result_type::terminal_type terminal_type;
479 typename result_type::base_type act = {{ terminal_type(subactor, m_max_size, m_overflow_marker) }};
480 return result_type(act);
487 * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
488 * to construct the actual decorator.
490 * \param max_size The maximum number of characters (i.e. string element objects) that the decorated formatter can produce.
492 template< typename CharT >
493 BOOST_FORCEINLINE aux::max_size_decorator_gen< CharT > max_size_decor(std::size_t max_size)
495 return aux::max_size_decorator_gen< CharT >(max_size);
499 * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
500 * to construct the actual decorator.
502 * \param max_size The maximum number of characters (i.e. string element objects) that the decorated formatter can produce.
503 * \param overflow_marker The marker string which is appended to the output if the \a max_size limit is exceeded. Must be
504 * a non-null pointer to a zero-terminated string.
506 * \pre The \a overflow_marker length must not exceed the \a max_size limit.
508 template< typename CharT >
509 BOOST_FORCEINLINE aux::max_size_decorator_gen< CharT > max_size_decor(std::size_t max_size, const CharT* overflow_marker)
511 return aux::max_size_decorator_gen< CharT >(max_size, overflow_marker);
515 * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
516 * to construct the actual decorator.
518 * \param max_size The maximum number of characters (i.e. string element objects) that the decorated formatter can produce.
519 * \param overflow_marker The marker string which is appended to the output if the \a max_size limit is exceeded.
521 * \pre The \a overflow_marker length must not exceed the \a max_size limit.
523 template< typename CharT >
524 BOOST_FORCEINLINE aux::max_size_decorator_gen< CharT > max_size_decor(std::size_t max_size, std::basic_string< CharT > const& overflow_marker)
526 return aux::max_size_decorator_gen< CharT >(max_size, overflow_marker);
529 } // namespace expressions
531 BOOST_LOG_CLOSE_NAMESPACE // namespace log
533 #ifndef BOOST_LOG_DOXYGEN_PASS
537 namespace result_of {
539 template< typename SubactorT, typename CharT >
540 struct is_nullary< custom_terminal< boost::log::expressions::max_size_decorator_terminal< SubactorT, CharT > > > :
545 template< typename LeftT, typename SubactorT, typename CharT >
546 struct is_nullary< custom_terminal< boost::log::expressions::aux::max_size_decorator_output_terminal< LeftT, SubactorT, CharT > > > :
551 } // namespace result_of
553 } // namespace phoenix
559 #include <boost/log/detail/footer.hpp>
561 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_MAX_SIZE_DECORATOR_HPP_INCLUDED_