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)
8 * \file formatters/named_scope.hpp
9 * \author Andrey Semashev
12 * The header contains a formatter function for named scope attribute values.
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_NAMED_SCOPE_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_NAMED_SCOPE_HPP_INCLUDED_
21 #include <boost/static_assert.hpp>
22 #include <boost/type_traits/is_same.hpp>
23 #include <boost/move/core.hpp>
24 #include <boost/move/utility_core.hpp>
25 #include <boost/parameter/binding.hpp>
26 #include <boost/preprocessor/iteration/iterate.hpp>
27 #include <boost/preprocessor/repetition/enum_params.hpp>
28 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
29 #include <boost/phoenix/core/actor.hpp>
30 #include <boost/phoenix/core/terminal_fwd.hpp>
31 #include <boost/phoenix/core/is_nullary.hpp>
32 #include <boost/phoenix/core/environment.hpp>
33 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
34 #include <boost/log/detail/config.hpp>
35 #include <boost/log/attributes/attribute_name.hpp>
36 #include <boost/log/attributes/fallback_policy.hpp>
37 #include <boost/log/attributes/named_scope.hpp>
38 #include <boost/log/attributes/value_visitation.hpp>
39 #include <boost/log/detail/light_function.hpp>
40 #include <boost/log/detail/parameter_tools.hpp>
41 #include <boost/log/detail/custom_terminal_spec.hpp>
42 #include <boost/log/detail/deduce_char_type.hpp>
43 #include <boost/log/detail/attr_output_terminal.hpp>
44 #include <boost/log/expressions/attr_fwd.hpp>
45 #include <boost/log/expressions/keyword_fwd.hpp>
46 #include <boost/log/utility/formatting_ostream.hpp>
47 #include <boost/log/utility/string_literal_fwd.hpp>
48 #include <boost/log/utility/functional/bind.hpp>
49 #include <boost/log/keywords/format.hpp>
50 #include <boost/log/keywords/delimiter.hpp>
51 #include <boost/log/keywords/depth.hpp>
52 #include <boost/log/keywords/iteration.hpp>
53 #include <boost/log/keywords/empty_marker.hpp>
54 #include <boost/log/keywords/incomplete_marker.hpp>
55 #include <boost/log/detail/header.hpp>
57 #ifdef BOOST_HAS_PRAGMA_ONCE
63 BOOST_LOG_OPEN_NAMESPACE
65 namespace expressions {
67 //! Scope iteration directions
68 enum scope_iteration_direction
70 forward, //!< Iterate through scopes from outermost to innermost
71 reverse //!< Iterate through scopes from innermost to outermost
76 #ifdef BOOST_LOG_USE_CHAR
77 //! Parses the named scope format string and constructs the formatter function
78 BOOST_LOG_API boost::log::aux::light_function< void (basic_formatting_ostream< char >&, attributes::named_scope::value_type::value_type const&) >
79 parse_named_scope_format(const char* begin, const char* end);
82 #ifdef BOOST_LOG_USE_WCHAR_T
83 //! Parses the named scope format string and constructs the formatter function
84 BOOST_LOG_API boost::log::aux::light_function< void (basic_formatting_ostream< wchar_t >&, attributes::named_scope::value_type::value_type const&) >
85 parse_named_scope_format(const wchar_t* begin, const wchar_t* end);
88 //! Parses the named scope format string and constructs the formatter function
89 template< typename CharT >
90 inline boost::log::aux::light_function< void (basic_formatting_ostream< CharT >&, attributes::named_scope::value_type::value_type const&) >
91 parse_named_scope_format(const CharT* format)
93 return parse_named_scope_format(format, format + std::char_traits< CharT >::length(format));
96 //! Parses the named scope format string and constructs the formatter function
97 template< typename CharT, typename TraitsT, typename AllocatorT >
98 inline boost::log::aux::light_function< void (basic_formatting_ostream< CharT >&, attributes::named_scope::value_type::value_type const&) >
99 parse_named_scope_format(std::basic_string< CharT, TraitsT, AllocatorT > const& format)
101 const CharT* p = format.c_str();
102 return parse_named_scope_format(p, p + format.size());
105 //! Parses the named scope format string and constructs the formatter function
106 template< typename CharT, typename TraitsT >
107 inline boost::log::aux::light_function< void (basic_formatting_ostream< CharT >&, attributes::named_scope::value_type::value_type const&) >
108 parse_named_scope_format(basic_string_literal< CharT, TraitsT > const& format)
110 const CharT* p = format.c_str();
111 return parse_named_scope_format(p, p + format.size());
114 template< typename CharT >
115 class format_named_scope_impl
118 //! Function result type
119 typedef void result_type;
122 typedef CharT char_type;
124 typedef std::basic_string< char_type > string_type;
125 //! Formatting stream type
126 typedef basic_formatting_ostream< char_type > stream_type;
127 //! Attribute value type
128 typedef attributes::named_scope::value_type value_type;
129 //! Named scope formatter
130 typedef boost::log::aux::light_function< void (stream_type&, value_type::value_type const&) > element_formatter_type;
133 //! Element formatting function
134 element_formatter_type m_element_formatter;
135 //! Element delimiter
136 string_type m_delimiter;
137 //! Incomplete list marker
138 string_type m_incomplete_marker;
139 //! Empty list marker
140 string_type m_empty_marker;
141 //! Maximum number of elements to output
142 value_type::size_type m_depth;
143 //! Iteration direction
144 scope_iteration_direction m_direction;
147 //! Initializing constructor
148 format_named_scope_impl
150 element_formatter_type const& element_formatter,
151 string_type const& delimiter,
152 string_type const& incomplete_marker,
153 string_type const& empty_marker,
154 value_type::size_type depth,
155 scope_iteration_direction direction
157 m_element_formatter(element_formatter),
158 m_delimiter(delimiter),
159 m_incomplete_marker(incomplete_marker),
160 m_empty_marker(empty_marker),
162 m_direction(direction)
166 format_named_scope_impl(format_named_scope_impl const& that) :
167 m_element_formatter(that.m_element_formatter),
168 m_delimiter(that.m_delimiter),
169 m_incomplete_marker(that.m_incomplete_marker),
170 m_empty_marker(that.m_empty_marker),
171 m_depth(that.m_depth),
172 m_direction(that.m_direction)
176 //! Formatting operator
177 result_type operator() (stream_type& strm, value_type const& scopes) const
181 if (m_direction == expressions::forward)
182 format_forward(strm, scopes);
184 format_reverse(strm, scopes);
188 strm << m_empty_marker;
193 //! The function performs formatting of the extracted scope stack in forward direction
194 void format_forward(stream_type& strm, value_type const& scopes) const
196 value_type::const_iterator it, end = scopes.end();
199 value_type::size_type const scopes_to_iterate = (std::min)(m_depth, scopes.size());
201 std::advance(it, -static_cast< value_type::difference_type >(scopes_to_iterate));
210 if (it != scopes.begin())
211 strm << m_incomplete_marker;
213 m_element_formatter(strm, *it);
214 for (++it; it != end; ++it)
217 m_element_formatter(strm, *it);
221 //! The function performs formatting of the extracted scope stack in reverse direction
222 void format_reverse(stream_type& strm, value_type const& scopes) const
224 value_type::const_reverse_iterator it = scopes.rbegin(), end;
227 value_type::size_type const scopes_to_iterate = (std::min)(m_depth, scopes.size());
229 std::advance(end, static_cast< value_type::difference_type >(scopes_to_iterate));
238 m_element_formatter(strm, *it);
239 for (++it; it != end; ++it)
242 m_element_formatter(strm, *it);
245 if (it != scopes.rend())
246 strm << m_incomplete_marker;
254 * Named scope formatter terminal.
256 template< typename FallbackPolicyT, typename CharT >
257 class format_named_scope_terminal
260 #ifndef BOOST_LOG_DOXYGEN_PASS
261 //! Internal typedef for type categorization
262 typedef void _is_boost_log_terminal;
265 //! Attribute value type
266 typedef attributes::named_scope::value_type value_type;
268 typedef FallbackPolicyT fallback_policy;
270 typedef CharT char_type;
272 typedef std::basic_string< char_type > string_type;
273 //! Formatting stream type
274 typedef basic_formatting_ostream< char_type > stream_type;
275 //! Formatter function
276 typedef aux::format_named_scope_impl< char_type > formatter_function_type;
278 //! Function result type
279 typedef string_type result_type;
282 //! Attribute value visitor invoker
283 typedef value_visitor_invoker< value_type, fallback_policy > visitor_invoker_type;
287 attribute_name m_name;
288 //! Formatter function
289 formatter_function_type m_formatter;
290 //! Attribute value visitor invoker
291 visitor_invoker_type m_visitor_invoker;
294 //! Initializing constructor
295 template< typename FormatT >
296 format_named_scope_terminal
298 attribute_name const& name,
299 fallback_policy const& fallback,
300 FormatT const& element_format,
301 string_type const& delimiter,
302 string_type const& incomplete_marker,
303 string_type const& empty_marker,
304 value_type::size_type depth,
305 scope_iteration_direction direction
307 m_name(name), m_formatter(aux::parse_named_scope_format(element_format), delimiter, incomplete_marker, empty_marker, depth, direction), m_visitor_invoker(fallback)
311 format_named_scope_terminal(format_named_scope_terminal const& that) :
312 m_name(that.m_name), m_formatter(that.m_formatter), m_visitor_invoker(that.m_visitor_invoker)
316 //! Returns attribute name
317 attribute_name get_name() const
322 //! Returns fallback policy
323 fallback_policy const& get_fallback_policy() const
325 return m_visitor_invoker.get_fallback_policy();
328 //! Retruns formatter function
329 formatter_function_type const& get_formatter_function() const
334 //! Invokation operator
335 template< typename ContextT >
336 result_type operator() (ContextT const& ctx)
339 stream_type strm(str);
340 m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type&, stream_type& >(m_formatter, strm));
342 return BOOST_LOG_NRVO_RESULT(str);
345 //! Invokation operator
346 template< typename ContextT >
347 result_type operator() (ContextT const& ctx) const
350 stream_type strm(str);
351 m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type const&, stream_type& >(m_formatter, strm));
353 return BOOST_LOG_NRVO_RESULT(str);
356 BOOST_DELETED_FUNCTION(format_named_scope_terminal())
360 * Named scope formatter actor.
362 template< typename FallbackPolicyT, typename CharT, template< typename > class ActorT = phoenix::actor >
363 class format_named_scope_actor :
364 public ActorT< format_named_scope_terminal< FallbackPolicyT, CharT > >
368 typedef CharT char_type;
370 typedef FallbackPolicyT fallback_policy;
371 //! Base terminal type
372 typedef format_named_scope_terminal< fallback_policy, char_type > terminal_type;
373 //! Attribute value type
374 typedef typename terminal_type::value_type value_type;
375 //! Formatter function
376 typedef typename terminal_type::formatter_function_type formatter_function_type;
379 typedef ActorT< terminal_type > base_type;
382 //! Initializing constructor
383 explicit format_named_scope_actor(base_type const& act) : base_type(act)
388 * \returns The attribute name
390 attribute_name get_name() const
392 return this->proto_expr_.child0.get_name();
396 * \returns Fallback policy
398 fallback_policy const& get_fallback_policy() const
400 return this->proto_expr_.child0.get_fallback_policy();
404 * \returns Formatter function
406 formatter_function_type const& get_formatter_function() const
408 return this->proto_expr_.child0.get_formatter_function();
412 #ifndef BOOST_LOG_DOXYGEN_PASS
414 #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
415 template< typename LeftExprT, typename FallbackPolicyT, typename CharT >\
416 BOOST_FORCEINLINE phoenix::actor< aux::attribute_output_terminal< phoenix::actor< LeftExprT >, attributes::named_scope::value_type, FallbackPolicyT, typename format_named_scope_actor< FallbackPolicyT, CharT >::formatter_function_type > >\
417 operator<< (phoenix::actor< LeftExprT > left_ref left, format_named_scope_actor< FallbackPolicyT, CharT > right_ref right)\
419 typedef aux::attribute_output_terminal< phoenix::actor< LeftExprT >, attributes::named_scope::value_type, FallbackPolicyT, typename format_named_scope_actor< FallbackPolicyT, CharT >::formatter_function_type > terminal_type;\
420 phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_name(), right.get_formatter_function(), right.get_fallback_policy()) }};\
424 #include <boost/log/detail/generate_overloads.hpp>
426 #undef BOOST_LOG_AUX_OVERLOAD
428 #endif // BOOST_LOG_DOXYGEN_PASS
432 //! Auxiliary traits to acquire default formatter parameters depending on the character type
433 template< typename CharT >
434 struct default_named_scope_params;
436 #ifdef BOOST_LOG_USE_CHAR
438 struct default_named_scope_params< char >
440 static const char* forward_delimiter() { return "->"; }
441 static const char* reverse_delimiter() { return "<-"; }
442 static const char* incomplete_marker() { return "..."; }
443 static const char* empty_marker() { return ""; }
446 #ifdef BOOST_LOG_USE_WCHAR_T
448 struct default_named_scope_params< wchar_t >
450 static const wchar_t* forward_delimiter() { return L"->"; }
451 static const wchar_t* reverse_delimiter() { return L"<-"; }
452 static const wchar_t* incomplete_marker() { return L"..."; }
453 static const wchar_t* empty_marker() { return L""; }
457 template< typename CharT, template< typename > class ActorT, typename FallbackPolicyT, typename ArgsT >
458 BOOST_FORCEINLINE format_named_scope_actor< FallbackPolicyT, CharT, ActorT > format_named_scope(attribute_name const& name, FallbackPolicyT const& fallback, ArgsT const& args)
460 typedef format_named_scope_actor< FallbackPolicyT, CharT, ActorT > actor_type;
461 typedef typename actor_type::terminal_type terminal_type;
462 typedef default_named_scope_params< CharT > default_params;
463 scope_iteration_direction dir = args[keywords::iteration | expressions::forward];
464 const CharT* default_delimiter = (dir == expressions::forward ? default_params::forward_delimiter() : default_params::reverse_delimiter());
465 typename actor_type::base_type act =
471 args[keywords::format],
472 args[keywords::delimiter | default_delimiter],
473 args[keywords::incomplete_marker | default_params::incomplete_marker()],
474 args[keywords::empty_marker | default_params::empty_marker()],
475 args[keywords::depth | static_cast< attributes::named_scope::value_type::size_type >(0)],
479 return actor_type(act);
485 * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
486 * expression (stream output or \c format placeholder filler).
488 * \param name Attribute name
489 * \param element_format Format string for a single named scope
491 template< typename CharT >
492 BOOST_FORCEINLINE format_named_scope_actor< fallback_to_none, CharT > format_named_scope(attribute_name const& name, const CharT* element_format)
494 typedef format_named_scope_actor< fallback_to_none, CharT > actor_type;
495 typedef typename actor_type::terminal_type terminal_type;
496 typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), element_format) }};
497 return actor_type(act);
501 * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
502 * expression (stream output or \c format placeholder filler).
504 * \param name Attribute name
505 * \param element_format Format string for a single named scope
507 template< typename CharT >
508 BOOST_FORCEINLINE format_named_scope_actor< fallback_to_none, CharT > format_named_scope(attribute_name const& name, std::basic_string< CharT > const& element_format)
510 typedef format_named_scope_actor< fallback_to_none, CharT > actor_type;
511 typedef typename actor_type::terminal_type terminal_type;
512 typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), element_format) }};
513 return actor_type(act);
517 * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
518 * expression (stream output or \c format placeholder filler).
520 * \param keyword Attribute keyword
521 * \param element_format Format string for a single named scope
523 template< typename DescriptorT, template< typename > class ActorT, typename CharT >
524 BOOST_FORCEINLINE format_named_scope_actor< fallback_to_none, CharT, ActorT >
525 format_named_scope(attribute_keyword< DescriptorT, ActorT > const& keyword, const CharT* element_format)
527 BOOST_STATIC_ASSERT_MSG((is_same< typename DescriptorT::value_type, attributes::named_scope::value_type >::value),\
528 "Boost.Log: Named scope formatter only accepts attribute values of type attributes::named_scope::value_type.");
530 typedef format_named_scope_actor< fallback_to_none, CharT, ActorT > actor_type;
531 typedef typename actor_type::terminal_type terminal_type;
532 typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), element_format) }};
533 return actor_type(act);
537 * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
538 * expression (stream output or \c format placeholder filler).
540 * \param keyword Attribute keyword
541 * \param element_format Format string for a single named scope
543 template< typename DescriptorT, template< typename > class ActorT, typename CharT >
544 BOOST_FORCEINLINE format_named_scope_actor< fallback_to_none, CharT, ActorT >
545 format_named_scope(attribute_keyword< DescriptorT, ActorT > const& keyword, std::basic_string< CharT > const& element_format)
547 BOOST_STATIC_ASSERT_MSG((is_same< typename DescriptorT::value_type, attributes::named_scope::value_type >::value),\
548 "Boost.Log: Named scope formatter only accepts attribute values of type attributes::named_scope::value_type.");
550 typedef format_named_scope_actor< fallback_to_none, CharT, ActorT > actor_type;
551 typedef typename actor_type::terminal_type terminal_type;
552 typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), element_format) }};
553 return actor_type(act);
557 * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
558 * expression (stream output or \c format placeholder filler).
560 * \param placeholder Attribute placeholder
561 * \param element_format Format string for a single named scope
563 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT >
564 BOOST_FORCEINLINE format_named_scope_actor< FallbackPolicyT, CharT, ActorT >
565 format_named_scope(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, const CharT* element_format)
567 BOOST_STATIC_ASSERT_MSG((is_same< T, attributes::named_scope::value_type >::value),\
568 "Boost.Log: Named scope formatter only accepts attribute values of type attributes::named_scope::value_type.");
570 typedef format_named_scope_actor< FallbackPolicyT, CharT, ActorT > actor_type;
571 typedef typename actor_type::terminal_type terminal_type;
572 typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), element_format) }};
573 return actor_type(act);
577 * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
578 * expression (stream output or \c format placeholder filler).
580 * \param placeholder Attribute placeholder
581 * \param element_format Format string for a single named scope
583 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT >
584 BOOST_FORCEINLINE format_named_scope_actor< FallbackPolicyT, CharT, ActorT >
585 format_named_scope(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, std::basic_string< CharT > const& element_format)
587 BOOST_STATIC_ASSERT_MSG((is_same< T, attributes::named_scope::value_type >::value),\
588 "Boost.Log: Named scope formatter only accepts attribute values of type attributes::named_scope::value_type.");
590 typedef format_named_scope_actor< FallbackPolicyT, CharT, ActorT > actor_type;
591 typedef typename actor_type::terminal_type terminal_type;
592 typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), element_format) }};
593 return actor_type(act);
596 #if !defined(BOOST_LOG_DOXYGEN_PASS)
598 # define BOOST_PP_FILENAME_1 <boost/log/detail/named_scope_fmt_pp.hpp>
599 # define BOOST_PP_ITERATION_LIMITS (1, 6)
600 # include BOOST_PP_ITERATE()
602 #else // BOOST_LOG_DOXYGEN_PASS
605 * Formatter generator. Construct the named scope formatter with the specified formatting parameters.
607 * \param name Attribute name
608 * \param args An set of named parameters. Supported parameters:
609 * \li \c format - A format string for named scopes. The string can contain "%n", "%f" and "%l" placeholders for the scope name, file and line number, respectively. This parameter is mandatory.
610 * \li \c delimiter - A string that is used to delimit the formatted scope names. Default: "->" or "<-", depending on the iteration direction.
611 * \li \c incomplete_marker - A string that is used to indicate that the list was printed incomplete because of depth limitation. Default: "...".
612 * \li \c empty_marker - A string that is output in case if the scope list is empty. Default: "", i.e. nothing is output.
613 * \li \c iteration - Iteration direction, see \c scope_iteration_direction enumeration. Default: forward.
614 * \li \c depth - Iteration depth. Default: unlimited.
616 template< typename... ArgsT >
617 unspecified format_named_scope(attribute_name const& name, ArgsT... const& args);
620 template< typename DescriptorT, template< typename > class ActorT, typename... ArgsT >
621 unspecified format_named_scope(attribute_keyword< DescriptorT, ActorT > const& keyword, ArgsT... const& args);
624 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename... ArgsT >
625 unspecified format_named_scope(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, ArgsT... const& args);
627 #endif // BOOST_LOG_DOXYGEN_PASS
629 } // namespace expressions
631 BOOST_LOG_CLOSE_NAMESPACE // namespace log
633 #ifndef BOOST_LOG_DOXYGEN_PASS
637 namespace result_of {
639 template< typename FallbackPolicyT, typename CharT >
640 struct is_nullary< custom_terminal< boost::log::expressions::format_named_scope_terminal< FallbackPolicyT, CharT > > > :
645 } // namespace result_of
647 } // namespace phoenix
653 #include <boost/log/detail/footer.hpp>
655 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_NAMED_SCOPE_HPP_INCLUDED_