diff options
Diffstat (limited to 'boost/log/utility/manipulators')
-rw-r--r-- | boost/log/utility/manipulators/add_value.hpp | 165 | ||||
-rw-r--r-- | boost/log/utility/manipulators/dump.hpp | 231 | ||||
-rw-r--r-- | boost/log/utility/manipulators/to_log.hpp | 87 |
3 files changed, 483 insertions, 0 deletions
diff --git a/boost/log/utility/manipulators/add_value.hpp b/boost/log/utility/manipulators/add_value.hpp new file mode 100644 index 0000000000..8d4a7c610f --- /dev/null +++ b/boost/log/utility/manipulators/add_value.hpp @@ -0,0 +1,165 @@ +/* + * 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 add_value.hpp + * \author Andrey Semashev + * \date 26.11.2012 + * + * This header contains the \c add_value manipulator. + */ + +#ifndef BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_ +#define BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_ + +#include <boost/mpl/if.hpp> +#include <boost/type_traits/is_scalar.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/embedded_string_type.hpp> +#include <boost/log/attributes/attribute_name.hpp> +#include <boost/log/attributes/attribute_value_impl.hpp> +#include <boost/log/expressions/keyword_fwd.hpp> +#include <boost/log/sources/record_ostream.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +#ifdef _MSC_VER +#pragma warning(push) +// 'boost::log::v2s_mt_nt6::add_value_manip<RefT>::m_value' : reference member is initialized to a temporary that doesn't persist after the constructor exits +// This is intentional since the manipulator can be used with a temporary, which will be used before the streaming expression ends and it is destroyed. +#pragma warning(disable: 4413) +// returning address of local variable or temporary +// This warning refers to add_value_manip<RefT>::get_value() when RefT is an rvalue reference. We store the reference in the manipulator and we intend to return it as is. +#pragma warning(disable: 4172) +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +//! Attribute value manipulator +template< typename RefT > +class add_value_manip +{ +public: + //! Stored reference type + typedef RefT reference_type; + //! Attribute value type + typedef typename remove_cv< typename remove_reference< reference_type >::type >::type value_type; + +private: + // The stored reference type is an lvalue reference since apparently different compilers (GCC and MSVC) have different quirks when rvalue references are stored as members. + // Additionally, MSVC (at least 11.0) has a bug which causes a dangling reference to be stored in the manipulator, if a scalar rvalue is passed to the add_value generator. + // To work around this problem we save the value inside the manipulator in this case. + typedef typename remove_reference< reference_type >::type& lvalue_reference_type; + + typedef typename mpl::if_< + is_scalar< value_type >, + value_type, + lvalue_reference_type + >::type stored_type; + + typedef typename mpl::if_< + is_scalar< value_type >, + value_type, + reference_type + >::type get_value_result_type; + +private: + //! Attribute value + stored_type m_value; + //! Attribute name + attribute_name m_name; + +public: + //! Initializing constructor + add_value_manip(attribute_name const& name, reference_type value) : m_value(static_cast< lvalue_reference_type >(value)), m_name(name) + { + } + + //! Returns attribute name + attribute_name get_name() const { return m_name; } + //! Returns attribute value + get_value_result_type get_value() const { return static_cast< get_value_result_type >(m_value); } +}; + +//! The operator attaches an attribute value to the log record +template< typename CharT, typename RefT > +inline basic_record_ostream< CharT >& operator<< (basic_record_ostream< CharT >& strm, add_value_manip< RefT > const& manip) +{ + typedef typename aux::make_embedded_string_type< typename add_value_manip< RefT >::value_type >::type value_type; + attribute_value value(new attributes::attribute_value_impl< value_type >(manip.get_value())); + strm.get_record().attribute_values().insert(manip.get_name(), value); + return strm; +} + +//! The function creates a manipulator that attaches an attribute value to a log record +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + +template< typename T > +inline add_value_manip< T&& > add_value(attribute_name const& name, T&& value) +{ + return add_value_manip< T&& >(name, static_cast< T&& >(value)); +} + +//! \overload +template< typename DescriptorT, template< typename > class ActorT > +inline add_value_manip< typename DescriptorT::value_type&& > +add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type&& value) +{ + typedef typename DescriptorT::value_type value_type; + return add_value_manip< value_type&& >(DescriptorT::get_name(), static_cast< value_type&& >(value)); +} + +//! \overload +template< typename DescriptorT, template< typename > class ActorT > +inline add_value_manip< typename DescriptorT::value_type& > +add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type& value) +{ + return add_value_manip< typename DescriptorT::value_type& >(DescriptorT::get_name(), value); +} + +//! \overload +template< typename DescriptorT, template< typename > class ActorT > +inline add_value_manip< typename DescriptorT::value_type const& > +add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value) +{ + return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value); +} + +#else // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + +template< typename T > +inline add_value_manip< T const& > add_value(attribute_name const& name, T const& value) +{ + return add_value_manip< T const& >(name, value); +} + +template< typename DescriptorT, template< typename > class ActorT > +inline add_value_manip< typename DescriptorT::value_type const& > +add_value(expressions::attribute_keyword< DescriptorT, ActorT > const&, typename DescriptorT::value_type const& value) +{ + return add_value_manip< typename DescriptorT::value_type const& >(DescriptorT::get_name(), value); +} + +#endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_UTILITY_MANIPULATORS_ADD_VALUE_HPP_INCLUDED_ diff --git a/boost/log/utility/manipulators/dump.hpp b/boost/log/utility/manipulators/dump.hpp new file mode 100644 index 0000000000..99b2fad749 --- /dev/null +++ b/boost/log/utility/manipulators/dump.hpp @@ -0,0 +1,231 @@ +/* + * 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 dump.hpp + * \author Andrey Semashev + * \date 03.05.2013 + * + * This header contains the \c dump output manipulator. + */ + +#ifndef BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_ +#define BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_ + +#include <cstddef> +#include <iosfwd> +#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 aux { + +typedef void dump_data_char_t(const void* data, std::size_t size, std::basic_ostream< char >& strm); +extern BOOST_LOG_API dump_data_char_t* dump_data_char; +BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char >& strm) +{ + (dump_data_char)(data, size, strm); +} + +typedef void dump_data_wchar_t(const void* data, std::size_t size, std::basic_ostream< wchar_t >& strm); +extern BOOST_LOG_API dump_data_wchar_t* dump_data_wchar; +BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< wchar_t >& strm) +{ + (dump_data_wchar)(data, size, strm); +} + +#if !defined(BOOST_NO_CXX11_CHAR16_T) +typedef void dump_data_char16_t(const void* data, std::size_t size, std::basic_ostream< char16_t >& strm); +extern BOOST_LOG_API dump_data_char16_t* dump_data_char16; +BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char16_t >& strm) +{ + (dump_data_char16)(data, size, strm); +} +#endif + +#if !defined(BOOST_NO_CXX11_CHAR32_T) +typedef void dump_data_char32_t(const void* data, std::size_t size, std::basic_ostream< char32_t >& strm); +extern BOOST_LOG_API dump_data_char32_t* dump_data_char32; +BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char32_t >& strm) +{ + (dump_data_char32)(data, size, strm); +} +#endif + +template< std::size_t SizeV, typename R > +struct enable_dump_size_based +{ +}; + +template< typename R > +struct enable_dump_size_based< 1u, R > +{ + typedef R type; +}; + +template< typename T, typename R > +struct enable_dump : + public enable_dump_size_based< sizeof(T), R > +{ +}; + +template< typename R > +struct enable_dump< void, R > +{ + typedef R type; +}; + +template< typename R > +struct enable_dump< const void, R > +{ + typedef R type; +}; + +template< typename R > +struct enable_dump< volatile void, R > +{ + typedef R type; +}; + +template< typename R > +struct enable_dump< const volatile void, R > +{ + typedef R type; +}; + +} // namespace aux + +/*! + * \brief Manipulator for printing binary representation of the data + */ +class dump_manip +{ +private: + //! Beginning of the data + const void* m_data; + //! Size of the data, in bytes + std::size_t m_size; + +public: + dump_manip(const void* data, std::size_t size) BOOST_NOEXCEPT : m_data(data), m_size(size) {} + dump_manip(dump_manip const& that) BOOST_NOEXCEPT : m_data(that.m_data), m_size(that.m_size) {} + + const void* get_data() const BOOST_NOEXCEPT { return m_data; } + std::size_t get_size() const BOOST_NOEXCEPT { return m_size; } +}; + +//! The operator outputs binary data to a stream +template< typename CharT, typename TraitsT > +inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, dump_manip const& manip) +{ + if (strm.good()) + aux::dump_data(manip.get_data(), manip.get_size(), strm); + + return strm; +} + +/*! + * \brief Manipulator for printing binary representation of the data with a size limit + */ +class bounded_dump_manip : + public dump_manip +{ +private: + //! Maximum size to output, in bytes + std::size_t m_max_size; + +public: + bounded_dump_manip(const void* data, std::size_t size, std::size_t max_size) BOOST_NOEXCEPT : dump_manip(data, size), m_max_size(max_size) {} + bounded_dump_manip(bounded_dump_manip const& that) BOOST_NOEXCEPT : dump_manip(static_cast< dump_manip const& >(that)), m_max_size(that.m_max_size) {} + + std::size_t get_max_size() const BOOST_NOEXCEPT { return m_max_size; } +}; + +//! The operator outputs binary data to a stream +template< typename CharT, typename TraitsT > +inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, bounded_dump_manip const& manip) +{ + if (strm.good()) + { + const std::size_t size = manip.get_size(), max_size = manip.get_max_size(); + if (max_size >= size) + { + aux::dump_data(manip.get_data(), size, strm); + } + else + { + aux::dump_data(manip.get_data(), max_size, strm); + strm << " and " << (size - max_size) << " bytes more"; + } + } + + return strm; +} + +/*! + * \brief Creates a stream manipulator that will output contents of a memory region in hexadecimal form + * \param data The pointer to the beginning of the region + * \param size The size of the region, in bytes + * \return The manipulator that is to be put to a stream + */ +template< typename T > +inline typename aux::enable_dump< T, dump_manip >::type dump(T* data, std::size_t size) BOOST_NOEXCEPT +{ + return dump_manip((const void*)data, size); +} + +/*! + * \brief Creates a stream manipulator that will dump elements of an array in hexadecimal form + * \param data The pointer to the beginning of the array + * \param count The size of the region, in number of \c T elements + * \return The manipulator that is to be put to a stream + */ +template< typename T > +inline dump_manip dump_elements(T* data, std::size_t count) BOOST_NOEXCEPT +{ + return dump_manip((const void*)data, count * sizeof(T)); +} + +/*! + * \brief Creates a stream manipulator that will output contents of a memory region in hexadecimal form + * \param data The pointer to the beginning of the region + * \param size The size of the region, in bytes + * \params max_size The maximum number of bytes of the region to output + * \return The manipulator that is to be put to a stream + */ +template< typename T > +inline typename aux::enable_dump< T, bounded_dump_manip >::type dump(T* data, std::size_t size, std::size_t max_size) BOOST_NOEXCEPT +{ + return bounded_dump_manip((const void*)data, size, max_size); +} + +/*! + * \brief Creates a stream manipulator that will dump elements of an array in hexadecimal form + * \param data The pointer to the beginning of the array + * \param count The size of the region, in number of \c T elements + * \params max_count The maximum number of elements to output + * \return The manipulator that is to be put to a stream + */ +template< typename T > +inline bounded_dump_manip dump_elements(T* data, std::size_t count, std::size_t max_count) BOOST_NOEXCEPT +{ + return bounded_dump_manip((const void*)data, count * sizeof(T), max_count * sizeof(T)); +} + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_ diff --git a/boost/log/utility/manipulators/to_log.hpp b/boost/log/utility/manipulators/to_log.hpp new file mode 100644 index 0000000000..51e96f5bc1 --- /dev/null +++ b/boost/log/utility/manipulators/to_log.hpp @@ -0,0 +1,87 @@ +/* + * 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 to_log.hpp + * \author Andrey Semashev + * \date 06.11.2012 + * + * This header contains the \c to_log output manipulator. + */ + +#ifndef BOOST_LOG_UTILITY_MANIPULATORS_TO_LOG_HPP_INCLUDED_ +#define BOOST_LOG_UTILITY_MANIPULATORS_TO_LOG_HPP_INCLUDED_ + +#include <iosfwd> +#include <boost/mpl/bool.hpp> +#include <boost/log/detail/config.hpp> +#include <boost/log/utility/formatting_ostream_fwd.hpp> +#include <boost/log/detail/header.hpp> + +#ifdef BOOST_HAS_PRAGMA_ONCE +#pragma once +#endif + +namespace boost { + +BOOST_LOG_OPEN_NAMESPACE + +/*! + * \brief Generic manipulator for customizing output to log + */ +template< typename T, typename TagT = void > +class to_log_manip +{ +public: + //! Output value type + typedef T value_type; + //! Value tag type + typedef TagT tag_type; + +private: + //! Reference to the value + value_type const& m_value; + +public: + explicit to_log_manip(value_type const& value) : m_value(value) {} + to_log_manip(to_log_manip const& that) : m_value(that.m_value) {} + + value_type const& get() const { return m_value; } +}; + +template< typename CharT, typename TraitsT, typename T, typename TagT > +inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, to_log_manip< T, TagT > manip) +{ + strm << manip.get(); + return strm; +} + +template< typename CharT, typename TraitsT, typename AllocatorT, typename T, typename TagT > +inline basic_formatting_ostream< CharT, TraitsT, AllocatorT >& operator<< (basic_formatting_ostream< CharT, TraitsT, AllocatorT >& strm, to_log_manip< T, TagT > manip) +{ + strm << manip.get(); + return strm; +} + +template< typename T > +inline to_log_manip< T > to_log(T const& value) +{ + return to_log_manip< T >(value); +} + +template< typename TagT, typename T > +inline to_log_manip< T, TagT > to_log(T const& value) +{ + return to_log_manip< T, TagT >(value); +} + +BOOST_LOG_CLOSE_NAMESPACE // namespace log + +} // namespace boost + +#include <boost/log/detail/footer.hpp> + +#endif // BOOST_LOG_UTILITY_MANIPULATORS_TO_LOG_HPP_INCLUDED_ |