diff options
Diffstat (limited to 'boost/log/expressions')
29 files changed, 5933 insertions, 0 deletions
diff --git a/boost/log/expressions/attr.hpp b/boost/log/expressions/attr.hpp new file mode 100644 index 0000000000..31deefc0a9 --- /dev/null +++ b/boost/log/expressions/attr.hpp @@ -0,0 +1,276 @@ +/* + * 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 attr.hpp + * \author Andrey Semashev + * \date 21.07.2012 + * + * The header contains implementation of a generic attribute placeholder in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_ + +#include <boost/mpl/bool.hpp> +#include <boost/utility/result_of.hpp> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/terminal_fwd.hpp> +#include <boost/phoenix/core/is_nullary.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/fusion/sequence/intrinsic/at_c.hpp> +#include <boost/type_traits/remove_cv.hpp> +#include <boost/type_traits/remove_reference.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/copy_cv.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/value_extraction.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * An attribute value extraction terminal + */ +template< typename T, typename FallbackPolicyT, typename TagT > +class attribute_terminal +{ +private: + //! Value extractor type + typedef value_extractor< T, FallbackPolicyT, TagT > value_extractor_type; + //! Self type + typedef attribute_terminal< T, FallbackPolicyT, TagT > this_type; + +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Attribute tag type + typedef TagT tag_type; + //! Attribute value type + typedef typename value_extractor_type::value_type value_type; + //! Fallback policy type + typedef typename value_extractor_type::fallback_policy fallback_policy; + + //! Function result type + template< typename > + struct result; + + template< typename ThisT, typename ContextT > + struct result< ThisT(ContextT) > + { + typedef typename remove_cv< + typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type + >::type env_type; + typedef typename env_type::args_type args_type; + typedef typename boost::log::aux::copy_cv< ThisT, value_extractor_type >::type cv_value_extractor_type; + + typedef typename boost::result_of< cv_value_extractor_type(attribute_name const&, typename fusion::result_of::at_c< args_type, 0 >::type) >::type type; + }; + +private: + //! Attribute value name + const attribute_name m_name; + //! Attribute value extractor + value_extractor_type m_value_extractor; + +public: + /*! + * Initializing constructor + */ + explicit attribute_terminal(attribute_name const& name) : m_name(name) + { + } + + /*! + * Initializing constructor + */ + template< typename U > + attribute_terminal(attribute_name const& name, U const& arg) : m_name(name), m_value_extractor(arg) + { + } + + /*! + * \returns Attribute value name + */ + attribute_name get_name() const + { + return m_name; + } + + /*! + * \returns Fallback policy + */ + fallback_policy const& get_fallback_policy() const + { + return m_value_extractor.get_fallback_policy(); + } + + /*! + * The operator extracts attribute value + */ + template< typename ContextT > + typename result< this_type(ContextT const&) >::type + operator() (ContextT const& ctx) + { + return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args())); + } + + /*! + * The operator extracts attribute value + */ + template< typename ContextT > + typename result< const this_type(ContextT const&) >::type + operator() (ContextT const& ctx) const + { + return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args())); + } + + BOOST_DELETED_FUNCTION(attribute_terminal()) +}; + +/*! + * An attribute value extraction terminal actor + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT > +class attribute_actor : + public ActorT< attribute_terminal< T, FallbackPolicyT, TagT > > +{ +public: + //! Attribute tag type + typedef TagT tag_type; + //! Fallback policy + typedef FallbackPolicyT fallback_policy; + //! Base terminal type + typedef attribute_terminal< T, fallback_policy, tag_type > terminal_type; + //! Attribute value type + typedef typename terminal_type::value_type value_type; + + //! Base actor type + typedef ActorT< terminal_type > base_type; + +public: + //! Initializing constructor + explicit attribute_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(); + } + + //! Expression with cached attribute name + typedef attribute_actor< value_type, fallback_to_none, tag_type, ActorT > or_none_result_type; + + //! Generates an expression that extracts the attribute value or a default value + or_none_result_type or_none() const + { + typedef typename or_none_result_type::terminal_type result_terminal; + typename or_none_result_type::base_type act = {{ result_terminal(get_name()) }}; + return or_none_result_type(act); + } + + //! Expression with cached attribute name + typedef attribute_actor< value_type, fallback_to_throw, tag_type, ActorT > or_throw_result_type; + + //! Generates an expression that extracts the attribute value or throws an exception + or_throw_result_type or_throw() const + { + typedef typename or_throw_result_type::terminal_type result_terminal; + typename or_throw_result_type::base_type act = {{ result_terminal(get_name()) }}; + return or_throw_result_type(act); + } + + //! Generates an expression that extracts the attribute value or a default value + template< typename DefaultT > + attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default(DefaultT const& def_val) const + { + typedef attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default_result_type; + typedef typename or_default_result_type::terminal_type result_terminal; + typename or_default_result_type::base_type act = {{ result_terminal(get_name(), def_val) }}; + return or_default_result_type(act); + } +}; + +/*! + * The function generates a terminal node in a template expression. The node will extract the value of the attribute + * with the specified name and type. + */ +template< typename AttributeValueT > +BOOST_FORCEINLINE attribute_actor< AttributeValueT > attr(attribute_name const& name) +{ + typedef attribute_actor< AttributeValueT > result_type; + typedef typename result_type::terminal_type result_terminal; + typename result_type::base_type act = {{ result_terminal(name) }}; + return result_type(act); +} + +/*! + * The function generates a terminal node in a template expression. The node will extract the value of the attribute + * with the specified name and type. + */ +template< typename AttributeValueT, typename TagT > +BOOST_FORCEINLINE attribute_actor< AttributeValueT, fallback_to_none, TagT > attr(attribute_name const& name) +{ + typedef attribute_actor< AttributeValueT, fallback_to_none, TagT > result_type; + typedef typename result_type::terminal_type result_terminal; + typename result_type::base_type act = {{ result_terminal(name) }}; + return result_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 TagT > +struct is_nullary< custom_terminal< boost::log::expressions::attribute_terminal< T, FallbackPolicyT, TagT > > > : + public mpl::false_ +{ +}; + +} // namespace result_of + +} // namespace phoenix + +#endif + +} // namespace boost + +#include <boost/log/detail/footer.hpp> +#if defined(BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_) +#include <boost/log/detail/attr_output_impl.hpp> +#endif + +#endif // BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_ diff --git a/boost/log/expressions/attr_fwd.hpp b/boost/log/expressions/attr_fwd.hpp new file mode 100644 index 0000000000..a8e7b92a81 --- /dev/null +++ b/boost/log/expressions/attr_fwd.hpp @@ -0,0 +1,69 @@ +/* + * 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 attr_fwd.hpp + * \author Andrey Semashev + * \date 21.07.2012 + * + * The header contains forward declaration of a generic attribute placeholder in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_ATTR_FWD_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_ATTR_FWD_HPP_INCLUDED_ + +#include <boost/log/detail/config.hpp> +#include <boost/log/attributes/fallback_policy_fwd.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +template< typename > +struct actor; + +} // namespace phoenix + +#endif + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * An attribute value extraction terminal + */ +template< + typename T, + typename FallbackPolicyT = fallback_to_none, + typename TagT = void +> +class attribute_terminal; + +/*! + * An attribute value extraction terminal actor + */ +template< + typename T, + typename FallbackPolicyT = fallback_to_none, + typename TagT = void, + template< typename > class ActorT = phoenix::actor +> +class attribute_actor; + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#endif // BOOST_LOG_EXPRESSIONS_ATTR_FWD_HPP_INCLUDED_ diff --git a/boost/log/expressions/filter.hpp b/boost/log/expressions/filter.hpp new file mode 100644 index 0000000000..dcf7d59cde --- /dev/null +++ b/boost/log/expressions/filter.hpp @@ -0,0 +1,183 @@ +/* + * 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 filter.hpp + * \author Andrey Semashev + * \date 13.07.2012 + * + * The header contains a filter function object definition. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FILTER_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FILTER_HPP_INCLUDED_ + +#include <boost/move/core.hpp> +#include <boost/move/utility.hpp> +#include <boost/utility/enable_if.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/attributes/attribute_value_set.hpp> +#include <boost/log/detail/light_function.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +/*! + * Log record filter function wrapper. + */ +class filter +{ + BOOST_COPYABLE_AND_MOVABLE(filter) + +public: + //! Result type + typedef bool result_type; + +private: + //! Filter function type + typedef boost::log::aux::light_function< bool (attribute_value_set const&) > filter_type; + + //! Default filter, always returns \c true + struct default_filter + { + typedef bool result_type; + result_type operator() (attribute_value_set const&) const { return true; } + }; + +private: + //! Filter function + filter_type m_Filter; + +public: + /*! + * Default constructor. Creates a filter that always returns \c true. + */ + filter() : m_Filter(default_filter()) + { + } + /*! + * Copy constructor + */ + filter(filter const& that) : m_Filter(that.m_Filter) + { + } + /*! + * Move constructor. The moved-from filter is left in an unspecified state. + */ + filter(BOOST_RV_REF(filter) that) BOOST_NOEXCEPT : m_Filter(boost::move(that.m_Filter)) + { + } + + /*! + * Initializing constructor. Creates a filter which will invoke the specified function object. + */ +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + template< typename FunT > + filter(FunT&& fun) : m_Filter(boost::forward< FunT >(fun)) + { + } +#elif !defined(BOOST_MSVC) || BOOST_MSVC > 1400 + template< typename FunT > + filter(FunT const& fun, typename disable_if_c< move_detail::is_rv< FunT >::value, int >::type = 0) : m_Filter(fun) + { + } +#else + // MSVC 8 blows up in unexpected ways if we use SFINAE to disable constructor instantiation + template< typename FunT > + filter(FunT const& fun) : m_Filter(fun) + { + } + template< typename FunT > + filter(rv< FunT >& fun) : m_Filter(fun) + { + } + template< typename FunT > + filter(rv< FunT > const& fun) : m_Filter(static_cast< FunT const& >(fun)) + { + } + filter(rv< filter > const& that) : m_Filter(that.m_Filter) + { + } +#endif + + /*! + * Move assignment. The moved-from filter is left in an unspecified state. + */ + filter& operator= (BOOST_RV_REF(filter) that) BOOST_NOEXCEPT + { + m_Filter.swap(that.m_Filter); + return *this; + } + /*! + * Copy assignment. + */ + filter& operator= (BOOST_COPY_ASSIGN_REF(filter) that) + { + m_Filter = that.m_Filter; + return *this; + } + /*! + * Initializing assignment. Sets the specified function object to the filter. + */ +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + template< typename FunT > + filter& operator= (FunT const& fun) +#else + template< typename FunT > + typename disable_if< is_same< typename remove_cv< FunT >::type, filter >, filter& >::type + operator= (FunT const& fun) +#endif + { + filter(fun).swap(*this); + return *this; + } + + /*! + * Filtering operator. + * + * \param values Attribute values of the log record. + * \return \c true if the log record passes the filter, \c false otherwise. + */ + result_type operator() (attribute_value_set const& values) const + { + return m_Filter(values); + } + + /*! + * Resets the filter to the default. The default filter always returns \c true. + */ + void reset() + { + m_Filter = default_filter(); + } + + /*! + * Swaps two filters + */ + void swap(filter& that) BOOST_NOEXCEPT + { + m_Filter.swap(that.m_Filter); + } +}; + +inline void swap(filter& left, filter& right) BOOST_NOEXCEPT +{ + left.swap(right); +} + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_FILTER_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatter.hpp b/boost/log/expressions/formatter.hpp new file mode 100644 index 0000000000..05692ec287 --- /dev/null +++ b/boost/log/expressions/formatter.hpp @@ -0,0 +1,257 @@ +/* + * 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 formatter.hpp + * \author Andrey Semashev + * \date 13.07.2012 + * + * The header contains a formatter function object definition. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTER_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTER_HPP_INCLUDED_ + +#include <boost/ref.hpp> +#include <boost/move/core.hpp> +#include <boost/move/utility.hpp> +#include <boost/utility/enable_if.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/light_function.hpp> +#include <boost/log/attributes/attribute_value_set.hpp> +#include <boost/log/attributes/value_visitation.hpp> +#include <boost/log/core/record_view.hpp> +#include <boost/log/utility/formatting_ostream.hpp> +#include <boost/log/utility/functional/bind_output.hpp> +#include <boost/log/expressions/message.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +// This reference class is a workaround for a Boost.Phoenix bug: https://svn.boost.org/trac/boost/ticket/9363 +// It is needed to pass output streams by non-const reference to function objects wrapped in phoenix::bind and phoenix::function. +// It's an implementation detail and will be removed when Boost.Phoenix is fixed. +template< typename StreamT > +class stream_ref : + public reference_wrapper< StreamT > +{ +public: + BOOST_FORCEINLINE explicit stream_ref(StreamT& strm) : reference_wrapper< StreamT >(strm) + { + } + + template< typename T > + BOOST_FORCEINLINE StreamT& operator<< (T& val) const + { + StreamT& strm = this->get(); + strm << val; + return strm; + } + + template< typename T > + BOOST_FORCEINLINE StreamT& operator<< (T const& val) const + { + StreamT& strm = this->get(); + strm << val; + return strm; + } +}; + +//! Default log record message formatter +struct message_formatter +{ + typedef void result_type; + + message_formatter() : m_MessageName(expressions::tag::message::get_name()) + { + } + + template< typename StreamT > + result_type operator() (record_view const& rec, StreamT& strm) const + { + boost::log::visit< expressions::tag::message::value_type >(m_MessageName, rec, boost::log::bind_output(strm)); + } + +private: + const attribute_name m_MessageName; +}; + +} // namespace aux + +} // namespace expressions + +/*! + * Log record formatter function wrapper. + */ +template< typename CharT > +class basic_formatter +{ + typedef basic_formatter this_type; + BOOST_COPYABLE_AND_MOVABLE(this_type) + +public: + //! Result type + typedef void result_type; + + //! Character type + typedef CharT char_type; + //! Output stream type + typedef basic_formatting_ostream< char_type > stream_type; + +private: + //! Filter function type + typedef boost::log::aux::light_function< void (record_view const&, expressions::aux::stream_ref< stream_type >) > formatter_type; + +private: + //! Formatter function + formatter_type m_Formatter; + +public: + /*! + * Default constructor. Creates a formatter that only outputs log message. + */ + basic_formatter() : m_Formatter(expressions::aux::message_formatter()) + { + } + /*! + * Copy constructor + */ + basic_formatter(basic_formatter const& that) : m_Formatter(that.m_Formatter) + { + } + /*! + * Move constructor. The moved-from formatter is left in an unspecified state. + */ + basic_formatter(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT : m_Formatter(boost::move(that.m_Formatter)) + { + } + + /*! + * Initializing constructor. Creates a formatter which will invoke the specified function object. + */ +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + template< typename FunT > + basic_formatter(FunT&& fun) : m_Formatter(boost::forward< FunT >(fun)) + { + } +#elif !defined(BOOST_MSVC) || BOOST_MSVC > 1400 + template< typename FunT > + basic_formatter(FunT const& fun, typename disable_if_c< move_detail::is_rv< FunT >::value, int >::type = 0) : m_Formatter(fun) + { + } +#else + // MSVC 8 blows up in unexpected ways if we use SFINAE to disable constructor instantiation + template< typename FunT > + basic_formatter(FunT const& fun) : m_Formatter(fun) + { + } + template< typename FunT > + basic_formatter(rv< FunT >& fun) : m_Formatter(fun) + { + } + template< typename FunT > + basic_formatter(rv< FunT > const& fun) : m_Formatter(static_cast< FunT const& >(fun)) + { + } + basic_formatter(rv< this_type > const& that) : m_Formatter(that.m_Formatter) + { + } +#endif + + /*! + * Move assignment. The moved-from formatter is left in an unspecified state. + */ + basic_formatter& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT + { + m_Formatter.swap(that.m_Formatter); + return *this; + } + /*! + * Copy assignment. + */ + basic_formatter& operator= (BOOST_COPY_ASSIGN_REF(this_type) that) + { + m_Formatter = that.m_Formatter; + return *this; + } + /*! + * Initializing assignment. Sets the specified function object to the formatter. + */ +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + template< typename FunT > + basic_formatter& operator= (FunT&& fun) + { + this_type(boost::forward< FunT >(fun)).swap(*this); + return *this; + } +#else + template< typename FunT > + typename disable_if< is_same< typename remove_cv< FunT >::type, this_type >, this_type& >::type + operator= (FunT const& fun) + { + this_type(fun).swap(*this); + return *this; + } +#endif + + /*! + * Formatting operator. + * + * \param rec A log record to format. + * \param strm A stream to put the formatted characters to. + */ + result_type operator() (record_view const& rec, stream_type& strm) const + { + m_Formatter(rec, expressions::aux::stream_ref< stream_type >(strm)); + } + + /*! + * Resets the formatter to the default. The default formatter only outputs message text. + */ + void reset() + { + m_Formatter = expressions::aux::message_formatter(); + } + + /*! + * Swaps two formatters + */ + void swap(basic_formatter& that) BOOST_NOEXCEPT + { + m_Formatter.swap(that.m_Formatter); + } +}; + +template< typename CharT > +inline void swap(basic_formatter< CharT >& left, basic_formatter< CharT >& right) BOOST_NOEXCEPT +{ + left.swap(right); +} + +#ifdef BOOST_LOG_USE_CHAR +typedef basic_formatter< char > formatter; +#endif +#ifdef BOOST_LOG_USE_WCHAR_T +typedef basic_formatter< wchar_t > wformatter; +#endif + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTER_HPP_INCLUDED_ diff --git a/boost/log/expressions/formatters.hpp b/boost/log/expressions/formatters.hpp new file mode 100644 index 0000000000..47f84a516f --- /dev/null +++ b/boost/log/expressions/formatters.hpp @@ -0,0 +1,38 @@ +/* + * 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.hpp + * \author Andrey Semashev + * \date 10.11.2012 + * + * The header includes all template expression formatters. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_FORMATTERS_HPP_INCLUDED_ + +#include <boost/log/detail/config.hpp> + +#include <boost/log/expressions/formatters/stream.hpp> +#include <boost/log/expressions/formatters/format.hpp> + +#include <boost/log/expressions/formatters/date_time.hpp> +#include <boost/log/expressions/formatters/named_scope.hpp> + +#include <boost/log/expressions/formatters/char_decorator.hpp> +#include <boost/log/expressions/formatters/xml_decorator.hpp> +#include <boost/log/expressions/formatters/csv_decorator.hpp> +#include <boost/log/expressions/formatters/c_decorator.hpp> + +#include <boost/log/expressions/formatters/if.hpp> +#include <boost/log/expressions/formatters/wrap_formatter.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_HPP_INCLUDED_ 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 <limits> +#include <boost/range/iterator_range_core.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/snprintf.hpp> +#include <boost/log/expressions/formatters/char_decorator.hpp> +#include <boost/log/detail/header.hpp> + +#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 <tt>operator[]</tt> that + * can be used to construct the actual decorator. For example: + * + * <code> + * c_decor[ attr< std::string >("MyAttr") ] + * </code> + * + * 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 <tt>operator[]</tt> that + * can be used to construct the actual decorator. For example: + * + * <code> + * c_ascii_decor[ attr< std::string >("MyAttr") ] + * </code> + * + * 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 <boost/log/detail/footer.hpp> + +#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 <vector> +#include <string> +#include <iterator> +#include <boost/assert.hpp> +#include <boost/static_assert.hpp> +#include <boost/mpl/bool.hpp> +#include <boost/range/begin.hpp> +#include <boost/range/end.hpp> +#include <boost/range/size.hpp> +#include <boost/range/const_iterator.hpp> +#include <boost/range/value_type.hpp> +#include <boost/move/core.hpp> +#include <boost/move/utility.hpp> +#include <boost/utility/addressof.hpp> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/meta_grammar.hpp> +#include <boost/phoenix/core/terminal_fwd.hpp> +#include <boost/phoenix/core/is_nullary.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/phoenix/support/vector.hpp> +#include <boost/fusion/sequence/intrinsic/at_c.hpp> +#include <boost/type_traits/is_same.hpp> +#include <boost/type_traits/remove_cv.hpp> +#include <boost/type_traits/remove_reference.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/detail/deduce_char_type.hpp> +#include <boost/log/utility/formatting_ostream.hpp> +#include <boost/log/detail/header.hpp> + +#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 <tt>i</tt>'th decoration will be + * <tt>from[i]</tt> -> <tt>to[i]</tt>. + */ + 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 <boost/log/detail/generate_overloads.hpp> + +#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 <tt>operator[]</tt> that can be used + * to construct the actual decorator. + * + * \param decorations A sequence of string pairs that will be used as decorations. Every <tt>decorations[i].first</tt> + * substring occurrence in the output will be replaced with <tt>decorations[i].second</tt>. + */ +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 <tt>operator[]</tt> 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 <tt>from[i]</tt> + * substring occurrence in the output will be replaced with <tt>to[i]</tt>. + */ +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 <boost/log/detail/footer.hpp> + +#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 <boost/range/iterator_range_core.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/expressions/formatters/char_decorator.hpp> +#include <boost/log/detail/header.hpp> + +#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 <tt>operator[]</tt> that can be used to construct + * the actual decorator. For example: + * + * <code> + * csv_decor[ attr< std::string >("MyAttr") ] + * </code> + * + * 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 <boost/log/detail/footer.hpp> + +#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 <string> +#include <boost/move/core.hpp> +#include <boost/move/utility.hpp> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/terminal_fwd.hpp> +#include <boost/phoenix/core/is_nullary.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/fusion/sequence/intrinsic/at_c.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/attributes/value_visitation.hpp> +#include <boost/log/detail/light_function.hpp> +#include <boost/log/detail/date_time_fmt_gen_traits_fwd.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/detail/attr_output_terminal.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/utility/formatting_ostream.hpp> +#include <boost/log/utility/functional/bind.hpp> +#include <boost/log/detail/header.hpp> + +#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 <boost/log/detail/generate_overloads.hpp> + +#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 <boost/log/detail/footer.hpp> + +#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 <string> +#include <boost/mpl/bool.hpp> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/terminal_fwd.hpp> +#include <boost/phoenix/core/is_nullary.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/fusion/sequence/intrinsic/at_c.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/format.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/detail/header.hpp> + +#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 <boost/log/detail/footer.hpp> + +#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 <boost/mpl/bool.hpp> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/meta_grammar.hpp> +#include <boost/phoenix/core/terminal_fwd.hpp> +#include <boost/phoenix/core/is_nullary.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/fusion/sequence/intrinsic/at_c.hpp> +#include <boost/type_traits/remove_cv.hpp> +#include <boost/type_traits/remove_reference.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/detail/header.hpp> + +#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 <boost/log/detail/generate_overloads.hpp> + +#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 <boost/log/detail/generate_overloads.hpp> + +#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 <tt>operator[]</tt> 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 <boost/log/detail/footer.hpp> + +#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 <string> +#include <iterator> +#include <utility> +#include <boost/static_assert.hpp> +#include <boost/type_traits/is_same.hpp> +#include <boost/move/core.hpp> +#include <boost/move/utility.hpp> +#include <boost/parameter/binding.hpp> +#include <boost/preprocessor/iteration/iterate.hpp> +#include <boost/preprocessor/repetition/enum_params.hpp> +#include <boost/preprocessor/repetition/enum_binary_params.hpp> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/terminal_fwd.hpp> +#include <boost/phoenix/core/is_nullary.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/fusion/sequence/intrinsic/at_c.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/attributes/named_scope.hpp> +#include <boost/log/attributes/value_visitation.hpp> +#include <boost/log/detail/light_function.hpp> +#include <boost/log/detail/parameter_tools.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/detail/deduce_char_type.hpp> +#include <boost/log/detail/attr_output_terminal.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/utility/formatting_ostream.hpp> +#include <boost/log/utility/string_literal_fwd.hpp> +#include <boost/log/utility/functional/bind.hpp> +#include <boost/log/keywords/format.hpp> +#include <boost/log/keywords/delimiter.hpp> +#include <boost/log/keywords/depth.hpp> +#include <boost/log/keywords/iteration.hpp> +#include <boost/log/keywords/empty_marker.hpp> +#include <boost/log/keywords/incomplete_marker.hpp> +#include <boost/log/detail/header.hpp> + +#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 <boost/log/detail/generate_overloads.hpp> + +#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 <boost/log/detail/named_scope_fmt_pp.hpp> +# 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 <boost/log/detail/footer.hpp> + +#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 <boost/phoenix/core/argument.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/header.hpp> + +#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 <boost/log/detail/footer.hpp> +#if defined(BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_) +#include <boost/log/detail/attr_output_impl.hpp> +#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 <string> +#include <boost/move/core.hpp> +#include <boost/move/utility.hpp> +#include <boost/mpl/has_xxx.hpp> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/terminal_fwd.hpp> +#include <boost/phoenix/core/is_nullary.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/type_traits/remove_cv.hpp> +#include <boost/type_traits/remove_reference.hpp> +#include <boost/fusion/sequence/intrinsic/at_c.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/detail/function_traits.hpp> +#include <boost/log/utility/formatting_ostream.hpp> +#include <boost/log/detail/header.hpp> + +#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 <boost/log/detail/generate_overloads.hpp> + +#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: + * + * <pre> + * void (record_view const&, basic_formatting_ostream< CharT >&) + * </pre> + * + * 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: + * + * <pre> + * void (record_view const&, basic_formatting_ostream< CharT >&) + * </pre> + * + * 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 <boost/log/detail/footer.hpp> + +#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 <boost/range/iterator_range_core.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/expressions/formatters/char_decorator.hpp> +#include <boost/log/detail/header.hpp> + +#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 + * <tt>operator[]</tt> that can be used to construct the actual decorator. For example: + * + * <code> + * xml_decor[ attr< std::string >("MyAttr") ] + * </code> + * + * 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 <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_ diff --git a/boost/log/expressions/is_keyword_descriptor.hpp b/boost/log/expressions/is_keyword_descriptor.hpp new file mode 100644 index 0000000000..9899e228e7 --- /dev/null +++ b/boost/log/expressions/is_keyword_descriptor.hpp @@ -0,0 +1,67 @@ +/* + * 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 is_keyword_descriptor.hpp + * \author Andrey Semashev + * \date 14.07.2012 + * + * The header contains attribute keyword descriptor detection trait. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_IS_KEYWORD_DESCRIPTOR_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_IS_KEYWORD_DESCRIPTOR_HPP_INCLUDED_ + +#include <boost/mpl/bool.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * Base class for keyword descriptors. All keyword descriptors must derive from this class to support the \c is_keyword_descriptor trait. + */ +struct keyword_descriptor +{ +#ifndef BOOST_LOG_DOXYGEN_PASS + typedef void _is_boost_log_keyword_descriptor; +#endif // BOOST_LOG_DOXYGEN_PASS +}; + +/*! + * The metafunction detects if the type \c T is a keyword descriptor + */ +template< typename T, typename VoidT = void > +struct is_keyword_descriptor : + public mpl::false_ +{ +}; + +#ifndef BOOST_LOG_DOXYGEN_PASS +template< typename T > +struct is_keyword_descriptor< T, typename T::_is_boost_log_keyword_descriptor > : + public mpl::true_ +{ +}; +#endif + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_IS_KEYWORD_DESCRIPTOR_HPP_INCLUDED_ diff --git a/boost/log/expressions/keyword.hpp b/boost/log/expressions/keyword.hpp new file mode 100644 index 0000000000..434247be91 --- /dev/null +++ b/boost/log/expressions/keyword.hpp @@ -0,0 +1,258 @@ +/* + * 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 keyword.hpp + * \author Andrey Semashev + * \date 29.01.2012 + * + * The header contains attribute keyword declaration. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_KEYWORD_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_KEYWORD_HPP_INCLUDED_ + +#include <boost/ref.hpp> +#include <boost/proto/extends.hpp> +#include <boost/proto/make_expr.hpp> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/domain.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/fusion/sequence/intrinsic/at.hpp> +#include <boost/preprocessor/cat.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/expressions/is_keyword_descriptor.hpp> +#include <boost/log/expressions/attr.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/value_extraction.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * This class implements an expression template keyword. It is used to start template expressions involving attribute values. + */ +template< typename DescriptorT, template< typename > class ActorT > +struct attribute_keyword +{ + //! Self type + typedef attribute_keyword this_type; + //! Attribute descriptor type + typedef DescriptorT descriptor_type; + + BOOST_PROTO_BASIC_EXTENDS(typename proto::terminal< descriptor_type >::type, this_type, phoenix::phoenix_domain) + + //! Attribute value type + typedef typename descriptor_type::value_type value_type; + + //! Returns attribute name + static attribute_name get_name() { return descriptor_type::get_name(); } + + //! Expression with cached attribute name + typedef attribute_actor< + value_type, + fallback_to_none, + descriptor_type, + ActorT + > or_none_result_type; + + //! Generates an expression that extracts the attribute value or a default value + static or_none_result_type or_none() + { + typedef typename or_none_result_type::terminal_type result_terminal; + typename or_none_result_type::base_type act = {{ result_terminal(get_name()) }}; + return or_none_result_type(act); + } + + //! Expression with cached attribute name + typedef attribute_actor< + value_type, + fallback_to_throw, + descriptor_type, + ActorT + > or_throw_result_type; + + //! Generates an expression that extracts the attribute value or throws an exception + static or_throw_result_type or_throw() + { + typedef typename or_throw_result_type::terminal_type result_terminal; + typename or_throw_result_type::base_type act = {{ result_terminal(get_name()) }}; + return or_throw_result_type(act); + } + + //! Generates an expression that extracts the attribute value or a default value + template< typename DefaultT > + static attribute_actor< + value_type, + fallback_to_default< DefaultT >, + descriptor_type, + ActorT + > or_default(DefaultT const& def_val) + { + typedef attribute_actor< + value_type, + fallback_to_default< DefaultT >, + descriptor_type, + ActorT + > or_default_result_type; + typedef typename or_default_result_type::terminal_type result_terminal; + typename or_default_result_type::base_type act = {{ result_terminal(get_name(), def_val) }}; + return or_default_result_type(act); + } +}; + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace proto { + +namespace detail { + +// This hack is needed in order to cache attribute name into the expression terminal when the template +// expression is constructed. The standard way through a custom domain doesn't work because phoenix::actor +// is bound to phoenix_domain. +template< typename DescriptorT, template< typename > class ActorT, typename DomainT > +struct protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT > +{ + typedef boost::log::expressions::attribute_keyword< DescriptorT, ActorT > keyword_type; + typedef typename keyword_type::or_none_result_type result_type; + + result_type operator() (keyword_type const& keyword) const + { + return keyword.or_none(); + } +}; + +template< typename DescriptorT, template< typename > class ActorT, typename DomainT > +struct protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >&, DomainT > : + public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT > +{ +}; + +template< typename DescriptorT, template< typename > class ActorT, typename DomainT > +struct protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT > const&, DomainT > : + public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT > +{ +}; + +template< typename DescriptorT, template< typename > class ActorT, typename DomainT > +struct protoify< boost::reference_wrapper< boost::log::expressions::attribute_keyword< DescriptorT, ActorT > >, DomainT > : + public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT > +{ +}; + +template< typename DescriptorT, template< typename > class ActorT, typename DomainT > +struct protoify< boost::reference_wrapper< boost::log::expressions::attribute_keyword< DescriptorT, ActorT > > const, DomainT > : + public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT > +{ +}; + +template< typename DescriptorT, template< typename > class ActorT, typename DomainT > +struct protoify< boost::reference_wrapper< const boost::log::expressions::attribute_keyword< DescriptorT, ActorT > >, DomainT > : + public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT > +{ +}; + +template< typename DescriptorT, template< typename > class ActorT, typename DomainT > +struct protoify< boost::reference_wrapper< const boost::log::expressions::attribute_keyword< DescriptorT, ActorT > > const, DomainT > : + public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT > +{ +}; + +} // namespace detail + +} // namespace proto + +#endif // !defined(BOOST_LOG_DOXYGEN_PASS) + +} // namespace boost + +#ifndef BOOST_LOG_DOXYGEN_PASS + +#define BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE_IMPL(keyword_, name_, value_type_, tag_ns_)\ + namespace tag_ns_\ + {\ + struct keyword_ :\ + public ::boost::log::expressions::keyword_descriptor\ + {\ + typedef value_type_ value_type;\ + static ::boost::log::attribute_name get_name() { return ::boost::log::attribute_name(name_); }\ + };\ + }\ + typedef ::boost::log::expressions::attribute_keyword< tag_ns_::keyword_ > BOOST_PP_CAT(keyword_, _type); + +#define BOOST_LOG_ATTRIBUTE_KEYWORD_IMPL(keyword_, name_, value_type_, tag_ns_)\ + BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE_IMPL(keyword_, name_, value_type_, tag_ns_)\ + const BOOST_PP_CAT(keyword_, _type) keyword_ = {}; + +#endif // BOOST_LOG_DOXYGEN_PASS + +/*! + * \brief The macro declares an attribute keyword type + * + * The macro should be used at a namespace scope. It expands into an attribute keyword type definition, including the + * \c tag namespace and the keyword tag type within which has the following layout: + * + * \code + * namespace tag + * { + * struct keyword_ : + * public boost::log::expressions::keyword_descriptor + * { + * typedef value_type_ value_type; + * static boost::log::attribute_name get_name(); + * }; + * } + * + * typedef boost::log::expressions::attribute_keyword< tag::keyword_ > keyword_type; + * \endcode + * + * The \c get_name method returns the attribute name. + * + * \note This macro only defines the type of the keyword. To also define the keyword object, use + * the \c BOOST_LOG_ATTRIBUTE_KEYWORD macro instead. + * + * \param keyword_ Keyword name + * \param name_ Attribute name string + * \param value_type_ Attribute value type + */ +#define BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(keyword_, name_, value_type_)\ + BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE_IMPL(keyword_, name_, value_type_, tag) + +/*! + * \brief The macro declares an attribute keyword + * + * The macro provides definitions similar to \c BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE and additionally + * defines the keyword object. + * + * \param keyword_ Keyword name + * \param name_ Attribute name string + * \param value_type_ Attribute value type + */ +#define BOOST_LOG_ATTRIBUTE_KEYWORD(keyword_, name_, value_type_)\ + BOOST_LOG_ATTRIBUTE_KEYWORD_IMPL(keyword_, name_, value_type_, tag) + +#include <boost/log/detail/footer.hpp> + +#if defined(BOOST_LOG_TRIVIAL_HPP_INCLUDED_) +#include <boost/log/detail/trivial_keyword.hpp> +#endif + +#endif // BOOST_LOG_EXPRESSIONS_KEYWORD_HPP_INCLUDED_ diff --git a/boost/log/expressions/keyword_fwd.hpp b/boost/log/expressions/keyword_fwd.hpp new file mode 100644 index 0000000000..a2671e9f61 --- /dev/null +++ b/boost/log/expressions/keyword_fwd.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 keyword_fwd.hpp + * \author Andrey Semashev + * \date 29.01.2012 + * + * The header contains attribute keyword forward declaration. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_KEYWORD_FWD_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_KEYWORD_FWD_HPP_INCLUDED_ + +#include <boost/log/detail/config.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +template< typename > +struct actor; + +} // namespace phoenix + +#endif // BOOST_LOG_DOXYGEN_PASS + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * \brief This class implements an expression template keyword + */ +template< typename DescriptorT, template< typename > class ActorT = phoenix::actor > +struct attribute_keyword; + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#endif // BOOST_LOG_EXPRESSIONS_KEYWORD_FWD_HPP_INCLUDED_ diff --git a/boost/log/expressions/message.hpp b/boost/log/expressions/message.hpp new file mode 100644 index 0000000000..79d826a992 --- /dev/null +++ b/boost/log/expressions/message.hpp @@ -0,0 +1,130 @@ +/* + * 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 message.hpp + * \author Andrey Semashev + * \date 13.07.2012 + * + * The header contains log message keyword declaration. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_MESSAGE_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_MESSAGE_HPP_INCLUDED_ + +#include <string> +#include <boost/mpl/vector.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/default_attribute_names.hpp> +#include <boost/log/expressions/keyword.hpp> +#include <boost/log/expressions/is_keyword_descriptor.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace tag { + +/*! + * Generic log message attribute descriptor. + */ +struct message : + public keyword_descriptor +{ + // The attribute value type here is not essential since message attributes are not intended to be created via the keyword + typedef void attribute_type; + +#if defined(BOOST_LOG_USE_CHAR) && defined(BOOST_LOG_USE_WCHAR_T) + typedef mpl::vector2< std::string, std::wstring > value_type; +#elif defined(BOOST_LOG_USE_CHAR) + typedef std::string value_type; +#elif defined(BOOST_LOG_USE_WCHAR_T) + typedef std::wstring value_type; +#endif + + static attribute_name get_name() { return boost::log::aux::default_attribute_names::message(); } +}; + +#if defined(BOOST_LOG_USE_CHAR) +/*! + * Narrow character log message attribute descriptor. + */ +struct smessage : + public keyword_descriptor +{ + // The attribute value type here is not essential since message attributes are not intended to be created via the keyword + typedef void attribute_type; + typedef std::string value_type; + + static attribute_name get_name() { return boost::log::aux::default_attribute_names::message(); } +}; +#endif + +#if defined(BOOST_LOG_USE_WCHAR_T) +/*! + * Wide character log message attribute descriptor. + */ +struct wmessage : + public keyword_descriptor +{ + // The attribute value type here is not essential since message attributes are not intended to be created via the keyword + typedef void attribute_type; + typedef std::wstring value_type; + + static attribute_name get_name() { return boost::log::aux::default_attribute_names::message(); } +}; +#endif + +} // namespace tag + +/*! + * Generic message keyword type. + */ +typedef attribute_keyword< tag::message > message_type; +/*! + * Generic message keyword. + */ +const message_type message = {}; + +#if defined(BOOST_LOG_USE_CHAR) +/*! + * Narrow message keyword type. + */ +typedef attribute_keyword< tag::smessage > smessage_type; +/*! + * Narrow message keyword. + */ +const smessage_type smessage = {}; +#endif + +#if defined(BOOST_LOG_USE_WCHAR_T) +/*! + * Wide message keyword type. + */ +typedef attribute_keyword< tag::wmessage > wmessage_type; +/*! + * Wide message keyword. + */ +const wmessage_type wmessage = {}; +#endif + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_MESSAGE_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates.hpp b/boost/log/expressions/predicates.hpp new file mode 100644 index 0000000000..297b51b9d2 --- /dev/null +++ b/boost/log/expressions/predicates.hpp @@ -0,0 +1,34 @@ +/* + * 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 predicates.hpp + * \author Andrey Semashev + * \date 29.01.2012 + * + * The header includes all template expression predicates. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_HPP_INCLUDED_ + +#include <boost/log/detail/config.hpp> + +#include <boost/log/expressions/predicates/has_attr.hpp> +#include <boost/log/expressions/predicates/begins_with.hpp> +#include <boost/log/expressions/predicates/ends_with.hpp> +#include <boost/log/expressions/predicates/contains.hpp> +#include <boost/log/expressions/predicates/matches.hpp> +#include <boost/log/expressions/predicates/is_in_range.hpp> + +#include <boost/log/expressions/predicates/is_debugger_present.hpp> +#include <boost/log/expressions/predicates/channel_severity_filter.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates/begins_with.hpp b/boost/log/expressions/predicates/begins_with.hpp new file mode 100644 index 0000000000..e837a4ac7e --- /dev/null +++ b/boost/log/expressions/predicates/begins_with.hpp @@ -0,0 +1,129 @@ +/* + * 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 begins_with.hpp + * \author Andrey Semashev + * \date 02.09.2012 + * + * The header contains implementation of a \c begins_with predicate in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_BEGINS_WITH_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_BEGINS_WITH_HPP_INCLUDED_ + +#include <boost/phoenix/core/actor.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/embedded_string_type.hpp> +#include <boost/log/detail/unary_function_terminal.hpp> +#include <boost/log/detail/attribute_predicate.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/utility/functional/begins_with.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * The predicate checks if the attribute value begins with a substring. The attribute value is assumed to be of a string type. + */ +#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +template< typename T, typename SubstringT, typename FallbackPolicyT = fallback_to_none > +using attribute_begins_with = aux::attribute_predicate< T, SubstringT, begins_with_fun, FallbackPolicyT >; + +#else // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +template< typename T, typename SubstringT, typename FallbackPolicyT = fallback_to_none > +class attribute_begins_with : + public aux::attribute_predicate< T, SubstringT, begins_with_fun, FallbackPolicyT > +{ + typedef aux::attribute_predicate< T, SubstringT, begins_with_fun, FallbackPolicyT > base_type; + +public: + /*! + * Initializing constructor + * + * \param name Attribute name + * \param substring The expected attribute value beginning + */ + attribute_begins_with(attribute_name const& name, SubstringT const& substring) : base_type(name, substring) + { + } + + /*! + * Initializing constructor + * + * \param name Attribute name + * \param substring The expected attribute value beginning + * \param arg Additional parameter for the fallback policy + */ + template< typename U > + attribute_begins_with(attribute_name const& name, SubstringT const& substring, U const& arg) : base_type(name, substring, arg) + { + } +}; + +#endif // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, begins with the specified substring. + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename SubstringT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_begins_with< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type, FallbackPolicyT > > > +begins_with(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& attr, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_begins_with< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type, FallbackPolicyT > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(attr.get_name(), substring, attr.get_fallback_policy()) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, begins with the specified substring. + */ +template< typename DescriptorT, template< typename > class ActorT, typename SubstringT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_begins_with< typename DescriptorT::value_type, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > > +begins_with(attribute_keyword< DescriptorT, ActorT > const&, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_begins_with< typename DescriptorT::value_type, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name(), substring) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, begins with the specified substring. + */ +template< typename T, typename SubstringT > +BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< attribute_begins_with< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > > +begins_with(attribute_name const& name, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_begins_with< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(name, substring) }}; + return act; +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_BEGINS_WITH_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates/channel_severity_filter.hpp b/boost/log/expressions/predicates/channel_severity_filter.hpp new file mode 100644 index 0000000000..f0ddfb564c --- /dev/null +++ b/boost/log/expressions/predicates/channel_severity_filter.hpp @@ -0,0 +1,572 @@ +/* + * 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 channel_severity_filter.hpp + * \author Andrey Semashev + * \date 25.11.2012 + * + * The header contains implementation of a minimal severity per channel filter. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_CHANNEL_SEVERITY_FILTER_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_CHANNEL_SEVERITY_FILTER_HPP_INCLUDED_ + +#include <map> +#include <memory> +#include <utility> +#include <boost/phoenix/core/actor.hpp> +#include <boost/phoenix/core/terminal_fwd.hpp> +#include <boost/phoenix/core/is_nullary.hpp> +#include <boost/phoenix/core/environment.hpp> +#include <boost/fusion/sequence/intrinsic/at_c.hpp> +#include <boost/type_traits/remove_cv.hpp> +#include <boost/type_traits/remove_reference.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/custom_terminal_spec.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/attributes/value_visitation.hpp> +#include <boost/log/utility/functional/logical.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +template< + typename ChannelT, + typename SeverityT, + typename ChannelFallbackT = fallback_to_none, + typename SeverityFallbackT = fallback_to_none, + typename ChannelOrderT = less, + typename SeverityCompareT = greater_equal, + typename AllocatorT = std::allocator< void > +> +class channel_severity_filter_terminal +{ +public: + //! Internal typedef for type categorization + typedef void _is_boost_log_terminal; + + //! Function result type + typedef bool result_type; + + //! Channel attribute value type + typedef ChannelT channel_value_type; + //! Channel fallback policy + typedef ChannelFallbackT channel_fallback_policy; + //! Severity level attribute value type + typedef SeverityT severity_value_type; + //! Severity level fallback policy + typedef SeverityFallbackT severity_fallback_policy; + +private: + //! Channel to severity mapping type + typedef std::map< + channel_value_type, + severity_value_type, + ChannelOrderT, + typename AllocatorT::BOOST_NESTED_TEMPLATE rebind< std::pair< const channel_value_type, severity_value_type > >::other + > mapping_type; + //! Attribute value visitor invoker for channel + typedef value_visitor_invoker< channel_value_type, channel_fallback_policy > channel_visitor_invoker_type; + //! Attribute value visitor invoker for severity level + typedef value_visitor_invoker< severity_value_type, severity_fallback_policy > severity_visitor_invoker_type; + + //! Channel visitor + template< typename ArgT > + struct channel_visitor + { + typedef void result_type; + + channel_visitor(channel_severity_filter_terminal const& self, ArgT arg, bool& res) : m_self(self), m_arg(arg), m_res(res) + { + } + + result_type operator() (channel_value_type const& channel) const + { + m_self.visit_channel(channel, m_arg, m_res); + } + + private: + channel_severity_filter_terminal const& m_self; + ArgT m_arg; + bool& m_res; + }; + + //! Severity level visitor + struct severity_visitor + { + typedef void result_type; + + severity_visitor(channel_severity_filter_terminal const& self, severity_value_type const& severity, bool& res) : m_self(self), m_severity(severity), m_res(res) + { + } + + result_type operator() (severity_value_type const& severity) const + { + m_self.visit_severity(severity, m_severity, m_res); + } + + private: + channel_severity_filter_terminal const& m_self; + severity_value_type const& m_severity; + bool& m_res; + }; + +private: + //! Channel attribute name + attribute_name m_channel_name; + //! Severity level attribute name + attribute_name m_severity_name; + //! Channel value visitor invoker + channel_visitor_invoker_type m_channel_visitor_invoker; + //! Severity level value visitor invoker + severity_visitor_invoker_type m_severity_visitor_invoker; + + //! Channel to severity level mapping + mapping_type m_mapping; + //! Severity checking predicate + SeverityCompareT m_severity_compare; + + //! Default result + bool m_default; + +public: + //! Initializing constructor + channel_severity_filter_terminal + ( + attribute_name const& channel_name, + attribute_name const& severity_name, + channel_fallback_policy const& channel_fallback = channel_fallback_policy(), + severity_fallback_policy const& severity_fallback = severity_fallback_policy(), + ChannelOrderT const& channel_order = ChannelOrderT(), + SeverityCompareT const& severity_compare = SeverityCompareT() + ) : + m_channel_name(channel_name), + m_severity_name(severity_name), + m_channel_visitor_invoker(channel_fallback), + m_severity_visitor_invoker(severity_fallback), + m_mapping(channel_order), + m_severity_compare(severity_compare), + m_default(false) + { + } + + //! Adds a new element to the mapping + void add(channel_value_type const& channel, severity_value_type const& severity) + { + typedef typename mapping_type::iterator iterator; + std::pair< iterator, bool > res = m_mapping.insert(typename mapping_type::value_type(channel, severity)); + if (!res.second) + res.first->second = severity; + } + + //! Sets the default result of the predicate + void set_default(bool def) + { + m_default = def; + } + + //! Invokation operator + template< typename ContextT > + result_type operator() (ContextT const& ctx) const + { + result_type res = m_default; + + typedef typename remove_cv< + typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type + >::type env_type; + typedef typename env_type::args_type args_type; + typedef typename fusion::result_of::at_c< args_type, 0 >::type arg_type; + arg_type arg = fusion::at_c< 0 >(phoenix::env(ctx).args()); + + m_channel_visitor_invoker(m_channel_name, arg, channel_visitor< arg_type >(*this, arg, res)); + + return res; + } + +private: + //! Visits channel name + template< typename ArgT > + void visit_channel(channel_value_type const& channel, ArgT const& arg, bool& res) const + { + typename mapping_type::const_iterator it = m_mapping.find(channel); + if (it != m_mapping.end()) + { + m_severity_visitor_invoker(m_severity_name, arg, severity_visitor(*this, it->second, res)); + } + } + + //! Visits severity level + void visit_severity(severity_value_type const& left, severity_value_type const& right, bool& res) const + { + res = m_severity_compare(left, right); + } +}; + +template< + typename ChannelT, + typename SeverityT, + typename ChannelFallbackT = fallback_to_none, + typename SeverityFallbackT = fallback_to_none, + typename ChannelOrderT = less, + typename SeverityCompareT = greater_equal, + typename AllocatorT = std::allocator< void >, + template< typename > class ActorT = phoenix::actor +> +class channel_severity_filter_actor : + public ActorT< channel_severity_filter_terminal< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, ChannelOrderT, SeverityCompareT, AllocatorT > > +{ +private: + //! Self type + typedef channel_severity_filter_actor this_type; + +public: + //! Terminal type + typedef channel_severity_filter_terminal< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, ChannelOrderT, SeverityCompareT, AllocatorT > terminal_type; + //! Base actor type + typedef ActorT< terminal_type > base_type; + + //! Channel attribute value type + typedef typename terminal_type::channel_value_type channel_value_type; + //! Channel fallback policy + typedef typename terminal_type::channel_fallback_policy channel_fallback_policy; + //! Severity level attribute value type + typedef typename terminal_type::severity_value_type severity_value_type; + //! Severity level fallback policy + typedef typename terminal_type::severity_fallback_policy severity_fallback_policy; + +private: + //! An auxiliary pseudo-reference to implement insertion through subscript operator + class subscript_result + { + private: + channel_severity_filter_actor& m_owner; + channel_value_type const& m_channel; + + public: + subscript_result(channel_severity_filter_actor& owner, channel_value_type const& channel) : m_owner(owner), m_channel(channel) + { + } + + void operator= (severity_value_type const& severity) + { + m_owner.add(m_channel, severity); + } + }; + +public: + //! Initializing constructor + explicit channel_severity_filter_actor(base_type const& act) : base_type(act) + { + } + //! Copy constructor + channel_severity_filter_actor(channel_severity_filter_actor const& that) : base_type(static_cast< base_type const& >(that)) + { + } + + //! Sets the default function result + this_type& set_default(bool def) + { + this->proto_expr_.child0.set_default(def); + return *this; + } + + //! Adds a new element to the mapping + this_type& add(channel_value_type const& channel, severity_value_type const& severity) + { + this->proto_expr_.child0.add(channel, severity); + return *this; + } + + //! Alternative interface for adding a new element to the mapping + subscript_result operator[] (channel_value_type const& channel) + { + return subscript_result(*this, channel); + } +}; + +/*! + * The function generates a filtering predicate that checks the severity levels of log records in different channels. The predicate will return \c true + * if the record severity level is not less than the threshold for the channel the record belongs to. + */ +template< typename ChannelT, typename SeverityT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT > +channel_severity_filter(attribute_name const& channel_name, attribute_name const& severity_name) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_name) }}; + return result_type(act); +} + +//! \overload +template< typename SeverityT, typename ChannelDescriptorT, template< typename > class ActorT > +BOOST_FORCEINLINE channel_severity_filter_actor< typename ChannelDescriptorT::value_type, SeverityT, fallback_to_none, fallback_to_none, less, greater_equal, std::allocator< void >, ActorT > +channel_severity_filter(attribute_keyword< ChannelDescriptorT, ActorT > const& channel_keyword, attribute_name const& severity_name) +{ + typedef channel_severity_filter_actor< typename ChannelDescriptorT::value_type, SeverityT, fallback_to_none, fallback_to_none, less, greater_equal, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_keyword.get_name(), severity_name) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename SeverityDescriptorT, template< typename > class ActorT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, less, greater_equal, std::allocator< void >, ActorT > +channel_severity_filter(attribute_name const& channel_name, attribute_keyword< SeverityDescriptorT, ActorT > const& severity_keyword) +{ + typedef channel_severity_filter_actor< ChannelT, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, less, greater_equal, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_keyword.get_name()) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelDescriptorT, typename SeverityDescriptorT, template< typename > class ActorT > +BOOST_FORCEINLINE channel_severity_filter_actor< typename ChannelDescriptorT::value_type, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, less, greater_equal, std::allocator< void >, ActorT > +channel_severity_filter(attribute_keyword< ChannelDescriptorT, ActorT > const& channel_keyword, attribute_keyword< SeverityDescriptorT, ActorT > const& severity_keyword) +{ + typedef channel_severity_filter_actor< typename ChannelDescriptorT::value_type, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, less, greater_equal, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_keyword.get_name(), severity_keyword.get_name()) }}; + return result_type(act); +} + +//! \overload +template< typename SeverityT, typename ChannelT, typename ChannelFallbackT, typename ChannelTagT, template< typename > class ActorT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, fallback_to_none, less, greater_equal, std::allocator< void >, ActorT > +channel_severity_filter(attribute_actor< ChannelT, ChannelFallbackT, ChannelTagT, ActorT > const& channel_placeholder, attribute_name const& severity_name) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, fallback_to_none, less, greater_equal, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_placeholder.get_name(), severity_name, channel_placeholder.get_fallback_policy()) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename SeverityT, typename SeverityFallbackT, typename SeverityTagT, template< typename > class ActorT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, SeverityFallbackT, less, greater_equal, std::allocator< void >, ActorT > +channel_severity_filter(attribute_name const& channel_name, attribute_actor< SeverityT, SeverityFallbackT, SeverityTagT, ActorT > const& severity_placeholder) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, SeverityFallbackT, less, greater_equal, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_placeholder.get_name(), fallback_to_none(), severity_placeholder.get_fallback_policy()) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename ChannelFallbackT, typename ChannelTagT, typename SeverityT, typename SeverityFallbackT, typename SeverityTagT, template< typename > class ActorT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, less, greater_equal, std::allocator< void >, ActorT > +channel_severity_filter(attribute_actor< ChannelT, ChannelFallbackT, ChannelTagT, ActorT > const& channel_placeholder, attribute_actor< SeverityT, SeverityFallbackT, SeverityTagT, ActorT > const& severity_placeholder) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, less, greater_equal, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_placeholder.get_name(), severity_placeholder.get_name(), channel_placeholder.get_fallback_policy(), severity_placeholder.get_fallback_policy()) }}; + return result_type(act); +} + + +//! \overload +template< typename ChannelT, typename SeverityT, typename SeverityCompareT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, fallback_to_none, less, SeverityCompareT > +channel_severity_filter(attribute_name const& channel_name, attribute_name const& severity_name, SeverityCompareT const& severity_compare) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, fallback_to_none, less, SeverityCompareT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_name, fallback_to_none(), fallback_to_none(), less(), severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename SeverityT, typename ChannelDescriptorT, template< typename > class ActorT, typename SeverityCompareT > +BOOST_FORCEINLINE channel_severity_filter_actor< typename ChannelDescriptorT::value_type, SeverityT, fallback_to_none, fallback_to_none, less, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_keyword< ChannelDescriptorT, ActorT > const& channel_keyword, attribute_name const& severity_name, SeverityCompareT const& severity_compare) +{ + typedef channel_severity_filter_actor< typename ChannelDescriptorT::value_type, SeverityT, fallback_to_none, fallback_to_none, less, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_keyword.get_name(), severity_name, fallback_to_none(), fallback_to_none(), less(), severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename SeverityDescriptorT, template< typename > class ActorT, typename SeverityCompareT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, less, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_name const& channel_name, attribute_keyword< SeverityDescriptorT, ActorT > const& severity_keyword, SeverityCompareT const& severity_compare) +{ + typedef channel_severity_filter_actor< ChannelT, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, less, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_keyword.get_name(), fallback_to_none(), fallback_to_none(), less(), severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelDescriptorT, typename SeverityDescriptorT, template< typename > class ActorT, typename SeverityCompareT > +BOOST_FORCEINLINE channel_severity_filter_actor< typename ChannelDescriptorT::value_type, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, less, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_keyword< ChannelDescriptorT, ActorT > const& channel_keyword, attribute_keyword< SeverityDescriptorT, ActorT > const& severity_keyword, SeverityCompareT const& severity_compare) +{ + typedef channel_severity_filter_actor< typename ChannelDescriptorT::value_type, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, less, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_keyword.get_name(), severity_keyword.get_name(), fallback_to_none(), fallback_to_none(), less(), severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename SeverityT, typename ChannelT, typename ChannelFallbackT, typename ChannelTagT, template< typename > class ActorT, typename SeverityCompareT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, fallback_to_none, less, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_actor< ChannelT, ChannelFallbackT, ChannelTagT, ActorT > const& channel_placeholder, attribute_name const& severity_name, SeverityCompareT const& severity_compare) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, fallback_to_none, less, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_placeholder.get_name(), severity_name, channel_placeholder.get_fallback_policy(), fallback_to_none(), less(), severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename SeverityT, typename SeverityFallbackT, typename SeverityTagT, template< typename > class ActorT, typename SeverityCompareT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, SeverityFallbackT, less, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_name const& channel_name, attribute_actor< SeverityT, SeverityFallbackT, SeverityTagT, ActorT > const& severity_placeholder, SeverityCompareT const& severity_compare) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, SeverityFallbackT, less, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_placeholder.get_name(), fallback_to_none(), severity_placeholder.get_fallback_policy(), less(), severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename ChannelFallbackT, typename ChannelTagT, typename SeverityT, typename SeverityFallbackT, typename SeverityTagT, template< typename > class ActorT, typename SeverityCompareT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, less, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_actor< ChannelT, ChannelFallbackT, ChannelTagT, ActorT > const& channel_placeholder, attribute_actor< SeverityT, SeverityFallbackT, SeverityTagT, ActorT > const& severity_placeholder, SeverityCompareT const& severity_compare) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, less, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_placeholder.get_name(), severity_placeholder.get_name(), channel_placeholder.get_fallback_policy(), severity_placeholder.get_fallback_policy(), less(), severity_compare) }}; + return result_type(act); +} + + +//! \overload +template< typename ChannelT, typename SeverityT, typename SeverityCompareT, typename ChannelOrderT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, fallback_to_none, ChannelOrderT, SeverityCompareT > +channel_severity_filter(attribute_name const& channel_name, attribute_name const& severity_name, SeverityCompareT const& severity_compare, ChannelOrderT const& channel_order) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, fallback_to_none, ChannelOrderT, SeverityCompareT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_name, fallback_to_none(), fallback_to_none(), channel_order, severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename SeverityT, typename ChannelDescriptorT, template< typename > class ActorT, typename SeverityCompareT, typename ChannelOrderT > +BOOST_FORCEINLINE channel_severity_filter_actor< typename ChannelDescriptorT::value_type, SeverityT, fallback_to_none, fallback_to_none, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_keyword< ChannelDescriptorT, ActorT > const& channel_keyword, attribute_name const& severity_name, SeverityCompareT const& severity_compare, ChannelOrderT const& channel_order) +{ + typedef channel_severity_filter_actor< typename ChannelDescriptorT::value_type, SeverityT, fallback_to_none, fallback_to_none, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_keyword.get_name(), severity_name, fallback_to_none(), fallback_to_none(), channel_order, severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename SeverityDescriptorT, template< typename > class ActorT, typename SeverityCompareT, typename ChannelOrderT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_name const& channel_name, attribute_keyword< SeverityDescriptorT, ActorT > const& severity_keyword, SeverityCompareT const& severity_compare, ChannelOrderT const& channel_order) +{ + typedef channel_severity_filter_actor< ChannelT, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_keyword.get_name(), fallback_to_none(), fallback_to_none(), channel_order, severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelDescriptorT, typename SeverityDescriptorT, template< typename > class ActorT, typename SeverityCompareT, typename ChannelOrderT > +BOOST_FORCEINLINE channel_severity_filter_actor< typename ChannelDescriptorT::value_type, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_keyword< ChannelDescriptorT, ActorT > const& channel_keyword, attribute_keyword< SeverityDescriptorT, ActorT > const& severity_keyword, SeverityCompareT const& severity_compare, ChannelOrderT const& channel_order) +{ + typedef channel_severity_filter_actor< typename ChannelDescriptorT::value_type, typename SeverityDescriptorT::value_type, fallback_to_none, fallback_to_none, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_keyword.get_name(), severity_keyword.get_name(), fallback_to_none(), fallback_to_none(), channel_order, severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename SeverityT, typename ChannelT, typename ChannelFallbackT, typename ChannelTagT, template< typename > class ActorT, typename SeverityCompareT, typename ChannelOrderT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, fallback_to_none, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_actor< ChannelT, ChannelFallbackT, ChannelTagT, ActorT > const& channel_placeholder, attribute_name const& severity_name, SeverityCompareT const& severity_compare, ChannelOrderT const& channel_order) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, fallback_to_none, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_placeholder.get_name(), severity_name, channel_placeholder.get_fallback_policy(), fallback_to_none(), channel_order, severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename SeverityT, typename SeverityFallbackT, typename SeverityTagT, template< typename > class ActorT, typename SeverityCompareT, typename ChannelOrderT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, SeverityFallbackT, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_name const& channel_name, attribute_actor< SeverityT, SeverityFallbackT, SeverityTagT, ActorT > const& severity_placeholder, SeverityCompareT const& severity_compare, ChannelOrderT const& channel_order) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, fallback_to_none, SeverityFallbackT, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_name, severity_placeholder.get_name(), fallback_to_none(), severity_placeholder.get_fallback_policy(), channel_order, severity_compare) }}; + return result_type(act); +} + +//! \overload +template< typename ChannelT, typename ChannelFallbackT, typename ChannelTagT, typename SeverityT, typename SeverityFallbackT, typename SeverityTagT, template< typename > class ActorT, typename SeverityCompareT, typename ChannelOrderT > +BOOST_FORCEINLINE channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > +channel_severity_filter(attribute_actor< ChannelT, ChannelFallbackT, ChannelTagT, ActorT > const& channel_placeholder, attribute_actor< SeverityT, SeverityFallbackT, SeverityTagT, ActorT > const& severity_placeholder, SeverityCompareT const& severity_compare, ChannelOrderT const& channel_order) +{ + typedef channel_severity_filter_actor< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, ChannelOrderT, SeverityCompareT, std::allocator< void >, ActorT > result_type; + typedef typename result_type::terminal_type terminal_type; + typename result_type::base_type act = {{ terminal_type(channel_placeholder.get_name(), severity_placeholder.get_name(), channel_placeholder.get_fallback_policy(), severity_placeholder.get_fallback_policy(), channel_order, severity_compare) }}; + return result_type(act); +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +#ifndef BOOST_LOG_DOXYGEN_PASS + +namespace phoenix { + +namespace result_of { + +template< + typename ChannelT, + typename SeverityT, + typename ChannelFallbackT, + typename SeverityFallbackT, + typename ChannelOrderT, + typename SeverityCompareT, + typename AllocatorT +> +struct is_nullary< custom_terminal< boost::log::expressions::channel_severity_filter_terminal< ChannelT, SeverityT, ChannelFallbackT, SeverityFallbackT, ChannelOrderT, SeverityCompareT, AllocatorT > > > : + public mpl::false_ +{ +}; + +} // namespace result_of + +} // namespace phoenix + +#endif + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_CHANNEL_SEVERITY_FILTER_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates/contains.hpp b/boost/log/expressions/predicates/contains.hpp new file mode 100644 index 0000000000..633ba65e8e --- /dev/null +++ b/boost/log/expressions/predicates/contains.hpp @@ -0,0 +1,129 @@ +/* + * 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 contains.hpp + * \author Andrey Semashev + * \date 02.09.2012 + * + * The header contains implementation of a \c contains predicate in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_CONTAINS_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_CONTAINS_HPP_INCLUDED_ + +#include <boost/phoenix/core/actor.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/embedded_string_type.hpp> +#include <boost/log/detail/unary_function_terminal.hpp> +#include <boost/log/detail/attribute_predicate.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/utility/functional/contains.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * The predicate checks if the attribute value contains a substring. The attribute value is assumed to be of a string type. + */ +#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +template< typename T, typename SubstringT, typename FallbackPolicyT = fallback_to_none > +using attribute_contains = aux::attribute_predicate< T, SubstringT, contains_fun, FallbackPolicyT >; + +#else // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +template< typename T, typename SubstringT, typename FallbackPolicyT = fallback_to_none > +class attribute_contains : + public aux::attribute_predicate< T, SubstringT, contains_fun, FallbackPolicyT > +{ + typedef aux::attribute_predicate< T, SubstringT, contains_fun, FallbackPolicyT > base_type; + +public: + /*! + * Initializing constructor + * + * \param name Attribute name + * \param substring The expected attribute value substring + */ + attribute_contains(attribute_name const& name, SubstringT const& substring) : base_type(name, substring) + { + } + + /*! + * Initializing constructor + * + * \param name Attribute name + * \param substring The expected attribute value substring + * \param arg Additional parameter for the fallback policy + */ + template< typename U > + attribute_contains(attribute_name const& name, SubstringT const& substring, U const& arg) : base_type(name, substring, arg) + { + } +}; + +#endif // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, contains the specified substring. + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename SubstringT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_contains< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type, FallbackPolicyT > > > +contains(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& attr, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_contains< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type, FallbackPolicyT > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(attr.get_name(), substring, attr.get_fallback_policy()) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, contains the specified substring. + */ +template< typename DescriptorT, template< typename > class ActorT, typename SubstringT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_contains< typename DescriptorT::value_type, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > > +contains(attribute_keyword< DescriptorT, ActorT > const&, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_contains< typename DescriptorT::value_type, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name(), substring) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, contains the specified substring. + */ +template< typename T, typename SubstringT > +BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< attribute_contains< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > > +contains(attribute_name const& name, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_contains< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(name, substring) }}; + return act; +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_CONTAINS_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates/ends_with.hpp b/boost/log/expressions/predicates/ends_with.hpp new file mode 100644 index 0000000000..1e8785dad0 --- /dev/null +++ b/boost/log/expressions/predicates/ends_with.hpp @@ -0,0 +1,129 @@ +/* + * 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 ends_with.hpp + * \author Andrey Semashev + * \date 02.09.2012 + * + * The header contains implementation of a \c ends_with predicate in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_ENDS_WITH_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_ENDS_WITH_HPP_INCLUDED_ + +#include <boost/phoenix/core/actor.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/embedded_string_type.hpp> +#include <boost/log/detail/unary_function_terminal.hpp> +#include <boost/log/detail/attribute_predicate.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/utility/functional/ends_with.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * The predicate checks if the attribute value ends with a substring. The attribute value is assumed to be of a string type. + */ +#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +template< typename T, typename SubstringT, typename FallbackPolicyT = fallback_to_none > +using attribute_ends_with = aux::attribute_predicate< T, SubstringT, ends_with_fun, FallbackPolicyT >; + +#else // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +template< typename T, typename SubstringT, typename FallbackPolicyT = fallback_to_none > +class attribute_ends_with : + public aux::attribute_predicate< T, SubstringT, ends_with_fun, FallbackPolicyT > +{ + typedef aux::attribute_predicate< T, SubstringT, ends_with_fun, FallbackPolicyT > base_type; + +public: + /*! + * Initializing constructor + * + * \param name Attribute name + * \param substring The expected attribute value ending + */ + attribute_ends_with(attribute_name const& name, SubstringT const& substring) : base_type(name, substring) + { + } + + /*! + * Initializing constructor + * + * \param name Attribute name + * \param substring The expected attribute value ending + * \param arg Additional parameter for the fallback policy + */ + template< typename U > + attribute_ends_with(attribute_name const& name, SubstringT const& substring, U const& arg) : base_type(name, substring, arg) + { + } +}; + +#endif // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, ends with the specified substring. + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename SubstringT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_ends_with< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type, FallbackPolicyT > > > +ends_with(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& attr, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_ends_with< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type, FallbackPolicyT > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(attr.get_name(), substring, attr.get_fallback_policy()) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, ends with the specified substring. + */ +template< typename DescriptorT, template< typename > class ActorT, typename SubstringT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_ends_with< typename DescriptorT::value_type, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > > +ends_with(attribute_keyword< DescriptorT, ActorT > const&, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_ends_with< typename DescriptorT::value_type, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name(), substring) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, ends with the specified substring. + */ +template< typename T, typename SubstringT > +BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< attribute_ends_with< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > > +ends_with(attribute_name const& name, SubstringT const& substring) +{ + typedef aux::unary_function_terminal< attribute_ends_with< T, typename boost::log::aux::make_embedded_string_type< SubstringT >::type > > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(name, substring) }}; + return act; +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_ENDS_WITH_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates/has_attr.hpp b/boost/log/expressions/predicates/has_attr.hpp new file mode 100644 index 0000000000..e4eb72e683 --- /dev/null +++ b/boost/log/expressions/predicates/has_attr.hpp @@ -0,0 +1,172 @@ +/* + * 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 has_attr.hpp + * \author Andrey Semashev + * \date 23.07.2012 + * + * The header contains implementation of a generic attribute presence checker in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_ + +#include <boost/phoenix/core/actor.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/core/record_view.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/attribute_value_set.hpp> +#include <boost/log/attributes/value_visitation.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/detail/unary_function_terminal.hpp> +#include <boost/log/utility/functional/nop.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * An attribute value presence checker. + */ +template< typename T > +class has_attribute +{ +public: + //! Function result_type + typedef bool result_type; + //! Expected attribute value type + typedef T value_type; + +private: + //! Attribute value name + const attribute_name m_name; + //! Visitor invoker + value_visitor_invoker< value_type > m_visitor_invoker; + +public: + /*! + * Initializing constructor + * + * \param name Attribute name + */ + explicit has_attribute(attribute_name const& name) : m_name(name) + { + } + + /*! + * Checking operator + * + * \param arg A set of attribute values or a log record + * \return \c true if the log record contains the sought attribute value, \c false otherwise + */ + template< typename ArgT > + result_type operator() (ArgT const& arg) const + { + return m_visitor_invoker(m_name, arg, nop()).code() == visitation_result::ok; + } +}; + +/*! + * An attribute value presence checker. This specialization does not check the type of the attribute value. + */ +template< > +class has_attribute< void > +{ +public: + //! Function result_type + typedef bool result_type; + //! Expected attribute value type + typedef void value_type; + +private: + //! Attribute name + const attribute_name m_name; + +public: + /*! + * Initializing constructor + * + * \param name Attribute name + */ + explicit has_attribute(attribute_name const& name) : m_name(name) + { + } + + /*! + * Checking operator + * + * \param attrs A set of attribute values + * \return \c true if the log record contains the sought attribute value, \c false otherwise + */ + result_type operator() (attribute_value_set const& attrs) const + { + return attrs.find(m_name) != attrs.end(); + } + + /*! + * Checking operator + * + * \param rec A log record + * \return \c true if the log record contains the sought attribute value, \c false otherwise + */ + result_type operator() (boost::log::record_view const& rec) const + { + return operator()(rec.attribute_values()); + } +}; + +/*! + * The function generates a terminal node in a template expression. The node will check for the attribute value + * presence in a log record. The node will also check that the attribute value has the specified type, if present. + */ +template< typename AttributeValueT > +BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< has_attribute< AttributeValueT > > > has_attr(attribute_name const& name) +{ + typedef aux::unary_function_terminal< has_attribute< AttributeValueT > > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(name) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check for the attribute value + * presence in a log record. + */ +BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< has_attribute< void > > > has_attr(attribute_name const& name) +{ + typedef aux::unary_function_terminal< has_attribute< void > > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(name) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check for the attribute value + * presence in a log record. The node will also check that the attribute value has the specified type, if present. + */ +template< typename DescriptorT, template< typename > class ActorT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< has_attribute< typename DescriptorT::value_type > > > has_attr(attribute_keyword< DescriptorT, ActorT > const&) +{ + typedef aux::unary_function_terminal< has_attribute< typename DescriptorT::value_type > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name()) }}; + return act; +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates/is_debugger_present.hpp b/boost/log/expressions/predicates/is_debugger_present.hpp new file mode 100644 index 0000000000..f7427c8a90 --- /dev/null +++ b/boost/log/expressions/predicates/is_debugger_present.hpp @@ -0,0 +1,107 @@ +/* + * 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 is_debugger_present.hpp + * \author Andrey Semashev + * \date 05.12.2012 + * + * The header contains implementation of the \c is_debugger_present predicate in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_IS_DEBUGGER_PRESENT_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_IS_DEBUGGER_PRESENT_HPP_INCLUDED_ + +#include <boost/log/detail/config.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +#if defined(BOOST_WINDOWS) + +#include <boost/phoenix/core/terminal.hpp> // this is needed to be able to include this header alone, Boost.Phoenix blows up without it +#include <boost/phoenix/function/adapt_callable.hpp> +#include <boost/log/detail/header.hpp> + +#if defined(BOOST_USE_WINDOWS_H) + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0500 +#endif + +#include <windows.h> + +#else // defined(BOOST_USE_WINDOWS_H) + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +extern "C" { + +__declspec(dllimport) int __stdcall IsDebuggerPresent(); + +} // extern "C" + +} // namespace aux + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#endif // BOOST_USE_WINDOWS_H + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +namespace aux { + +struct is_debugger_present +{ + typedef bool result_type; + + result_type operator() () const + { + return IsDebuggerPresent() != 0; + } +}; + +} // namespace aux + +#ifndef BOOST_LOG_DOXYGEN_PASS + +BOOST_PHOENIX_ADAPT_CALLABLE_NULLARY(is_debugger_present, aux::is_debugger_present) + +#else + +/*! + * The function generates a filter that will check whether the logger is attached to the process + */ +unspecified is_debugger_present(); + +#endif + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_WINDOWS + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_IS_DEBUGGER_PRESENT_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates/is_in_range.hpp b/boost/log/expressions/predicates/is_in_range.hpp new file mode 100644 index 0000000000..e819d2bfe2 --- /dev/null +++ b/boost/log/expressions/predicates/is_in_range.hpp @@ -0,0 +1,133 @@ +/* + * 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 is_in_range.hpp + * \author Andrey Semashev + * \date 02.09.2012 + * + * The header contains implementation of an \c is_in_range predicate in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_IS_IN_RANGE_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_IS_IN_RANGE_HPP_INCLUDED_ + +#include <utility> +#include <boost/phoenix/core/actor.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/embedded_string_type.hpp> +#include <boost/log/detail/unary_function_terminal.hpp> +#include <boost/log/detail/attribute_predicate.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/utility/functional/in_range.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * The predicate checks if the attribute value contains a substring. The attribute value is assumed to be of a string type. + */ +#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +template< typename T, typename BoundaryT, typename FallbackPolicyT = fallback_to_none > +using attribute_is_in_range = aux::attribute_predicate< T, std::pair< BoundaryT, BoundaryT >, in_range_fun, FallbackPolicyT >; + +#else // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +template< typename T, typename BoundaryT, typename FallbackPolicyT = fallback_to_none > +class attribute_is_in_range : + public aux::attribute_predicate< T, std::pair< BoundaryT, BoundaryT >, in_range_fun, FallbackPolicyT > +{ + typedef aux::attribute_predicate< T, std::pair< BoundaryT, BoundaryT >, in_range_fun, FallbackPolicyT > base_type; + +public: + /*! + * Initializing constructor + * + * \param name Attribute name + * \param boundaries The expected attribute value boundaries + */ + attribute_is_in_range(attribute_name const& name, std::pair< BoundaryT, BoundaryT > const& boundaries) : base_type(name, boundaries) + { + } + + /*! + * Initializing constructor + * + * \param name Attribute name + * \param boundaries The expected attribute value boundaries + * \param arg Additional parameter for the fallback policy + */ + template< typename U > + attribute_is_in_range(attribute_name const& name, std::pair< BoundaryT, BoundaryT > const& boundaries, U const& arg) : base_type(name, boundaries, arg) + { + } +}; + +#endif // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value + * is in the specified range. The range must be half-open, that is the predicate will be equivalent to <tt>least <= attr < most</tt>. + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename BoundaryT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_is_in_range< T, typename boost::log::aux::make_embedded_string_type< BoundaryT >::type, FallbackPolicyT > > > +is_in_range(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& attr, BoundaryT const& least, BoundaryT const& most) +{ + typedef typename boost::log::aux::make_embedded_string_type< BoundaryT >::type boundary_type; + typedef aux::unary_function_terminal< attribute_is_in_range< T, boundary_type, FallbackPolicyT > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(attr.get_name(), std::pair< boundary_type, boundary_type >(least, most), attr.get_fallback_policy()) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value + * is in the specified range. The range must be half-open, that is the predicate will be equivalent to <tt>least <= attr < most</tt>. + */ +template< typename DescriptorT, template< typename > class ActorT, typename BoundaryT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_is_in_range< typename DescriptorT::value_type, typename boost::log::aux::make_embedded_string_type< BoundaryT >::type > > > +is_in_range(attribute_keyword< DescriptorT, ActorT > const&, BoundaryT const& least, BoundaryT const& most) +{ + typedef typename boost::log::aux::make_embedded_string_type< BoundaryT >::type boundary_type; + typedef aux::unary_function_terminal< attribute_is_in_range< typename DescriptorT::value_type, boundary_type > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name(), std::pair< boundary_type, boundary_type >(least, most)) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value + * is in the specified range. The range must be half-open, that is the predicate will be equivalent to <tt>least <= attr < most</tt>. + */ +template< typename T, typename BoundaryT > +BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< attribute_is_in_range< T, typename boost::log::aux::make_embedded_string_type< BoundaryT >::type > > > +is_in_range(attribute_name const& name, BoundaryT const& least, BoundaryT const& most) +{ + typedef typename boost::log::aux::make_embedded_string_type< BoundaryT >::type boundary_type; + typedef aux::unary_function_terminal< attribute_is_in_range< T, boundary_type > > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(name, std::pair< boundary_type, boundary_type >(least, most)) }}; + return act; +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_IS_IN_RANGE_HPP_INCLUDED_ diff --git a/boost/log/expressions/predicates/matches.hpp b/boost/log/expressions/predicates/matches.hpp new file mode 100644 index 0000000000..798f2e7e4f --- /dev/null +++ b/boost/log/expressions/predicates/matches.hpp @@ -0,0 +1,119 @@ +/* + * 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 matches.hpp + * \author Andrey Semashev + * \date 02.09.2012 + * + * The header contains implementation of a \c matches predicate in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_ + +#include <boost/phoenix/core/actor.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/unary_function_terminal.hpp> +#include <boost/log/detail/attribute_predicate.hpp> +#include <boost/log/expressions/attr_fwd.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/fallback_policy.hpp> +#include <boost/log/utility/functional/matches.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * The predicate checks if the attribute value matches a regular expression. The attribute value is assumed to be of a string type. + */ +template< typename T, typename RegexT, typename FallbackPolicyT = fallback_to_none > +class attribute_matches : + public aux::attribute_predicate< T, typename boost::log::aux::match_traits< RegexT >::compiled_type, matches_fun, FallbackPolicyT > +{ + typedef aux::attribute_predicate< T, typename boost::log::aux::match_traits< RegexT >::compiled_type, matches_fun, FallbackPolicyT > base_type; + +public: + /*! + * Initializing constructor + * + * \param name Attribute name + * \param rex The regular expression to match the attribute value against + */ + attribute_matches(attribute_name const& name, RegexT const& rex) : base_type(name, boost::log::aux::match_traits< RegexT >::compile(rex)) + { + } + + /*! + * Initializing constructor + * + * \param name Attribute name + * \param rex The regular expression to match the attribute value against + * \param arg Additional parameter for the fallback policy + */ + template< typename U > + attribute_matches(attribute_name const& name, RegexT const& rex, U const& arg) : base_type(name, boost::log::aux::match_traits< RegexT >::compile(rex), arg) + { + } +}; + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, matches the specified regular expression. + */ +template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename RegexT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_matches< T, RegexT, FallbackPolicyT > > > +matches(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& attr, RegexT const& rex) +{ + typedef aux::unary_function_terminal< attribute_matches< T, RegexT, FallbackPolicyT > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(attr.get_name(), rex, attr.get_fallback_policy()) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, matches the specified regular expression. + */ +template< typename DescriptorT, template< typename > class ActorT, typename RegexT > +BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_matches< typename DescriptorT::value_type, RegexT > > > +matches(attribute_keyword< DescriptorT, ActorT > const&, RegexT const& rex) +{ + typedef aux::unary_function_terminal< attribute_matches< typename DescriptorT::value_type, RegexT > > terminal_type; + ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name(), rex) }}; + return act; +} + +/*! + * The function generates a terminal node in a template expression. The node will check if the attribute value, + * which is assumed to be a string, matches the specified regular expression. + */ +template< typename T, typename RegexT > +BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< attribute_matches< T, RegexT > > > +matches(attribute_name const& name, RegexT const& rex) +{ + typedef aux::unary_function_terminal< attribute_matches< T, RegexT > > terminal_type; + phoenix::actor< terminal_type > act = {{ terminal_type(name, rex) }}; + return act; +} + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_PREDICATES_MATCHES_HPP_INCLUDED_ diff --git a/boost/log/expressions/record.hpp b/boost/log/expressions/record.hpp new file mode 100644 index 0000000000..d695c2aa65 --- /dev/null +++ b/boost/log/expressions/record.hpp @@ -0,0 +1,50 @@ +/* + * 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 record.hpp + * \author Andrey Semashev + * \date 25.07.2012 + * + * The header contains implementation of a log record placeholder in template expressions. + */ + +#ifndef BOOST_LOG_EXPRESSIONS_RECORD_HPP_INCLUDED_ +#define BOOST_LOG_EXPRESSIONS_RECORD_HPP_INCLUDED_ + +#include <boost/phoenix/core/argument.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +namespace expressions { + +/*! + * Log record placeholder type in formatter template expressions. + */ +typedef phoenix::expression::argument< 1 >::type record_type; + +/*! + * Log record placeholder in formatter template expressions. + */ +const record_type record = {}; + +} // namespace expressions + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_EXPRESSIONS_RECORD_HPP_INCLUDED_ |