From 08c1e93fa36a49f49325a07fe91ff92c964c2b6c Mon Sep 17 00:00:00 2001 From: Chanho Park Date: Thu, 11 Dec 2014 18:55:56 +0900 Subject: Imported Upstream version 1.57.0 --- boost/log/expressions/formatters/c_decorator.hpp | 281 +++++++++ .../log/expressions/formatters/char_decorator.hpp | 639 ++++++++++++++++++++ boost/log/expressions/formatters/csv_decorator.hpp | 140 +++++ boost/log/expressions/formatters/date_time.hpp | 343 +++++++++++ boost/log/expressions/formatters/format.hpp | 128 ++++ boost/log/expressions/formatters/if.hpp | 315 ++++++++++ boost/log/expressions/formatters/named_scope.hpp | 653 +++++++++++++++++++++ boost/log/expressions/formatters/stream.hpp | 53 ++ .../log/expressions/formatters/wrap_formatter.hpp | 338 +++++++++++ boost/log/expressions/formatters/xml_decorator.hpp | 138 +++++ 10 files changed, 3028 insertions(+) create mode 100644 boost/log/expressions/formatters/c_decorator.hpp create mode 100644 boost/log/expressions/formatters/char_decorator.hpp create mode 100644 boost/log/expressions/formatters/csv_decorator.hpp create mode 100644 boost/log/expressions/formatters/date_time.hpp create mode 100644 boost/log/expressions/formatters/format.hpp create mode 100644 boost/log/expressions/formatters/if.hpp create mode 100644 boost/log/expressions/formatters/named_scope.hpp create mode 100644 boost/log/expressions/formatters/stream.hpp create mode 100644 boost/log/expressions/formatters/wrap_formatter.hpp create mode 100644 boost/log/expressions/formatters/xml_decorator.hpp (limited to 'boost/log/expressions/formatters') diff --git a/boost/log/expressions/formatters/c_decorator.hpp b/boost/log/expressions/formatters/c_decorator.hpp new file mode 100644 index 0000000000..a793b52bfe --- /dev/null +++ b/boost/log/expressions/formatters/c_decorator.hpp @@ -0,0 +1,281 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/c_decorator.hpp + * \author Andrey Semashev + * \date 18.11.2012 + * + * The header contains implementation of C-style character decorators. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_C_DECORATOR_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_C_DECORATOR_HPP_INCLUDED_ + +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +template< typename > +struct c_decorator_traits; + +#ifdef BOOST_LOG_USE_CHAR +template< > +struct c_decorator_traits< char > +{ + static boost::iterator_range< const char* const* > get_patterns() + { + static const char* const patterns[] = + { + "\\", "\a", "\b", "\f", "\n", "\r", "\t", "\v", "'", "\"", "?" + }; + return boost::make_iterator_range(patterns); + } + static boost::iterator_range< const char* const* > get_replacements() + { + static const char* const replacements[] = + { + "\\\\", "\\a", "\\b", "\\f", "\\n", "\\r", "\\t", "\\v", "\\'", "\\\"", "\\?" + }; + return boost::make_iterator_range(replacements); + } + template< unsigned int N > + static std::size_t print_escaped(char (&buf)[N], char c) + { + int n = boost::log::aux::snprintf(buf, N, "\\x%0.2X", static_cast< unsigned int >(static_cast< uint8_t >(c))); + if (n < 0) + { + n = 0; + buf[0] = '\0'; + } + return static_cast< unsigned int >(n) >= N ? N - 1 : static_cast< unsigned int >(n); + } +}; +#endif // BOOST_LOG_USE_CHAR + +#ifdef BOOST_LOG_USE_WCHAR_T +template< > +struct c_decorator_traits< wchar_t > +{ + static boost::iterator_range< const wchar_t* const* > get_patterns() + { + static const wchar_t* const patterns[] = + { + L"\\", L"\a", L"\b", L"\f", L"\n", L"\r", L"\t", L"\v", L"'", L"\"", L"?" + }; + return boost::make_iterator_range(patterns); + } + static boost::iterator_range< const wchar_t* const* > get_replacements() + { + static const wchar_t* const replacements[] = + { + L"\\\\", L"\\a", L"\\b", L"\\f", L"\\n", L"\\r", L"\\t", L"\\v", L"\\'", L"\\\"", L"\\?" + }; + return boost::make_iterator_range(replacements); + } + template< unsigned int N > + static std::size_t print_escaped(wchar_t (&buf)[N], wchar_t c) + { + const wchar_t* format; + unsigned int val; + if (sizeof(wchar_t) == 1) + { + format = L"\\x%0.2X"; + val = static_cast< uint8_t >(c); + } + else if (sizeof(wchar_t) == 2) + { + format = L"\\x%0.4X"; + val = static_cast< uint16_t >(c); + } + else + { + format = L"\\x%0.8X"; + val = static_cast< uint32_t >(c); + } + + int n = boost::log::aux::swprintf(buf, N, format, val); + if (n < 0) + { + n = 0; + buf[0] = L'\0'; + } + return static_cast< unsigned int >(n) >= N ? N - 1 : static_cast< unsigned int >(n); + } +}; +#endif // BOOST_LOG_USE_WCHAR_T + +template< typename CharT > +struct c_decorator_gen +{ + typedef CharT char_type; + + template< typename SubactorT > + BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const + { + typedef c_decorator_traits< char_type > traits_type; + typedef pattern_replacer< char_type > replacer_type; + typedef char_decorator_actor< SubactorT, replacer_type > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(traits_type::get_patterns(), traits_type::get_replacements())) }}; + return result_type(act); + } +}; + +} // namespace aux + +/*! + * C-style decorator generator object. The decorator replaces characters with specific meaning in C + * language with the corresponding escape sequences. The generator provides operator[] that + * can be used to construct the actual decorator. For example: + * + * + * c_decor[ attr< std::string >("MyAttr") ] + * + * + * For wide-character formatting there is the similar \c wc_decor decorator generator object. + */ +#ifdef BOOST_LOG_USE_CHAR +const aux::c_decorator_gen< char > c_decor = {}; +#endif +#ifdef BOOST_LOG_USE_WCHAR_T +const aux::c_decorator_gen< wchar_t > wc_decor = {}; +#endif + +/*! + * The function creates a C-style decorator generator for arbitrary character type. + */ +template< typename CharT > +BOOST_FORCEINLINE aux::c_decorator_gen< CharT > make_c_decor() +{ + return aux::c_decorator_gen< CharT >(); +} + +/*! + * A character decorator implementation that escapes all non-prontable and non-ASCII characters + * in the output with C-style escape sequences. + */ +template< typename CharT > +class c_ascii_pattern_replacer : + public pattern_replacer< CharT > +{ +private: + //! Base type + typedef pattern_replacer< CharT > base_type; + +public: + //! Result type + typedef typename base_type::result_type result_type; + //! Character type + typedef typename base_type::char_type char_type; + //! String type + typedef typename base_type::string_type string_type; + +private: + //! Traits type + typedef aux::c_decorator_traits< char_type > traits_type; + +public: + //! Default constructor + c_ascii_pattern_replacer() : base_type(traits_type::get_patterns(), traits_type::get_replacements()) + { + } + + //! Applies string replacements starting from the specified position + result_type operator() (string_type& str, typename string_type::size_type start_pos = 0) const + { + base_type::operator() (str, start_pos); + + typedef typename string_type::iterator string_iterator; + for (string_iterator it = str.begin() + start_pos, end = str.end(); it != end; ++it) + { + char_type c = *it; + if (c < 0x20 || c > 0x7e) + { + char_type buf[(std::numeric_limits< char_type >::digits + 3) / 4 + 3]; + std::size_t n = traits_type::print_escaped(buf, c); + std::size_t pos = it - str.begin(); + str.replace(pos, 1, buf, n); + it = str.begin() + n - 1; + end = str.end(); + } + } + } +}; + +namespace aux { + +template< typename CharT > +struct c_ascii_decorator_gen +{ + typedef CharT char_type; + + template< typename SubactorT > + BOOST_FORCEINLINE char_decorator_actor< SubactorT, c_ascii_pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const + { + typedef c_decorator_traits< char_type > traits_type; + typedef c_ascii_pattern_replacer< char_type > replacer_type; + typedef char_decorator_actor< SubactorT, replacer_type > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(subactor, replacer_type()) }}; + return result_type(act); + } +}; + +} // namespace aux + +/*! + * C-style decorator generator object. Acts similarly to \c c_decor, except that \c c_ascii_decor also + * converts all non-ASCII and non-printable ASCII characters, except for space character, into + * C-style hexadecimal escape sequences. The generator provides operator[] that + * can be used to construct the actual decorator. For example: + * + * + * c_ascii_decor[ attr< std::string >("MyAttr") ] + * + * + * For wide-character formatting there is the similar \c wc_ascii_decor decorator generator object. + */ +#ifdef BOOST_LOG_USE_CHAR +const aux::c_ascii_decorator_gen< char > c_ascii_decor = {}; +#endif +#ifdef BOOST_LOG_USE_WCHAR_T +const aux::c_ascii_decorator_gen< wchar_t > wc_ascii_decor = {}; +#endif + +/*! + * The function creates a C-style decorator generator for arbitrary character type. + */ +template< typename CharT > +BOOST_FORCEINLINE aux::c_ascii_decorator_gen< CharT > make_c_ascii_decor() +{ + return aux::c_ascii_decorator_gen< CharT >(); +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_C_DECORATOR_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/char_decorator.hpp b/boost/log/expressions/formatters/char_decorator.hpp new file mode 100644 index 0000000000..ca24c0d486 --- /dev/null +++ b/boost/log/expressions/formatters/char_decorator.hpp @@ -0,0 +1,639 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/char_decorator.hpp + * \author Andrey Semashev + * \date 17.11.2012 + * + * The header contains implementation of a character decorator. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +template< typename RangeT > +struct string_const_iterator : range_const_iterator< RangeT > {}; +template< > +struct string_const_iterator< char* > { typedef char* type; }; +template< > +struct string_const_iterator< const char* > { typedef const char* type; }; +template< > +struct string_const_iterator< wchar_t* > { typedef wchar_t* type; }; +template< > +struct string_const_iterator< const wchar_t* > { typedef const wchar_t* type; }; + +} // namespace aux + +/*! + * A simple character decorator implementation. This implementation replaces string patterns in the source string with + * the fixed replacements. Source patterns and replacements can be specified at the object construction. + */ +template< typename CharT > +class pattern_replacer +{ +public: + //! Result type + typedef void result_type; + + //! Character type + typedef CharT char_type; + //! String type + typedef std::basic_string< char_type > string_type; + +private: + //! Lengths of source pattern and replacement + struct string_lengths + { + unsigned int from_len, to_len; + }; + + //! List of the decorations to apply + typedef std::vector< string_lengths > string_lengths_list; + +private: + //! Characters of the interleaved source patterns and replacements + string_type m_decoration_chars; + //! List of the decorations to apply + string_lengths_list m_string_lengths; + +public: + /*! + * Initializing constructor. Creates a pattern replacer with the specified \a decorations. + * The provided decorations must be a sequence of \c std::pair of strings. The first element + * of each pair is the source pattern, and the second one is the corresponding replacement. + */ + template< typename RangeT > + explicit pattern_replacer(RangeT const& decorations) + { + typedef typename range_const_iterator< RangeT >::type iterator; + for (iterator it = begin(decorations), end_ = end(decorations); it != end_; ++it) + { + string_lengths lens; + { + typedef typename aux::string_const_iterator< typename range_value< RangeT >::type::first_type >::type first_iterator; + first_iterator b = string_begin(it->first), e = string_end(it->first); + lens.from_len = static_cast< unsigned int >(std::distance(b, e)); + m_decoration_chars.append(b, e); + } + { + typedef typename aux::string_const_iterator< typename range_value< RangeT >::type::second_type >::type second_iterator; + second_iterator b = string_begin(it->second), e = string_end(it->second); + lens.to_len = static_cast< unsigned int >(std::distance(b, e)); + m_decoration_chars.append(b, e); + } + m_string_lengths.push_back(lens); + } + } + /*! + * Initializing constructor. Creates a pattern replacer with decorations specified + * in form of two same-sized string sequences. Each i'th decoration will be + * from[i] -> to[i]. + */ + template< typename FromRangeT, typename ToRangeT > + pattern_replacer(FromRangeT const& from, ToRangeT const& to) + { + typedef typename range_const_iterator< FromRangeT >::type iterator1; + typedef typename range_const_iterator< ToRangeT >::type iterator2; + iterator1 it1 = begin(from), end1 = end(from); + iterator2 it2 = begin(to), end2 = end(to); + for (; it1 != end1 && it2 != end2; ++it1, ++it2) + { + string_lengths lens; + { + typedef typename aux::string_const_iterator< typename range_value< FromRangeT >::type >::type from_iterator; + from_iterator b = string_begin(*it1), e = string_end(*it1); + lens.from_len = static_cast< unsigned int >(std::distance(b, e)); + m_decoration_chars.append(b, e); + } + { + typedef typename aux::string_const_iterator< typename range_value< ToRangeT >::type >::type to_iterator; + to_iterator b = string_begin(*it2), e = string_end(*it2); + lens.to_len = static_cast< unsigned int >(std::distance(b, e)); + m_decoration_chars.append(b, e); + } + m_string_lengths.push_back(lens); + } + + // Both sequences should be of the same size + BOOST_ASSERT(it1 == end1); + BOOST_ASSERT(it2 == end2); + } + //! Copy constructor + pattern_replacer(pattern_replacer const& that) : m_decoration_chars(that.m_decoration_chars), m_string_lengths(that.m_string_lengths) + { + } + + //! Applies string replacements starting from the specified position + result_type operator() (string_type& str, typename string_type::size_type start_pos = 0) const + { + typedef typename string_type::size_type size_type; + + const char_type* from_chars = m_decoration_chars.c_str(); + for (typename string_lengths_list::const_iterator it = m_string_lengths.begin(), end = m_string_lengths.end(); it != end; ++it) + { + const unsigned int from_len = it->from_len, to_len = it->to_len; + const char_type* const to_chars = from_chars + from_len; + for (size_type pos = str.find(from_chars, start_pos, from_len); pos != string_type::npos; pos = str.find(from_chars, pos, from_len)) + { + str.replace(pos, from_len, to_chars, to_len); + pos += to_len; + } + from_chars = to_chars + to_len; + } + } + +private: + static char_type* string_begin(char_type* p) + { + return p; + } + static const char_type* string_begin(const char_type* p) + { + return p; + } + template< typename RangeT > + static typename range_const_iterator< RangeT >::type string_begin(RangeT const& r) + { + return begin(r); + } + + static char_type* string_end(char_type* p) + { + while (*p) + ++p; + return p; + } + static const char_type* string_end(const char_type* p) + { + while (*p) + ++p; + return p; + } + template< typename RangeT > + static typename range_const_iterator< RangeT >::type string_end(RangeT const& r) + { + return end(r); + } +}; + +namespace aux { + +//! Character decorator stream output terminal +template< typename LeftT, typename SubactorT, typename ImplT > +class char_decorator_output_terminal +{ +private: + //! Self type + typedef char_decorator_output_terminal< LeftT, SubactorT, ImplT > this_type; + +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Implementation type + typedef ImplT impl_type; + + //! Character type + typedef typename impl_type::char_type char_type; + //! String type + typedef typename impl_type::string_type string_type; + //! Adopted actor type + typedef SubactorT subactor_type; + + //! Result type definition + template< typename > + struct result; + + template< typename ThisT, typename ContextT > + struct result< ThisT(ContextT) > + { + typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type; + typedef typename phoenix::evaluator::impl< + typename LeftT::proto_base_expr&, + context_type, + phoenix::unused + >::result_type type; + }; + +private: + //! Left argument actor + LeftT m_left; + //! Adopted formatter actor + subactor_type m_subactor; + //! Implementation type + impl_type m_impl; + +public: + /*! + * Initializing constructor. Creates decorator of the \a fmt formatter with the specified \a decorations. + */ + char_decorator_output_terminal(LeftT const& left, subactor_type const& sub, impl_type const& impl) : + m_left(left), m_subactor(sub), m_impl(impl) + { + } + /*! + * Copy constructor + */ + char_decorator_output_terminal(char_decorator_output_terminal const& that) : + m_left(that.m_left), m_subactor(that.m_subactor), m_impl(that.m_impl) + { + } + + /*! + * Invokation operator + */ + template< typename ContextT > + typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx) + { + // Flush the stream and keep the current write position in the target string + typedef typename result< this_type(ContextT const&) >::type result_type; + result_type strm = phoenix::eval(m_left, ctx); + strm.flush(); + typename string_type::size_type const start_pos = strm.rdbuf()->storage()->size(); + + // Invoke the adopted formatter + typedef typename result< this_type(ContextT const&) >::type result_type; + phoenix::eval(m_subactor, ctx); + + // Flush the buffered characters and apply decorations + strm.flush(); + m_impl(*strm.rdbuf()->storage(), start_pos); + + return strm; + } + + /*! + * Invokation operator + */ + template< typename ContextT > + typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const + { + // Flush the stream and keep the current write position in the target string + typedef typename result< const this_type(ContextT const&) >::type result_type; + result_type strm = phoenix::eval(m_left, ctx); + strm.flush(); + typename string_type::size_type const start_pos = strm.rdbuf()->storage()->size(); + + // Invoke the adopted formatter + typedef typename result< const this_type(ContextT const&) >::type result_type; + phoenix::eval(m_subactor, ctx); + + // Flush the buffered characters and apply decorations + strm.flush(); + m_impl(*strm.rdbuf()->storage(), start_pos); + + return strm; + } + + BOOST_DELETED_FUNCTION(char_decorator_output_terminal()) +}; + +} // namespace aux + +/*! + * Character decorator terminal class. This formatter allows to modify strings generated by other + * formatters on character level. The most obvious application of decorators is replacing + * a certain set of characters with decorated equivalents to satisfy requirements of + * text-based sinks. + * + * The \c char_decorator_terminal class aggregates the formatter being decorated, and a set + * of string pairs that are used as decorations. All decorations are applied sequentially. + * The \c char_decorator_terminal class is a formatter itself, so it can be used to construct + * more complex formatters, including nesting decorators. + */ +template< typename SubactorT, typename ImplT > +class char_decorator_terminal +{ +private: + //! Self type + typedef char_decorator_terminal< SubactorT, ImplT > this_type; + +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Implementation type + typedef ImplT impl_type; + //! Character type + typedef typename impl_type::char_type char_type; + //! String type + typedef typename impl_type::string_type string_type; + //! Stream type + typedef basic_formatting_ostream< char_type > stream_type; + //! Adopted actor type + typedef SubactorT subactor_type; + + //! Result type definition + typedef string_type result_type; + +private: + //! Adopted formatter actor + subactor_type m_subactor; + //! Implementation + impl_type m_impl; + +public: + /*! + * Initializing constructor. + */ + char_decorator_terminal(subactor_type const& sub, impl_type const& impl) : m_subactor(sub), m_impl(impl) + { + } + /*! + * Copy constructor + */ + char_decorator_terminal(char_decorator_terminal const& that) : m_subactor(that.m_subactor), m_impl(that.m_impl) + { + } + + /*! + * \returns Adopted subactor + */ + subactor_type const& get_subactor() const + { + return m_subactor; + } + + /*! + * \returns Implementation + */ + impl_type const& get_impl() const + { + return m_impl; + } + + /*! + * Invokation operator + */ + template< typename ContextT > + result_type operator() (ContextT const& ctx) + { + string_type str; + stream_type strm(str); + + // Invoke the adopted formatter + typedef phoenix::vector3< + subactor_type*, + typename fusion::result_of::at_c< + typename remove_cv< + typename remove_reference< + typename phoenix::result_of::env< ContextT const& >::type + >::type + >::type::args_type, + 0 + >::type, + stream_type& + > env_type; + env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm }; + phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx))); + + // Flush the buffered characters and apply decorations + strm.flush(); + m_impl(*strm.rdbuf()->storage()); + + return boost::move(str); + } + + /*! + * Invokation operator + */ + template< typename ContextT > + result_type operator() (ContextT const& ctx) const + { + string_type str; + stream_type strm(str); + + // Invoke the adopted formatter + typedef phoenix::vector3< + const subactor_type*, + typename fusion::result_of::at_c< + typename remove_cv< + typename remove_reference< + typename phoenix::result_of::env< ContextT const& >::type + >::type + >::type::args_type, + 0 + >::type, + stream_type& + > env_type; + env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm }; + phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx))); + + // Flush the buffered characters and apply decorations + strm.flush(); + m_impl(*strm.rdbuf()->storage()); + + return boost::move(str); + } + + BOOST_DELETED_FUNCTION(char_decorator_terminal()) +}; + +/*! + * Character decorator actor + */ +template< typename SubactorT, typename ImplT, template< typename > class ActorT = phoenix::actor > +class char_decorator_actor : + public ActorT< char_decorator_terminal< SubactorT, ImplT > > +{ +public: + //! Base terminal type + typedef char_decorator_terminal< SubactorT, ImplT > terminal_type; + //! Character type + typedef typename terminal_type::char_type char_type; + + //! Base actor type + typedef ActorT< terminal_type > base_type; + +public: + //! Initializing constructor + explicit char_decorator_actor(base_type const& act) : base_type(act) + { + } + + //! Returns reference to the terminal + terminal_type const& get_terminal() const + { + return this->proto_expr_.child0; + } +}; + +#ifndef BOOST_LOG_DOXYGEN_PASS + +#define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\ + template< typename LeftExprT, typename SubactorT, typename ImplT, template< typename > class ActorT >\ + BOOST_FORCEINLINE phoenix::actor< aux::char_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, ImplT > >\ + operator<< (phoenix::actor< LeftExprT > left_ref left, char_decorator_actor< SubactorT, ImplT, ActorT > right_ref right)\ + {\ + typedef aux::char_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, ImplT > terminal_type;\ + phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_terminal().get_subactor(), right.get_terminal().get_impl()) }};\ + return actor;\ + } + +#include + +#undef BOOST_LOG_AUX_OVERLOAD + +#endif // BOOST_LOG_DOXYGEN_PASS + +namespace aux { + +template< typename RangeT > +class char_decorator_gen1 +{ + RangeT const& m_decorations; + + typedef typename boost::log::aux::deduce_char_type< typename range_value< RangeT >::type::first_type >::type char_type; + +public: + explicit char_decorator_gen1(RangeT const& decorations) : m_decorations(decorations) + { + } + + template< typename SubactorT > + BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const + { + typedef pattern_replacer< char_type > replacer_type; + typedef char_decorator_actor< SubactorT, replacer_type > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(m_decorations)) }}; + return result_type(act); + } +}; + +template< typename FromRangeT, typename ToRangeT > +class char_decorator_gen2 +{ + FromRangeT const& m_from; + ToRangeT const& m_to; + + typedef typename boost::log::aux::deduce_char_type< typename range_value< FromRangeT >::type >::type from_char_type; + typedef typename boost::log::aux::deduce_char_type< typename range_value< ToRangeT >::type >::type to_char_type; + 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"); + +public: + char_decorator_gen2(FromRangeT const& from, ToRangeT const& to) : m_from(from), m_to(to) + { + } + + template< typename SubactorT > + BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< from_char_type > > operator[] (SubactorT const& subactor) const + { + typedef pattern_replacer< from_char_type > replacer_type; + typedef char_decorator_actor< SubactorT, replacer_type > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(m_from, m_to)) }}; + return result_type(act); + } +}; + +} // namespace aux + +/*! + * The function returns a decorator generator object. The generator provides operator[] that can be used + * to construct the actual decorator. + * + * \param decorations A sequence of string pairs that will be used as decorations. Every decorations[i].first + * substring occurrence in the output will be replaced with decorations[i].second. + */ +template< typename RangeT > +BOOST_FORCEINLINE aux::char_decorator_gen1< RangeT > char_decor(RangeT const& decorations) +{ + return aux::char_decorator_gen1< RangeT >(decorations); +} + +/*! + * The function returns a decorator generator object. The generator provides operator[] that can be used + * to construct the actual decorator. + * + * \param from A sequence of strings that will be sought in the output. + * \param to A sequence of strings that will be used as replacements. + * + * \note The \a from and \a to sequences mush be of the same size. Every from[i] + * substring occurrence in the output will be replaced with to[i]. + */ +template< typename FromRangeT, typename ToRangeT > +BOOST_FORCEINLINE aux::char_decorator_gen2< FromRangeT, ToRangeT > char_decor(FromRangeT const& from, ToRangeT const& to) +{ + return aux::char_decorator_gen2< FromRangeT, ToRangeT >(from, to); +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +namespace result_of { + +template< typename SubactorT, typename ImplT > +struct is_nullary< custom_terminal< boost::log::expressions::char_decorator_terminal< SubactorT, ImplT > > > : + public mpl::false_ +{ +}; + +template< typename LeftT, typename SubactorT, typename ImplT > +struct is_nullary< custom_terminal< boost::log::expressions::aux::char_decorator_output_terminal< LeftT, SubactorT, ImplT > > > : + public mpl::false_ +{ +}; + +} // namespace result_of + +} // namespace phoenix + +#endif + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/csv_decorator.hpp b/boost/log/expressions/formatters/csv_decorator.hpp new file mode 100644 index 0000000000..8c964a0d13 --- /dev/null +++ b/boost/log/expressions/formatters/csv_decorator.hpp @@ -0,0 +1,140 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/csv_decorator.hpp + * \author Andrey Semashev + * \date 18.11.2012 + * + * The header contains implementation of a CSV-style character decorator. + * See: http://en.wikipedia.org/wiki/Comma-separated_values + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_ + +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +template< typename > +struct csv_decorator_traits; + +#ifdef BOOST_LOG_USE_CHAR +template< > +struct csv_decorator_traits< char > +{ + static boost::iterator_range< const char* const* > get_patterns() + { + static const char* const patterns[] = + { + "\"" + }; + return boost::make_iterator_range(patterns); + } + static boost::iterator_range< const char* const* > get_replacements() + { + static const char* const replacements[] = + { + "\"\"" + }; + return boost::make_iterator_range(replacements); + } +}; +#endif // BOOST_LOG_USE_CHAR + +#ifdef BOOST_LOG_USE_WCHAR_T +template< > +struct csv_decorator_traits< wchar_t > +{ + static boost::iterator_range< const wchar_t* const* > get_patterns() + { + static const wchar_t* const patterns[] = + { + L"\"" + }; + return boost::make_iterator_range(patterns); + } + static boost::iterator_range< const wchar_t* const* > get_replacements() + { + static const wchar_t* const replacements[] = + { + L"\"\"" + }; + return boost::make_iterator_range(replacements); + } +}; +#endif // BOOST_LOG_USE_WCHAR_T + +template< typename CharT > +struct csv_decorator_gen +{ + typedef CharT char_type; + + template< typename SubactorT > + BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const + { + typedef csv_decorator_traits< char_type > traits_type; + typedef pattern_replacer< char_type > replacer_type; + typedef char_decorator_actor< SubactorT, replacer_type > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(traits_type::get_patterns(), traits_type::get_replacements())) }}; + return result_type(act); + } +}; + +} // namespace aux + +/*! + * CSV-style decorator generator object. The decorator doubles double quotes that may be found + * in the output. See http://en.wikipedia.org/wiki/Comma-separated_values for more information on + * the CSV format. The generator provides operator[] that can be used to construct + * the actual decorator. For example: + * + * + * csv_decor[ attr< std::string >("MyAttr") ] + * + * + * For wide-character formatting there is the similar \c wcsv_decor decorator generator object. + */ +#ifdef BOOST_LOG_USE_CHAR +const aux::csv_decorator_gen< char > csv_decor = {}; +#endif +#ifdef BOOST_LOG_USE_WCHAR_T +const aux::csv_decorator_gen< wchar_t > wcsv_decor = {}; +#endif + +/*! + * The function creates an CSV-style decorator generator for arbitrary character type. + */ +template< typename CharT > +BOOST_FORCEINLINE aux::csv_decorator_gen< CharT > make_csv_decor() +{ + return aux::csv_decorator_gen< CharT >(); +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_CSV_DECORATOR_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/date_time.hpp b/boost/log/expressions/formatters/date_time.hpp new file mode 100644 index 0000000000..0ff6b680e6 --- /dev/null +++ b/boost/log/expressions/formatters/date_time.hpp @@ -0,0 +1,343 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/date_time.hpp + * \author Andrey Semashev + * \date 16.09.2012 + * + * The header contains a formatter function for date and time attribute values. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * Date and time formatter terminal. + */ +template< typename T, typename FallbackPolicyT, typename CharT > +class format_date_time_terminal +{ +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Attribute value type + typedef T value_type; + //! Fallback policy + typedef FallbackPolicyT fallback_policy; + //! Character type + typedef CharT char_type; + //! String type + typedef std::basic_string< char_type > string_type; + //! Formatting stream type + typedef basic_formatting_ostream< char_type > stream_type; + + //! Formatter function + typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type; + + //! Function result type + typedef string_type result_type; + +private: + //! Formatter generator traits + typedef aux::date_time_formatter_generator_traits< value_type, char_type > formatter_generator; + //! Attribute value visitor invoker + typedef value_visitor_invoker< value_type, fallback_policy > visitor_invoker_type; + +private: + //! Attribute name + attribute_name m_name; + //! Formattr function + formatter_function_type m_formatter; + //! Attribute value visitor invoker + visitor_invoker_type m_visitor_invoker; + +public: + //! Initializing constructor + format_date_time_terminal(attribute_name const& name, fallback_policy const& fallback, string_type const& format) : + m_name(name), m_formatter(formatter_generator::parse(format)), m_visitor_invoker(fallback) + { + } + //! Copy constructor + format_date_time_terminal(format_date_time_terminal const& that) : + m_name(that.m_name), m_formatter(that.m_formatter), m_visitor_invoker(that.m_visitor_invoker) + { + } + + //! Returns attribute name + attribute_name get_name() const + { + return m_name; + } + + //! Returns fallback policy + fallback_policy const& get_fallback_policy() const + { + return m_visitor_invoker.get_fallback_policy(); + } + + //! Retruns formatter function + formatter_function_type const& get_formatter_function() const + { + return m_formatter; + } + + //! Invokation operator + template< typename ContextT > + result_type operator() (ContextT const& ctx) + { + string_type str; + stream_type strm(str); + m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type&, stream_type& >(m_formatter, strm)); + strm.flush(); + return boost::move(str); + } + + //! Invokation operator + template< typename ContextT > + result_type operator() (ContextT const& ctx) const + { + string_type str; + stream_type strm(str); + m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type const&, stream_type& >(m_formatter, strm)); + strm.flush(); + return boost::move(str); + } + + BOOST_DELETED_FUNCTION(format_date_time_terminal()) +}; + +/*! + * Date and time formatter actor. + */ +template< typename T, typename FallbackPolicyT, typename CharT, template< typename > class ActorT = phoenix::actor > +class format_date_time_actor : + public ActorT< format_date_time_terminal< T, FallbackPolicyT, CharT > > +{ +public: + //! Attribute value type + typedef T value_type; + //! Character type + typedef CharT char_type; + //! Fallback policy + typedef FallbackPolicyT fallback_policy; + //! Base terminal type + typedef format_date_time_terminal< value_type, fallback_policy, char_type > terminal_type; + //! Formatter function + typedef typename terminal_type::formatter_function_type formatter_function_type; + + //! Base actor type + typedef ActorT< terminal_type > base_type; + +public: + //! Initializing constructor + explicit format_date_time_actor(base_type const& act) : base_type(act) + { + } + + /*! + * \returns The attribute name + */ + attribute_name get_name() const + { + return this->proto_expr_.child0.get_name(); + } + + /*! + * \returns Fallback policy + */ + fallback_policy const& get_fallback_policy() const + { + return this->proto_expr_.child0.get_fallback_policy(); + } + + /*! + * \returns Formatter function + */ + formatter_function_type const& get_formatter_function() const + { + return this->proto_expr_.child0.get_formatter_function(); + } +}; + +#ifndef BOOST_LOG_DOXYGEN_PASS + +#define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\ + template< typename LeftExprT, typename T, typename FallbackPolicyT, typename CharT >\ + BOOST_FORCEINLINE phoenix::actor< aux::attribute_output_terminal< phoenix::actor< LeftExprT >, T, FallbackPolicyT, typename format_date_time_actor< T, FallbackPolicyT, CharT >::formatter_function_type > >\ + operator<< (phoenix::actor< LeftExprT > left_ref left, format_date_time_actor< T, FallbackPolicyT, CharT > right_ref right)\ + {\ + typedef aux::attribute_output_terminal< phoenix::actor< LeftExprT >, T, FallbackPolicyT, typename format_date_time_actor< T, FallbackPolicyT, CharT >::formatter_function_type > terminal_type;\ + phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_name(), right.get_formatter_function(), right.get_fallback_policy()) }};\ + return actor;\ + } + +#include + +#undef BOOST_LOG_AUX_OVERLOAD + +#endif // BOOST_LOG_DOXYGEN_PASS + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param name Attribute name + * \param format Format string + */ +template< typename AttributeValueT, typename CharT > +BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, const CharT* format) +{ + typedef format_date_time_actor< AttributeValueT, fallback_to_none, CharT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param name Attribute name + * \param format Format string + */ +template< typename AttributeValueT, typename CharT > +BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, std::basic_string< CharT > const& format) +{ + typedef format_date_time_actor< AttributeValueT, fallback_to_none, CharT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param keyword Attribute keyword + * \param format Format string + */ +template< typename DescriptorT, template< typename > class ActorT, typename CharT > +BOOST_FORCEINLINE format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > +format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, const CharT* format) +{ + typedef format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param keyword Attribute keyword + * \param format Format string + */ +template< typename DescriptorT, template< typename > class ActorT, typename CharT > +BOOST_FORCEINLINE format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > +format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, std::basic_string< CharT > const& format) +{ + typedef format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param placeholder Attribute placeholder + * \param format Format string + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT > +BOOST_FORCEINLINE format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > +format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, const CharT* format) +{ + typedef format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param placeholder Attribute placeholder + * \param format Format string + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT > +BOOST_FORCEINLINE format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > +format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, std::basic_string< CharT > const& format) +{ + typedef format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), format) }}; + return actor_type(act); +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +namespace result_of { + +template< typename T, typename FallbackPolicyT, typename CharT > +struct is_nullary< custom_terminal< boost::log::expressions::format_date_time_terminal< T, FallbackPolicyT, CharT > > > : + public mpl::false_ +{ +}; + +} // namespace result_of + +} // namespace phoenix + +#endif // BOOST_LOG_DOXYGEN_PASS + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/format.hpp b/boost/log/expressions/formatters/format.hpp new file mode 100644 index 0000000000..c5d7917453 --- /dev/null +++ b/boost/log/expressions/formatters/format.hpp @@ -0,0 +1,128 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/format.hpp + * \author Andrey Semashev + * \date 15.11.2012 + * + * The header contains a generic log record formatter function. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_FORMAT_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_FORMAT_HPP_INCLUDED_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * \brief Template expressions terminal node with Boost.Format-like formatter + */ +template< typename CharT > +class format_terminal +{ +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Character type + typedef CharT char_type; + //! Boost.Format formatter type + typedef boost::log::aux::basic_format< char_type > format_type; + //! String type + typedef std::basic_string< char_type > string_type; + + //! Terminal result type + typedef typename format_type::pump result_type; + +private: + //! Formatter object + mutable format_type m_format; + +public: + //! Initializing constructor + explicit format_terminal(const char_type* format) : m_format(format) {} + + //! Invokation operator + template< typename ContextT > + result_type operator() (ContextT const& ctx) const + { + return m_format.make_pump(fusion::at_c< 1 >(phoenix::env(ctx).args())); + } + + BOOST_DELETED_FUNCTION(format_terminal()) +}; + +/*! + * The function generates a terminal node in a template expression. The node will perform log record formatting + * according to the provided format string. + */ +template< typename CharT > +BOOST_FORCEINLINE phoenix::actor< format_terminal< CharT > > format(const CharT* fmt) +{ + typedef format_terminal< CharT > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(fmt) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will perform log record formatting + * according to the provided format string. + */ +template< typename CharT, typename TraitsT, typename AllocatorT > +BOOST_FORCEINLINE phoenix::actor< format_terminal< CharT > > format(std::basic_string< CharT, TraitsT, AllocatorT > const& fmt) +{ + typedef format_terminal< CharT > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(fmt.c_str()) }}; + return act; +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +namespace result_of { + +template< typename CharT > +struct is_nullary< custom_terminal< boost::log::expressions::format_terminal< CharT > > > : + public mpl::false_ +{ +}; + +} // namespace result_of + +} // namespace phoenix + +#endif + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_FORMAT_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/if.hpp b/boost/log/expressions/formatters/if.hpp new file mode 100644 index 0000000000..0985439761 --- /dev/null +++ b/boost/log/expressions/formatters/if.hpp @@ -0,0 +1,315 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/if.hpp + * \author Andrey Semashev + * \date 17.11.2012 + * + * The header contains implementation of a conditional formatter. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +template< typename LeftT, typename CondT, typename ThenT > +class if_output_terminal +{ +private: + //! Self type + typedef if_output_terminal this_type; + +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Result type definition + template< typename > + struct result; + + template< typename ThisT, typename ContextT > + struct result< ThisT(ContextT) > + { + typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type; + typedef typename phoenix::evaluator::impl< + typename LeftT::proto_base_expr&, + context_type, + phoenix::unused + >::result_type type; + }; + +private: + //! Left argument actor + LeftT m_left; + //! Condition expression + CondT m_cond; + //! Positive branch + ThenT m_then; + +public: + //! Initializing constructor + if_output_terminal(LeftT const& left, CondT const& cond, ThenT const& then_) : m_left(left), m_cond(cond), m_then(then_) + { + } + + //! Invokation operator + template< typename ContextT > + typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx) + { + typedef typename result< this_type(ContextT const&) >::type result_type; + result_type strm = phoenix::eval(m_left, ctx); + if (phoenix::eval(m_cond, ctx)) + phoenix::eval(m_then, ctx); + return strm; + } + + //! Invokation operator + template< typename ContextT > + typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const + { + typedef typename result< const this_type(ContextT const&) >::type result_type; + result_type strm = phoenix::eval(m_left, ctx); + if (phoenix::eval(m_cond, ctx)) + phoenix::eval(m_then, ctx); + return strm; + } + + BOOST_DELETED_FUNCTION(if_output_terminal()) +}; + +template< typename LeftT, typename CondT, typename ThenT, typename ElseT > +class if_else_output_terminal +{ +private: + //! Self type + typedef if_else_output_terminal this_type; + +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Result type definition + template< typename > + struct result; + + template< typename ThisT, typename ContextT > + struct result< ThisT(ContextT) > + { + typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type; + typedef typename phoenix::evaluator::impl< + typename LeftT::proto_base_expr&, + context_type, + phoenix::unused + >::result_type type; + }; + +private: + //! Left argument actor + LeftT m_left; + //! Condition expression + CondT m_cond; + //! Positive branch + ThenT m_then; + //! Negative branch + ElseT m_else; + +public: + //! Initializing constructor + if_else_output_terminal(LeftT const& left, CondT const& cond, ThenT const& then_, ElseT const& else_) : m_left(left), m_cond(cond), m_then(then_), m_else(else_) + { + } + + //! Invokation operator + template< typename ContextT > + typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx) + { + typedef typename result< this_type(ContextT const&) >::type result_type; + result_type strm = phoenix::eval(m_left, ctx); + if (phoenix::eval(m_cond, ctx)) + phoenix::eval(m_then, ctx); + else + phoenix::eval(m_else, ctx); + return strm; + } + + //! Invokation operator + template< typename ContextT > + typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const + { + typedef typename result< const this_type(ContextT const&) >::type result_type; + result_type strm = phoenix::eval(m_left, ctx); + if (phoenix::eval(m_cond, ctx)) + phoenix::eval(m_then, ctx); + else + phoenix::eval(m_else, ctx); + return strm; + } + + BOOST_DELETED_FUNCTION(if_else_output_terminal()) +}; + + +template< typename CondT, typename ThenT, typename ElseT > +struct if_then_else_gen +{ + CondT m_cond; + ThenT m_then; + ElseT m_else; + + if_then_else_gen(CondT const& cond, ThenT const& then_, ElseT const& else_) : m_cond(cond), m_then(then_), m_else(else_) + { + } +}; + +#ifndef BOOST_LOG_DOXYGEN_PASS + +#define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\ + template< typename LeftExprT, typename CondT, typename ThenT, typename ElseT >\ + BOOST_FORCEINLINE phoenix::actor< if_else_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT, ElseT > >\ + operator<< (phoenix::actor< LeftExprT > left_ref left, if_then_else_gen< CondT, ThenT, ElseT > right_ref right)\ + {\ + typedef if_else_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT, ElseT > terminal_type;\ + phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.m_cond, right.m_then, right.m_else) }};\ + return actor;\ + } + +#include + +#undef BOOST_LOG_AUX_OVERLOAD + +#endif // BOOST_LOG_DOXYGEN_PASS + +template< typename CondT, typename ThenT > +struct if_then_gen +{ + struct else_gen + { + CondT m_cond; + ThenT m_then; + + else_gen(CondT const& cond, ThenT const& then_) : m_cond(cond), m_then(then_) + { + } + + template< typename ElseT > + BOOST_FORCEINLINE if_then_else_gen< CondT, ThenT, ElseT > operator[] (ElseT const& el) + { + return if_then_else_gen< CondT, ThenT, ElseT >(m_cond, m_then, el); + } + } + else_; + + if_then_gen(CondT const& cond, ThenT const& then_) : else_(cond, then_) {} +}; + +#ifndef BOOST_LOG_DOXYGEN_PASS + +#define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\ + template< typename LeftExprT, typename CondT, typename ThenT >\ + BOOST_FORCEINLINE phoenix::actor< if_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT > >\ + operator<< (phoenix::actor< LeftExprT > left_ref left, if_then_gen< CondT, ThenT > right_ref right)\ + {\ + typedef if_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT > terminal_type;\ + phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.else_.m_cond, right.else_.m_then) }};\ + return actor;\ + } + +#include + +#undef BOOST_LOG_AUX_OVERLOAD + +#endif // BOOST_LOG_DOXYGEN_PASS + +template< typename CondT > +class if_gen +{ +private: + CondT const& m_cond; + +public: + explicit if_gen(CondT const& cond) : m_cond(cond) + { + } + + template< typename ThenT > + BOOST_FORCEINLINE if_then_gen< CondT, ThenT > operator[] (ThenT const& then_) const + { + return if_then_gen< CondT, ThenT >(m_cond, then_); + } +}; + +} // namespace aux + +/*! + * The function returns a conditional formatter generator object. The generator provides operator[] that can be used + * to construct the actual formatter. The formatter must participate in a streaming expression. + * + * \param cond A filter expression that will be used as the condition + */ +template< typename CondT > +BOOST_FORCEINLINE aux::if_gen< CondT > if_(CondT const& cond) +{ + return aux::if_gen< CondT >(cond); +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +namespace result_of { + +template< typename LeftT, typename CondT, typename ThenT > +struct is_nullary< custom_terminal< boost::log::expressions::aux::if_output_terminal< LeftT, CondT, ThenT > > > : + public mpl::false_ +{ +}; + +template< typename LeftT, typename CondT, typename ThenT, typename ElseT > +struct is_nullary< custom_terminal< boost::log::expressions::aux::if_else_output_terminal< LeftT, CondT, ThenT, ElseT > > > : + public mpl::false_ +{ +}; + +} // namespace result_of + +} // namespace phoenix + +#endif + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/named_scope.hpp b/boost/log/expressions/formatters/named_scope.hpp new file mode 100644 index 0000000000..2225e00ec9 --- /dev/null +++ b/boost/log/expressions/formatters/named_scope.hpp @@ -0,0 +1,653 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/named_scope.hpp + * \author Andrey Semashev + * \date 11.11.2012 + * + * The header contains a formatter function for named scope attribute values. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_NAMED_SCOPE_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_NAMED_SCOPE_HPP_INCLUDED_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +//! Scope iteration directions +enum scope_iteration_direction +{ + forward, //!< Iterate through scopes from outermost to innermost + reverse //!< Iterate through scopes from innermost to outermost +}; + +namespace aux { + +#ifdef BOOST_LOG_USE_CHAR +//! Parses the named scope format string and constructs the formatter function +BOOST_LOG_API boost::log::aux::light_function< void (basic_formatting_ostream< char >&, attributes::named_scope::value_type::value_type const&) > +parse_named_scope_format(const char* begin, const char* end); +#endif + +#ifdef BOOST_LOG_USE_WCHAR_T +//! Parses the named scope format string and constructs the formatter function +BOOST_LOG_API boost::log::aux::light_function< void (basic_formatting_ostream< wchar_t >&, attributes::named_scope::value_type::value_type const&) > +parse_named_scope_format(const wchar_t* begin, const wchar_t* end); +#endif + +//! Parses the named scope format string and constructs the formatter function +template< typename CharT > +inline boost::log::aux::light_function< void (basic_formatting_ostream< CharT >&, attributes::named_scope::value_type::value_type const&) > +parse_named_scope_format(const CharT* format) +{ + return parse_named_scope_format(format, format + std::char_traits< CharT >::length(format)); +} + +//! Parses the named scope format string and constructs the formatter function +template< typename CharT, typename TraitsT, typename AllocatorT > +inline boost::log::aux::light_function< void (basic_formatting_ostream< CharT >&, attributes::named_scope::value_type::value_type const&) > +parse_named_scope_format(std::basic_string< CharT, TraitsT, AllocatorT > const& format) +{ + const CharT* p = format.c_str(); + return parse_named_scope_format(p, p + format.size()); +} + +//! Parses the named scope format string and constructs the formatter function +template< typename CharT, typename TraitsT > +inline boost::log::aux::light_function< void (basic_formatting_ostream< CharT >&, attributes::named_scope::value_type::value_type const&) > +parse_named_scope_format(basic_string_literal< CharT, TraitsT > const& format) +{ + const CharT* p = format.c_str(); + return parse_named_scope_format(p, p + format.size()); +} + +template< typename CharT > +class format_named_scope_impl +{ +public: + //! Function result type + typedef void result_type; + + //! Character type + typedef CharT char_type; + //! String type + typedef std::basic_string< char_type > string_type; + //! Formatting stream type + typedef basic_formatting_ostream< char_type > stream_type; + //! Attribute value type + typedef attributes::named_scope::value_type value_type; + //! Named scope formatter + typedef boost::log::aux::light_function< void (stream_type&, value_type::value_type const&) > element_formatter_type; + +private: + //! Element formatting function + element_formatter_type m_element_formatter; + //! Element delimiter + string_type m_delimiter; + //! Incomplete list marker + string_type m_incomplete_marker; + //! Empty list marker + string_type m_empty_marker; + //! Maximum number of elements to output + value_type::size_type m_depth; + //! Iteration direction + scope_iteration_direction m_direction; + +public: + //! Initializing constructor + format_named_scope_impl + ( + element_formatter_type const& element_formatter, + string_type const& delimiter, + string_type const& incomplete_marker, + string_type const& empty_marker, + value_type::size_type depth, + scope_iteration_direction direction + ) : + m_element_formatter(element_formatter), + m_delimiter(delimiter), + m_incomplete_marker(incomplete_marker), + m_empty_marker(empty_marker), + m_depth(depth), + m_direction(direction) + { + } + //! Copy constructor + format_named_scope_impl(format_named_scope_impl const& that) : + m_element_formatter(that.m_element_formatter), + m_delimiter(that.m_delimiter), + m_incomplete_marker(that.m_incomplete_marker), + m_empty_marker(that.m_empty_marker), + m_depth(that.m_depth), + m_direction(that.m_direction) + { + } + + //! Formatting operator + result_type operator() (stream_type& strm, value_type const& scopes) const + { + if (!scopes.empty()) + { + if (m_direction == expressions::forward) + format_forward(strm, scopes); + else + format_reverse(strm, scopes); + } + else + { + strm << m_empty_marker; + } + } + +private: + //! The function performs formatting of the extracted scope stack in forward direction + void format_forward(stream_type& strm, value_type const& scopes) const + { + value_type::const_iterator it, end = scopes.end(); + if (m_depth > 0) + { + value_type::size_type const scopes_to_iterate = (std::min)(m_depth, scopes.size()); + it = scopes.end(); + std::advance(it, -static_cast< value_type::difference_type >(scopes_to_iterate)); + } + else + { + it = scopes.begin(); + } + + if (it != end) + { + if (it != scopes.begin()) + strm << m_incomplete_marker; + + m_element_formatter(strm, *it); + for (++it; it != end; ++it) + { + strm << m_delimiter; + m_element_formatter(strm, *it); + } + } + } + //! The function performs formatting of the extracted scope stack in reverse direction + void format_reverse(stream_type& strm, value_type const& scopes) const + { + value_type::const_reverse_iterator it = scopes.rbegin(), end; + if (m_depth > 0) + { + value_type::size_type const scopes_to_iterate = (std::min)(m_depth, scopes.size()); + end = it; + std::advance(end, static_cast< value_type::difference_type >(scopes_to_iterate)); + } + else + { + end = scopes.rend(); + } + + if (it != end) + { + m_element_formatter(strm, *it); + for (++it; it != end; ++it) + { + strm << m_delimiter; + m_element_formatter(strm, *it); + } + + if (it != scopes.rend()) + strm << m_incomplete_marker; + } + } +}; + +} // namespace aux + +/*! + * Named scope formatter terminal. + */ +template< typename FallbackPolicyT, typename CharT > +class format_named_scope_terminal +{ +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Attribute value type + typedef attributes::named_scope::value_type value_type; + //! Fallback policy + typedef FallbackPolicyT fallback_policy; + //! Character type + typedef CharT char_type; + //! String type + typedef std::basic_string< char_type > string_type; + //! Formatting stream type + typedef basic_formatting_ostream< char_type > stream_type; + //! Formatter function + typedef aux::format_named_scope_impl< char_type > formatter_function_type; + + //! Function result type + typedef string_type result_type; + +private: + //! Attribute value visitor invoker + typedef value_visitor_invoker< value_type, fallback_policy > visitor_invoker_type; + +private: + //! Attribute name + attribute_name m_name; + //! Formatter function + formatter_function_type m_formatter; + //! Attribute value visitor invoker + visitor_invoker_type m_visitor_invoker; + +public: + //! Initializing constructor + template< typename FormatT > + format_named_scope_terminal + ( + attribute_name const& name, + fallback_policy const& fallback, + FormatT const& element_format, + string_type const& delimiter, + string_type const& incomplete_marker, + string_type const& empty_marker, + value_type::size_type depth, + scope_iteration_direction direction + ) : + m_name(name), m_formatter(aux::parse_named_scope_format(element_format), delimiter, incomplete_marker, empty_marker, depth, direction), m_visitor_invoker(fallback) + { + } + //! Copy constructor + format_named_scope_terminal(format_named_scope_terminal const& that) : + m_name(that.m_name), m_formatter(that.m_formatter), m_visitor_invoker(that.m_visitor_invoker) + { + } + + //! Returns attribute name + attribute_name get_name() const + { + return m_name; + } + + //! Returns fallback policy + fallback_policy const& get_fallback_policy() const + { + return m_visitor_invoker.get_fallback_policy(); + } + + //! Retruns formatter function + formatter_function_type const& get_formatter_function() const + { + return m_formatter; + } + + //! Invokation operator + template< typename ContextT > + result_type operator() (ContextT const& ctx) + { + string_type str; + stream_type strm(str); + m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type&, stream_type& >(m_formatter, strm)); + strm.flush(); + return boost::move(str); + } + + //! Invokation operator + template< typename ContextT > + result_type operator() (ContextT const& ctx) const + { + string_type str; + stream_type strm(str); + m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type const&, stream_type& >(m_formatter, strm)); + strm.flush(); + return boost::move(str); + } + + BOOST_DELETED_FUNCTION(format_named_scope_terminal()) +}; + +/*! + * Named scope formatter actor. + */ +template< typename FallbackPolicyT, typename CharT, template< typename > class ActorT = phoenix::actor > +class format_named_scope_actor : + public ActorT< format_named_scope_terminal< FallbackPolicyT, CharT > > +{ +public: + //! Character type + typedef CharT char_type; + //! Fallback policy + typedef FallbackPolicyT fallback_policy; + //! Base terminal type + typedef format_named_scope_terminal< fallback_policy, char_type > terminal_type; + //! Attribute value type + typedef typename terminal_type::value_type value_type; + //! Formatter function + typedef typename terminal_type::formatter_function_type formatter_function_type; + + //! Base actor type + typedef ActorT< terminal_type > base_type; + +public: + //! Initializing constructor + explicit format_named_scope_actor(base_type const& act) : base_type(act) + { + } + + /*! + * \returns The attribute name + */ + attribute_name get_name() const + { + return this->proto_expr_.child0.get_name(); + } + + /*! + * \returns Fallback policy + */ + fallback_policy const& get_fallback_policy() const + { + return this->proto_expr_.child0.get_fallback_policy(); + } + + /*! + * \returns Formatter function + */ + formatter_function_type const& get_formatter_function() const + { + return this->proto_expr_.child0.get_formatter_function(); + } +}; + +#ifndef BOOST_LOG_DOXYGEN_PASS + +#define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\ + template< typename LeftExprT, typename FallbackPolicyT, typename CharT >\ + 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 > >\ + operator<< (phoenix::actor< LeftExprT > left_ref left, format_named_scope_actor< FallbackPolicyT, CharT > right_ref right)\ + {\ + 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;\ + phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_name(), right.get_formatter_function(), right.get_fallback_policy()) }};\ + return actor;\ + } + +#include + +#undef BOOST_LOG_AUX_OVERLOAD + +#endif // BOOST_LOG_DOXYGEN_PASS + +namespace aux { + +//! Auxiliary traits to acquire default formatter parameters depending on the character type +template< typename CharT > +struct default_named_scope_params; + +#ifdef BOOST_LOG_USE_CHAR +template< > +struct default_named_scope_params< char > +{ + static const char* forward_delimiter() { return "->"; } + static const char* reverse_delimiter() { return "<-"; } + static const char* incomplete_marker() { return "..."; } + static const char* empty_marker() { return ""; } +}; +#endif +#ifdef BOOST_LOG_USE_WCHAR_T +template< > +struct default_named_scope_params< wchar_t > +{ + static const wchar_t* forward_delimiter() { return L"->"; } + static const wchar_t* reverse_delimiter() { return L"<-"; } + static const wchar_t* incomplete_marker() { return L"..."; } + static const wchar_t* empty_marker() { return L""; } +}; +#endif + +template< typename CharT, template< typename > class ActorT, typename FallbackPolicyT, typename ArgsT > +BOOST_FORCEINLINE format_named_scope_actor< FallbackPolicyT, CharT, ActorT > format_named_scope(attribute_name const& name, FallbackPolicyT const& fallback, ArgsT const& args) +{ + typedef format_named_scope_actor< FallbackPolicyT, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typedef default_named_scope_params< CharT > default_params; + scope_iteration_direction dir = args[keywords::iteration | expressions::forward]; + const CharT* default_delimiter = (dir == expressions::forward ? default_params::forward_delimiter() : default_params::reverse_delimiter()); + typename actor_type::base_type act = + {{ + terminal_type + ( + name, + fallback, + args[keywords::format], + args[keywords::delimiter | default_delimiter], + args[keywords::incomplete_marker | default_params::incomplete_marker()], + args[keywords::empty_marker | default_params::empty_marker()], + args[keywords::depth | static_cast< attributes::named_scope::value_type::size_type >(0)], + dir + ) + }}; + return actor_type(act); +} + +} // namespace aux + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param name Attribute name + * \param element_format Format string for a single named scope + */ +template< typename CharT > +BOOST_FORCEINLINE format_named_scope_actor< fallback_to_none, CharT > format_named_scope(attribute_name const& name, const CharT* element_format) +{ + typedef format_named_scope_actor< fallback_to_none, CharT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), element_format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param name Attribute name + * \param element_format Format string for a single named scope + */ +template< typename CharT > +BOOST_FORCEINLINE format_named_scope_actor< fallback_to_none, CharT > format_named_scope(attribute_name const& name, std::basic_string< CharT > const& element_format) +{ + typedef format_named_scope_actor< fallback_to_none, CharT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), element_format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param keyword Attribute keyword + * \param element_format Format string for a single named scope + */ +template< typename DescriptorT, template< typename > class ActorT, typename CharT > +BOOST_FORCEINLINE format_named_scope_actor< fallback_to_none, CharT, ActorT > +format_named_scope(attribute_keyword< DescriptorT, ActorT > const& keyword, const CharT* element_format) +{ + BOOST_STATIC_ASSERT_MSG((is_same< typename DescriptorT::value_type, attributes::named_scope::value_type >::value),\ + "Boost.Log: Named scope formatter only accepts attribute values of type attributes::named_scope::value_type."); + + typedef format_named_scope_actor< fallback_to_none, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), element_format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param keyword Attribute keyword + * \param element_format Format string for a single named scope + */ +template< typename DescriptorT, template< typename > class ActorT, typename CharT > +BOOST_FORCEINLINE format_named_scope_actor< fallback_to_none, CharT, ActorT > +format_named_scope(attribute_keyword< DescriptorT, ActorT > const& keyword, std::basic_string< CharT > const& element_format) +{ + BOOST_STATIC_ASSERT_MSG((is_same< typename DescriptorT::value_type, attributes::named_scope::value_type >::value),\ + "Boost.Log: Named scope formatter only accepts attribute values of type attributes::named_scope::value_type."); + + typedef format_named_scope_actor< fallback_to_none, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), element_format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param placeholder Attribute placeholder + * \param element_format Format string for a single named scope + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT > +BOOST_FORCEINLINE format_named_scope_actor< FallbackPolicyT, CharT, ActorT > +format_named_scope(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, const CharT* element_format) +{ + BOOST_STATIC_ASSERT_MSG((is_same< T, attributes::named_scope::value_type >::value),\ + "Boost.Log: Named scope formatter only accepts attribute values of type attributes::named_scope::value_type."); + + typedef format_named_scope_actor< FallbackPolicyT, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), element_format) }}; + return actor_type(act); +} + +/*! + * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting + * expression (stream output or \c format placeholder filler). + * + * \param placeholder Attribute placeholder + * \param element_format Format string for a single named scope + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT > +BOOST_FORCEINLINE format_named_scope_actor< FallbackPolicyT, CharT, ActorT > +format_named_scope(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, std::basic_string< CharT > const& element_format) +{ + BOOST_STATIC_ASSERT_MSG((is_same< T, attributes::named_scope::value_type >::value),\ + "Boost.Log: Named scope formatter only accepts attribute values of type attributes::named_scope::value_type."); + + typedef format_named_scope_actor< FallbackPolicyT, CharT, ActorT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), element_format) }}; + return actor_type(act); +} + +#if !defined(BOOST_LOG_DOXYGEN_PASS) + +# define BOOST_PP_FILENAME_1 +# define BOOST_PP_ITERATION_LIMITS (1, 6) +# include BOOST_PP_ITERATE() + +#else // BOOST_LOG_DOXYGEN_PASS + +/*! + * Formatter generator. Construct the named scope formatter with the specified formatting parameters. + * + * \param name Attribute name + * \param args An set of named parameters. Supported parameters: + * \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. + * \li \c delimiter - A string that is used to delimit the formatted scope names. Default: "->" or "<-", depending on the iteration direction. + * \li \c incomplete_marker - A string that is used to indicate that the list was printed incomplete because of depth limitation. Default: "...". + * \li \c empty_marker - A string that is output in case if the scope list is empty. Default: "", i.e. nothing is output. + * \li \c iteration - Iteration direction, see \c scope_iteration_direction enumeration. Default: forward. + * \li \c depth - Iteration depth. Default: unlimited. + */ +template< typename... ArgsT > +unspecified format_named_scope(attribute_name const& name, ArgsT... const& args); + +/*! \overload */ +template< typename DescriptorT, template< typename > class ActorT, typename... ArgsT > +unspecified format_named_scope(attribute_keyword< DescriptorT, ActorT > const& keyword, ArgsT... const& args); + +/*! \overload */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename... ArgsT > +unspecified format_named_scope(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, ArgsT... const& args); + +#endif // BOOST_LOG_DOXYGEN_PASS + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +namespace result_of { + +template< typename FallbackPolicyT, typename CharT > +struct is_nullary< custom_terminal< boost::log::expressions::format_named_scope_terminal< FallbackPolicyT, CharT > > > : + public mpl::false_ +{ +}; + +} // namespace result_of + +} // namespace phoenix + +#endif + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_NAMED_SCOPE_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/stream.hpp b/boost/log/expressions/formatters/stream.hpp new file mode 100644 index 0000000000..cd5f1d36c3 --- /dev/null +++ b/boost/log/expressions/formatters/stream.hpp @@ -0,0 +1,53 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file stream.hpp + * \author Andrey Semashev + * \date 24.07.2012 + * + * The header contains implementation of a stream placeholder in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_ + +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * Stream placeholder type in formatter template expressions. + */ +typedef phoenix::expression::argument< 2 >::type stream_type; + +/*! + * Stream placeholder in formatter template expressions. + */ +const stream_type stream = {}; + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include +#if defined(BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_) +#include +#endif + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/wrap_formatter.hpp b/boost/log/expressions/formatters/wrap_formatter.hpp new file mode 100644 index 0000000000..7d8a22c4fc --- /dev/null +++ b/boost/log/expressions/formatters/wrap_formatter.hpp @@ -0,0 +1,338 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/wrap_formatter.hpp + * \author Andrey Semashev + * \date 24.11.2012 + * + * The header contains a formatter function wrapper that enables third-party functions to participate in formatting expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_WRAP_FORMATTER_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_WRAP_FORMATTER_HPP_INCLUDED_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +//! Wrapped formatter stream output terminal +template< typename LeftT, typename FunT > +class wrapped_formatter_output_terminal +{ +private: + //! Self type + typedef wrapped_formatter_output_terminal< LeftT, FunT > this_type; + +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Wrapped function type + typedef FunT function_type; + + //! Result type definition + template< typename > + struct result; + + template< typename ThisT, typename ContextT > + struct result< ThisT(ContextT) > + { + typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type; + typedef typename phoenix::evaluator::impl< + typename LeftT::proto_base_expr&, + context_type, + phoenix::unused + >::result_type type; + }; + +private: + //! Left argument actor + LeftT m_left; + //! Wrapped function + function_type m_fun; + +public: + //! Initializing constructor + wrapped_formatter_output_terminal(LeftT const& left, function_type const& fun) : m_left(left), m_fun(fun) + { + } + //! Copy constructor + wrapped_formatter_output_terminal(wrapped_formatter_output_terminal const& that) : m_left(that.m_left), m_fun(that.m_fun) + { + } + + //! Invokation operator + template< typename ContextT > + typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx) + { + typedef typename result< this_type(ContextT const&) >::type result_type; + result_type strm = phoenix::eval(m_left, ctx); + m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()), strm); + return strm; + } + + //! Invokation operator + template< typename ContextT > + typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const + { + typedef typename result< const this_type(ContextT const&) >::type result_type; + result_type strm = phoenix::eval(m_left, ctx); + m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()), strm); + return strm; + } + + BOOST_DELETED_FUNCTION(wrapped_formatter_output_terminal()) +}; + +BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_char_type, char_type, false) + +template< + typename FunT, + bool HasCharTypeV = has_char_type< FunT >::value, + bool HasSecondArgumentV = boost::log::aux::has_second_argument_type< FunT >::value, + bool HasArg2V = boost::log::aux::has_arg2_type< FunT >::value +> +struct default_char_type +{ + // Use this char type if all detection fails + typedef char type; +}; + +template< typename FunT, bool HasSecondArgumentV, bool HasArg2V > +struct default_char_type< FunT, true, HasSecondArgumentV, HasArg2V > +{ + typedef typename FunT::char_type type; +}; + +template< typename FunT, bool HasArg2V > +struct default_char_type< FunT, false, true, HasArg2V > +{ + typedef typename remove_cv< typename remove_reference< typename FunT::second_argument_type >::type >::type argument_type; + typedef typename argument_type::char_type type; +}; + +template< typename FunT > +struct default_char_type< FunT, false, false, true > +{ + typedef typename remove_cv< typename remove_reference< typename FunT::arg2_type >::type >::type argument_type; + typedef typename argument_type::char_type type; +}; + +} // namespace aux + +/*! + * Formatter function wrapper terminal. + */ +template< typename FunT, typename CharT > +class wrapped_formatter_terminal +{ +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Character type + typedef CharT char_type; + //! String type + typedef std::basic_string< char_type > string_type; + //! Formatting stream type + typedef basic_formatting_ostream< char_type > stream_type; + //! Wrapped function type + typedef FunT function_type; + + //! Formatter result type + typedef string_type result_type; + +private: + //! Wrapped function + function_type m_fun; + +public: + //! Initializing construction + explicit wrapped_formatter_terminal(function_type const& fun) : m_fun(fun) + { + } + //! Copy constructor + wrapped_formatter_terminal(wrapped_formatter_terminal const& that) : m_fun(that.m_fun) + { + } + + //! Returns the wrapped function + function_type const& get_function() const + { + return m_fun; + } + + //! Invokation operator + template< typename ContextT > + result_type operator() (ContextT const& ctx) + { + string_type str; + stream_type strm(str); + m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()), strm); + strm.flush(); + return boost::move(str); + } + + //! Invokation operator + template< typename ContextT > + result_type operator() (ContextT const& ctx) const + { + string_type str; + stream_type strm(str); + m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()), strm); + strm.flush(); + return boost::move(str); + } +}; + +/*! + * Wrapped formatter function actor. + */ +template< typename FunT, typename CharT, template< typename > class ActorT = phoenix::actor > +class wrapped_formatter_actor : + public ActorT< wrapped_formatter_terminal< FunT, CharT > > +{ +public: + //! Character type + typedef CharT char_type; + //! Wrapped function type + typedef FunT function_type; + //! Base terminal type + typedef wrapped_formatter_terminal< function_type, char_type > terminal_type; + + //! Base actor type + typedef ActorT< terminal_type > base_type; + +public: + //! Initializing constructor + explicit wrapped_formatter_actor(base_type const& act) : base_type(act) + { + } + + /*! + * \returns The wrapped function + */ + function_type const& get_function() const + { + return this->proto_expr_.child0.get_function(); + } +}; + +#ifndef BOOST_LOG_DOXYGEN_PASS + +#define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\ + template< typename LeftExprT, typename FunT, typename CharT >\ + BOOST_FORCEINLINE phoenix::actor< aux::wrapped_formatter_output_terminal< phoenix::actor< LeftExprT >, FunT > >\ + operator<< (phoenix::actor< LeftExprT > left_ref left, wrapped_formatter_actor< FunT, CharT > right_ref right)\ + {\ + typedef aux::wrapped_formatter_output_terminal< phoenix::actor< LeftExprT >, FunT > terminal_type;\ + phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_function()) }};\ + return actor;\ + } + +#include + +#undef BOOST_LOG_AUX_OVERLOAD + +#endif // BOOST_LOG_DOXYGEN_PASS + +/*! + * The function wraps a function object in order it to be able to participate in formatting expressions. The wrapped + * function object must be compatible with the following signature: + * + *
+ * void (record_view const&, basic_formatting_ostream< CharT >&)
+ * 
+ * + * where \c CharT is the character type of the formatting expression. + */ +template< typename FunT > +BOOST_FORCEINLINE wrapped_formatter_actor< FunT, typename aux::default_char_type< FunT >::type > wrap_formatter(FunT const& fun) +{ + typedef wrapped_formatter_actor< FunT, typename aux::default_char_type< FunT >::type > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(fun) }}; + return actor_type(act); +} + +/*! + * The function wraps a function object in order it to be able to participate in formatting expressions. The wrapped + * function object must be compatible with the following signature: + * + *
+ * void (record_view const&, basic_formatting_ostream< CharT >&)
+ * 
+ * + * where \c CharT is the character type of the formatting expression. + */ +template< typename CharT, typename FunT > +BOOST_FORCEINLINE wrapped_formatter_actor< FunT, CharT > wrap_formatter(FunT const& fun) +{ + typedef wrapped_formatter_actor< FunT, CharT > actor_type; + typedef typename actor_type::terminal_type terminal_type; + typename actor_type::base_type act = {{ terminal_type(fun) }}; + return actor_type(act); +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +namespace result_of { + +template< typename LeftT, typename FunT > +struct is_nullary< custom_terminal< boost::log::expressions::aux::wrapped_formatter_output_terminal< LeftT, FunT > > > : + public mpl::false_ +{ +}; + +template< typename FunT, typename CharT > +struct is_nullary< custom_terminal< boost::log::expressions::wrapped_formatter_terminal< FunT, CharT > > > : + public mpl::false_ +{ +}; + +} // namespace result_of + +} // namespace phoenix + +#endif + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_WRAP_FORMATTER_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters/xml_decorator.hpp b/boost/log/expressions/formatters/xml_decorator.hpp new file mode 100644 index 0000000000..021ed60bd4 --- /dev/null +++ b/boost/log/expressions/formatters/xml_decorator.hpp @@ -0,0 +1,138 @@ +/* + * Copyright Andrey Semashev 2007 - 2014. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file formatters/xml_decorator.hpp + * \author Andrey Semashev + * \date 18.11.2012 + * + * The header contains implementation of a XML-style character decorator. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_ + +#include +#include +#include +#include + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +template< typename > +struct xml_decorator_traits; + +#ifdef BOOST_LOG_USE_CHAR +template< > +struct xml_decorator_traits< char > +{ + static boost::iterator_range< const char* const* > get_patterns() + { + static const char* const patterns[] = + { + "&", "<", ">", "\"", "'" + }; + return boost::make_iterator_range(patterns); + } + static boost::iterator_range< const char* const* > get_replacements() + { + static const char* const replacements[] = + { + "&", "<", ">", """, "'" + }; + return boost::make_iterator_range(replacements); + } +}; +#endif // BOOST_LOG_USE_CHAR + +#ifdef BOOST_LOG_USE_WCHAR_T +template< > +struct xml_decorator_traits< wchar_t > +{ + static boost::iterator_range< const wchar_t* const* > get_patterns() + { + static const wchar_t* const patterns[] = + { + L"&", L"<", L">", L"\"", L"'" + }; + return boost::make_iterator_range(patterns); + } + static boost::iterator_range< const wchar_t* const* > get_replacements() + { + static const wchar_t* const replacements[] = + { + L"&", L"<", L">", L""", L"'" + }; + return boost::make_iterator_range(replacements); + } +}; +#endif // BOOST_LOG_USE_WCHAR_T + +template< typename CharT > +struct xml_decorator_gen +{ + typedef CharT char_type; + + template< typename SubactorT > + BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const + { + typedef xml_decorator_traits< char_type > traits_type; + typedef pattern_replacer< char_type > replacer_type; + typedef char_decorator_actor< SubactorT, replacer_type > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(traits_type::get_patterns(), traits_type::get_replacements())) }}; + return result_type(act); + } +}; + +} // namespace aux + +/*! + * XML-style decorator generator object. The decorator replaces characters that have special meaning + * in XML documents with the corresponding decorated counterparts. The generator provides + * operator[] that can be used to construct the actual decorator. For example: + * + * + * xml_decor[ attr< std::string >("MyAttr") ] + * + * + * For wide-character formatting there is the similar \c wxml_decor decorator generator object. + */ +#ifdef BOOST_LOG_USE_CHAR +const aux::xml_decorator_gen< char > xml_decor = {}; +#endif +#ifdef BOOST_LOG_USE_WCHAR_T +const aux::xml_decorator_gen< wchar_t > wxml_decor = {}; +#endif + +/*! + * The function creates an XML-style decorator generator for arbitrary character type. + */ +template< typename CharT > +BOOST_FORCEINLINE aux::xml_decorator_gen< CharT > make_xml_decor() +{ + return aux::xml_decorator_gen< CharT >(); +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_ -- cgit v1.2.3