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/char_decorator.hpp
9  * \author Andrey Semashev
10  * \date   17.11.2012
11  *
12  * The header contains implementation of a character decorator.
13  */
14 
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_
17 
18 #include <vector>
19 #include <string>
20 #include <iterator>
21 #include <boost/assert.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/mpl/bool.hpp>
24 #include <boost/range/begin.hpp>
25 #include <boost/range/end.hpp>
26 #include <boost/range/size.hpp>
27 #include <boost/range/const_iterator.hpp>
28 #include <boost/range/value_type.hpp>
29 #include <boost/move/core.hpp>
30 #include <boost/move/utility.hpp>
31 #include <boost/utility/addressof.hpp>
32 #include <boost/phoenix/core/actor.hpp>
33 #include <boost/phoenix/core/meta_grammar.hpp>
34 #include <boost/phoenix/core/terminal_fwd.hpp>
35 #include <boost/phoenix/core/is_nullary.hpp>
36 #include <boost/phoenix/core/environment.hpp>
37 #include <boost/phoenix/support/vector.hpp>
38 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
39 #include <boost/type_traits/is_same.hpp>
40 #include <boost/type_traits/remove_cv.hpp>
41 #include <boost/type_traits/remove_reference.hpp>
42 #include <boost/log/detail/config.hpp>
43 #include <boost/log/detail/custom_terminal_spec.hpp>
44 #include <boost/log/detail/deduce_char_type.hpp>
45 #include <boost/log/utility/formatting_ostream.hpp>
46 #include <boost/log/detail/header.hpp>
47 
48 #ifdef BOOST_HAS_PRAGMA_ONCE
49 #pragma once
50 #endif
51 
52 namespace boost {
53 
54 BOOST_LOG_OPEN_NAMESPACE
55 
56 namespace expressions {
57 
58 namespace aux {
59 
60 template< typename RangeT >
61 struct string_const_iterator : range_const_iterator< RangeT > {};
62 template< >
63 struct string_const_iterator< char* > { typedef char* type; };
64 template< >
65 struct string_const_iterator< const char* > { typedef const char* type; };
66 template< >
67 struct string_const_iterator< wchar_t* > { typedef wchar_t* type; };
68 template< >
69 struct string_const_iterator< const wchar_t* > { typedef const wchar_t* type; };
70 
71 } // namespace aux
72 
73 /*!
74  * A simple character decorator implementation. This implementation replaces string patterns in the source string with
75  * the fixed replacements. Source patterns and replacements can be specified at the object construction.
76  */
77 template< typename CharT >
78 class pattern_replacer
79 {
80 public:
81     //! Result type
82     typedef void result_type;
83 
84     //! Character type
85     typedef CharT char_type;
86     //! String type
87     typedef std::basic_string< char_type > string_type;
88 
89 private:
90     //! Lengths of source pattern and replacement
91     struct string_lengths
92     {
93         unsigned int from_len, to_len;
94     };
95 
96     //! List of the decorations to apply
97     typedef std::vector< string_lengths > string_lengths_list;
98 
99 private:
100     //! Characters of the interleaved source patterns and replacements
101     string_type m_decoration_chars;
102     //! List of the decorations to apply
103     string_lengths_list m_string_lengths;
104 
105 public:
106     /*!
107      * Initializing constructor. Creates a pattern replacer with the specified \a decorations.
108      * The provided decorations must be a sequence of \c std::pair of strings. The first element
109      * of each pair is the source pattern, and the second one is the corresponding replacement.
110      */
111     template< typename RangeT >
pattern_replacer(RangeT const & decorations)112     explicit pattern_replacer(RangeT const& decorations)
113     {
114         typedef typename range_const_iterator< RangeT >::type iterator;
115         for (iterator it = begin(decorations), end_ = end(decorations); it != end_; ++it)
116         {
117             string_lengths lens;
118             {
119                 typedef typename aux::string_const_iterator< typename range_value< RangeT >::type::first_type >::type first_iterator;
120                 first_iterator b = string_begin(it->first), e = string_end(it->first);
121                 lens.from_len = static_cast< unsigned int >(std::distance(b, e));
122                 m_decoration_chars.append(b, e);
123             }
124             {
125                 typedef typename aux::string_const_iterator< typename range_value< RangeT >::type::second_type >::type second_iterator;
126                 second_iterator b = string_begin(it->second), e = string_end(it->second);
127                 lens.to_len = static_cast< unsigned int >(std::distance(b, e));
128                 m_decoration_chars.append(b, e);
129             }
130             m_string_lengths.push_back(lens);
131         }
132     }
133     /*!
134      * Initializing constructor. Creates a pattern replacer with decorations specified
135      * in form of two same-sized string sequences. Each <tt>i</tt>'th decoration will be
136      * <tt>from[i]</tt> -> <tt>to[i]</tt>.
137      */
138     template< typename FromRangeT, typename ToRangeT >
pattern_replacer(FromRangeT const & from,ToRangeT const & to)139     pattern_replacer(FromRangeT const& from, ToRangeT const& to)
140     {
141         typedef typename range_const_iterator< FromRangeT >::type iterator1;
142         typedef typename range_const_iterator< ToRangeT >::type iterator2;
143         iterator1 it1 = begin(from), end1 = end(from);
144         iterator2 it2 = begin(to), end2 = end(to);
145         for (; it1 != end1 && it2 != end2; ++it1, ++it2)
146         {
147             string_lengths lens;
148             {
149                 typedef typename aux::string_const_iterator< typename range_value< FromRangeT >::type >::type from_iterator;
150                 from_iterator b = string_begin(*it1), e = string_end(*it1);
151                 lens.from_len = static_cast< unsigned int >(std::distance(b, e));
152                 m_decoration_chars.append(b, e);
153             }
154             {
155                 typedef typename aux::string_const_iterator< typename range_value< ToRangeT >::type >::type to_iterator;
156                 to_iterator b = string_begin(*it2), e = string_end(*it2);
157                 lens.to_len = static_cast< unsigned int >(std::distance(b, e));
158                 m_decoration_chars.append(b, e);
159             }
160             m_string_lengths.push_back(lens);
161         }
162 
163         // Both sequences should be of the same size
164         BOOST_ASSERT(it1 == end1);
165         BOOST_ASSERT(it2 == end2);
166     }
167     //! Copy constructor
pattern_replacer(pattern_replacer const & that)168     pattern_replacer(pattern_replacer const& that) : m_decoration_chars(that.m_decoration_chars), m_string_lengths(that.m_string_lengths)
169     {
170     }
171 
172     //! Applies string replacements starting from the specified position
operator ()(string_type & str,typename string_type::size_type start_pos=0) const173     result_type operator() (string_type& str, typename string_type::size_type start_pos = 0) const
174     {
175         typedef typename string_type::size_type size_type;
176 
177         const char_type* from_chars = m_decoration_chars.c_str();
178         for (typename string_lengths_list::const_iterator it = m_string_lengths.begin(), end = m_string_lengths.end(); it != end; ++it)
179         {
180             const unsigned int from_len = it->from_len, to_len = it->to_len;
181             const char_type* const to_chars = from_chars + from_len;
182             for (size_type pos = str.find(from_chars, start_pos, from_len); pos != string_type::npos; pos = str.find(from_chars, pos, from_len))
183             {
184                 str.replace(pos, from_len, to_chars, to_len);
185                 pos += to_len;
186             }
187             from_chars = to_chars + to_len;
188         }
189     }
190 
191 private:
string_begin(char_type * p)192     static char_type* string_begin(char_type* p)
193     {
194         return p;
195     }
string_begin(const char_type * p)196     static const char_type* string_begin(const char_type* p)
197     {
198         return p;
199     }
200     template< typename RangeT >
string_begin(RangeT const & r)201     static typename range_const_iterator< RangeT >::type string_begin(RangeT const& r)
202     {
203         return begin(r);
204     }
205 
string_end(char_type * p)206     static char_type* string_end(char_type* p)
207     {
208         while (*p)
209             ++p;
210         return p;
211     }
string_end(const char_type * p)212     static const char_type* string_end(const char_type* p)
213     {
214         while (*p)
215             ++p;
216         return p;
217     }
218     template< typename RangeT >
string_end(RangeT const & r)219     static typename range_const_iterator< RangeT >::type string_end(RangeT const& r)
220     {
221         return end(r);
222     }
223 };
224 
225 namespace aux {
226 
227 //! Character decorator stream output terminal
228 template< typename LeftT, typename SubactorT, typename ImplT >
229 class char_decorator_output_terminal
230 {
231 private:
232     //! Self type
233     typedef char_decorator_output_terminal< LeftT, SubactorT, ImplT > this_type;
234 
235 public:
236     //! Internal typedef for type categorization
237     typedef void _is_boost_log_terminal;
238 
239     //! Implementation type
240     typedef ImplT impl_type;
241 
242     //! Character type
243     typedef typename impl_type::char_type char_type;
244     //! String type
245     typedef typename impl_type::string_type string_type;
246     //! Adopted actor type
247     typedef SubactorT subactor_type;
248 
249     //! Result type definition
250     template< typename >
251     struct result;
252 
253     template< typename ThisT, typename ContextT >
254     struct result< ThisT(ContextT) >
255     {
256         typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
257         typedef typename phoenix::evaluator::impl<
258             typename LeftT::proto_base_expr&,
259             context_type,
260             phoenix::unused
261         >::result_type type;
262     };
263 
264 private:
265     //! Left argument actor
266     LeftT m_left;
267     //! Adopted formatter actor
268     subactor_type m_subactor;
269     //! Implementation type
270     impl_type m_impl;
271 
272 public:
273     /*!
274      * Initializing constructor. Creates decorator of the \a fmt formatter with the specified \a decorations.
275      */
char_decorator_output_terminal(LeftT const & left,subactor_type const & sub,impl_type const & impl)276     char_decorator_output_terminal(LeftT const& left, subactor_type const& sub, impl_type const& impl) :
277         m_left(left), m_subactor(sub), m_impl(impl)
278     {
279     }
280     /*!
281      * Copy constructor
282      */
char_decorator_output_terminal(char_decorator_output_terminal const & that)283     char_decorator_output_terminal(char_decorator_output_terminal const& that) :
284         m_left(that.m_left), m_subactor(that.m_subactor), m_impl(that.m_impl)
285     {
286     }
287 
288     /*!
289      * Invokation operator
290      */
291     template< typename ContextT >
operator ()(ContextT const & ctx)292     typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
293     {
294         // Flush the stream and keep the current write position in the target string
295         typedef typename result< this_type(ContextT const&) >::type result_type;
296         result_type strm = phoenix::eval(m_left, ctx);
297         strm.flush();
298         typename string_type::size_type const start_pos = strm.rdbuf()->storage()->size();
299 
300         // Invoke the adopted formatter
301         typedef typename result< this_type(ContextT const&) >::type result_type;
302         phoenix::eval(m_subactor, ctx);
303 
304         // Flush the buffered characters and apply decorations
305         strm.flush();
306         m_impl(*strm.rdbuf()->storage(), start_pos);
307 
308         return strm;
309     }
310 
311     /*!
312      * Invokation operator
313      */
314     template< typename ContextT >
operator ()(ContextT const & ctx) const315     typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
316     {
317         // Flush the stream and keep the current write position in the target string
318         typedef typename result< const this_type(ContextT const&) >::type result_type;
319         result_type strm = phoenix::eval(m_left, ctx);
320         strm.flush();
321         typename string_type::size_type const start_pos = strm.rdbuf()->storage()->size();
322 
323         // Invoke the adopted formatter
324         typedef typename result< const this_type(ContextT const&) >::type result_type;
325         phoenix::eval(m_subactor, ctx);
326 
327         // Flush the buffered characters and apply decorations
328         strm.flush();
329         m_impl(*strm.rdbuf()->storage(), start_pos);
330 
331         return strm;
332     }
333 
334     BOOST_DELETED_FUNCTION(char_decorator_output_terminal())
335 };
336 
337 } // namespace aux
338 
339 /*!
340  * Character decorator terminal class. This formatter allows to modify strings generated by other
341  * formatters on character level. The most obvious application of decorators is replacing
342  * a certain set of characters with decorated equivalents to satisfy requirements of
343  * text-based sinks.
344  *
345  * The \c char_decorator_terminal class aggregates the formatter being decorated, and a set
346  * of string pairs that are used as decorations. All decorations are applied sequentially.
347  * The \c char_decorator_terminal class is a formatter itself, so it can be used to construct
348  * more complex formatters, including nesting decorators.
349  */
350 template< typename SubactorT, typename ImplT >
351 class char_decorator_terminal
352 {
353 private:
354     //! Self type
355     typedef char_decorator_terminal< SubactorT, ImplT > this_type;
356 
357 public:
358     //! Internal typedef for type categorization
359     typedef void _is_boost_log_terminal;
360 
361     //! Implementation type
362     typedef ImplT impl_type;
363     //! Character type
364     typedef typename impl_type::char_type char_type;
365     //! String type
366     typedef typename impl_type::string_type string_type;
367     //! Stream type
368     typedef basic_formatting_ostream< char_type > stream_type;
369     //! Adopted actor type
370     typedef SubactorT subactor_type;
371 
372     //! Result type definition
373     typedef string_type result_type;
374 
375 private:
376     //! Adopted formatter actor
377     subactor_type m_subactor;
378     //! Implementation
379     impl_type m_impl;
380 
381 public:
382     /*!
383      * Initializing constructor.
384      */
char_decorator_terminal(subactor_type const & sub,impl_type const & impl)385     char_decorator_terminal(subactor_type const& sub, impl_type const& impl) : m_subactor(sub), m_impl(impl)
386     {
387     }
388     /*!
389      * Copy constructor
390      */
char_decorator_terminal(char_decorator_terminal const & that)391     char_decorator_terminal(char_decorator_terminal const& that) : m_subactor(that.m_subactor), m_impl(that.m_impl)
392     {
393     }
394 
395     /*!
396      * \returns Adopted subactor
397      */
get_subactor() const398     subactor_type const& get_subactor() const
399     {
400         return m_subactor;
401     }
402 
403     /*!
404      * \returns Implementation
405      */
get_impl() const406     impl_type const& get_impl() const
407     {
408         return m_impl;
409     }
410 
411     /*!
412      * Invokation operator
413      */
414     template< typename ContextT >
operator ()(ContextT const & ctx)415     result_type operator() (ContextT const& ctx)
416     {
417         string_type str;
418         stream_type strm(str);
419 
420         // Invoke the adopted formatter
421         typedef phoenix::vector3<
422             subactor_type*,
423             typename fusion::result_of::at_c<
424                 typename remove_cv<
425                     typename remove_reference<
426                         typename phoenix::result_of::env< ContextT const& >::type
427                     >::type
428                 >::type::args_type,
429                 0
430             >::type,
431             stream_type&
432         > env_type;
433         env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm };
434         phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx)));
435 
436         // Flush the buffered characters and apply decorations
437         strm.flush();
438         m_impl(*strm.rdbuf()->storage());
439 
440         return boost::move(str);
441     }
442 
443     /*!
444      * Invokation operator
445      */
446     template< typename ContextT >
operator ()(ContextT const & ctx) const447     result_type operator() (ContextT const& ctx) const
448     {
449         string_type str;
450         stream_type strm(str);
451 
452         // Invoke the adopted formatter
453         typedef phoenix::vector3<
454             const subactor_type*,
455             typename fusion::result_of::at_c<
456                 typename remove_cv<
457                     typename remove_reference<
458                         typename phoenix::result_of::env< ContextT const& >::type
459                     >::type
460                 >::type::args_type,
461                 0
462             >::type,
463             stream_type&
464         > env_type;
465         env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm };
466         phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx)));
467 
468         // Flush the buffered characters and apply decorations
469         strm.flush();
470         m_impl(*strm.rdbuf()->storage());
471 
472         return boost::move(str);
473     }
474 
475     BOOST_DELETED_FUNCTION(char_decorator_terminal())
476 };
477 
478 /*!
479  * Character decorator actor
480  */
481 template< typename SubactorT, typename ImplT, template< typename > class ActorT = phoenix::actor >
482 class char_decorator_actor :
483     public ActorT< char_decorator_terminal< SubactorT, ImplT > >
484 {
485 public:
486     //! Base terminal type
487     typedef char_decorator_terminal< SubactorT, ImplT > terminal_type;
488     //! Character type
489     typedef typename terminal_type::char_type char_type;
490 
491     //! Base actor type
492     typedef ActorT< terminal_type > base_type;
493 
494 public:
495     //! Initializing constructor
char_decorator_actor(base_type const & act)496     explicit char_decorator_actor(base_type const& act) : base_type(act)
497     {
498     }
499 
500     //! Returns reference to the terminal
get_terminal() const501     terminal_type const& get_terminal() const
502     {
503         return this->proto_expr_.child0;
504     }
505 };
506 
507 #ifndef BOOST_LOG_DOXYGEN_PASS
508 
509 #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
510     template< typename LeftExprT, typename SubactorT, typename ImplT, template< typename > class ActorT >\
511     BOOST_FORCEINLINE phoenix::actor< aux::char_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, ImplT > >\
512     operator<< (phoenix::actor< LeftExprT > left_ref left, char_decorator_actor< SubactorT, ImplT, ActorT > right_ref right)\
513     {\
514         typedef aux::char_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, ImplT > terminal_type;\
515         phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_terminal().get_subactor(), right.get_terminal().get_impl()) }};\
516         return actor;\
517     }
518 
519 #include <boost/log/detail/generate_overloads.hpp>
520 
521 #undef BOOST_LOG_AUX_OVERLOAD
522 
523 #endif // BOOST_LOG_DOXYGEN_PASS
524 
525 namespace aux {
526 
527 template< typename RangeT >
528 class char_decorator_gen1
529 {
530     RangeT const& m_decorations;
531 
532     typedef typename boost::log::aux::deduce_char_type< typename range_value< RangeT >::type::first_type >::type char_type;
533 
534 public:
char_decorator_gen1(RangeT const & decorations)535     explicit char_decorator_gen1(RangeT const& decorations) : m_decorations(decorations)
536     {
537     }
538 
539     template< typename SubactorT >
operator [](SubactorT const & subactor) const540     BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
541     {
542         typedef pattern_replacer< char_type > replacer_type;
543         typedef char_decorator_actor< SubactorT, replacer_type > result_type;
544         typedef typename result_type::terminal_type terminal_type;
545         typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(m_decorations)) }};
546         return result_type(act);
547     }
548 };
549 
550 template< typename FromRangeT, typename ToRangeT >
551 class char_decorator_gen2
552 {
553     FromRangeT const& m_from;
554     ToRangeT const& m_to;
555 
556     typedef typename boost::log::aux::deduce_char_type< typename range_value< FromRangeT >::type >::type from_char_type;
557     typedef typename boost::log::aux::deduce_char_type< typename range_value< ToRangeT >::type >::type to_char_type;
558     BOOST_STATIC_ASSERT_MSG((is_same< from_char_type, to_char_type >::value), "Boost.Log: character decorator cannot be instantiated with different character types for source and replacement strings");
559 
560 public:
char_decorator_gen2(FromRangeT const & from,ToRangeT const & to)561     char_decorator_gen2(FromRangeT const& from, ToRangeT const& to) : m_from(from), m_to(to)
562     {
563     }
564 
565     template< typename SubactorT >
operator [](SubactorT const & subactor) const566     BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< from_char_type > > operator[] (SubactorT const& subactor) const
567     {
568         typedef pattern_replacer< from_char_type > replacer_type;
569         typedef char_decorator_actor< SubactorT, replacer_type > result_type;
570         typedef typename result_type::terminal_type terminal_type;
571         typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(m_from, m_to)) }};
572         return result_type(act);
573     }
574 };
575 
576 } // namespace aux
577 
578 /*!
579  * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
580  * to construct the actual decorator.
581  *
582  * \param decorations A sequence of string pairs that will be used as decorations. Every <tt>decorations[i].first</tt>
583  *                    substring occurrence in the output will be replaced with <tt>decorations[i].second</tt>.
584  */
585 template< typename RangeT >
char_decor(RangeT const & decorations)586 BOOST_FORCEINLINE aux::char_decorator_gen1< RangeT > char_decor(RangeT const& decorations)
587 {
588     return aux::char_decorator_gen1< RangeT >(decorations);
589 }
590 
591 /*!
592  * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
593  * to construct the actual decorator.
594  *
595  * \param from A sequence of strings that will be sought in the output.
596  * \param to A sequence of strings that will be used as replacements.
597  *
598  * \note The \a from and \a to sequences mush be of the same size. Every <tt>from[i]</tt>
599  *       substring occurrence in the output will be replaced with <tt>to[i]</tt>.
600  */
601 template< typename FromRangeT, typename ToRangeT >
char_decor(FromRangeT const & from,ToRangeT const & to)602 BOOST_FORCEINLINE aux::char_decorator_gen2< FromRangeT, ToRangeT > char_decor(FromRangeT const& from, ToRangeT const& to)
603 {
604     return aux::char_decorator_gen2< FromRangeT, ToRangeT >(from, to);
605 }
606 
607 } // namespace expressions
608 
609 BOOST_LOG_CLOSE_NAMESPACE // namespace log
610 
611 #ifndef BOOST_LOG_DOXYGEN_PASS
612 
613 namespace phoenix {
614 
615 namespace result_of {
616 
617 template< typename SubactorT, typename ImplT >
618 struct is_nullary< custom_terminal< boost::log::expressions::char_decorator_terminal< SubactorT, ImplT > > > :
619     public mpl::false_
620 {
621 };
622 
623 template< typename LeftT, typename SubactorT, typename ImplT >
624 struct is_nullary< custom_terminal< boost::log::expressions::aux::char_decorator_output_terminal< LeftT, SubactorT, ImplT > > > :
625     public mpl::false_
626 {
627 };
628 
629 } // namespace result_of
630 
631 } // namespace phoenix
632 
633 #endif
634 
635 } // namespace boost
636 
637 #include <boost/log/detail/footer.hpp>
638 
639 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_
640