Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file dump.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 03.05.2013 Chris@16: * Chris@16: * This header contains the \c dump output manipulator. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace aux { Chris@16: Chris@16: typedef void dump_data_char_t(const void* data, std::size_t size, std::basic_ostream< char >& strm); Chris@16: extern BOOST_LOG_API dump_data_char_t* dump_data_char; Chris@16: BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char >& strm) Chris@16: { Chris@16: (dump_data_char)(data, size, strm); Chris@16: } Chris@16: Chris@16: typedef void dump_data_wchar_t(const void* data, std::size_t size, std::basic_ostream< wchar_t >& strm); Chris@16: extern BOOST_LOG_API dump_data_wchar_t* dump_data_wchar; Chris@16: BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< wchar_t >& strm) Chris@16: { Chris@16: (dump_data_wchar)(data, size, strm); Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_CHAR16_T) Chris@16: typedef void dump_data_char16_t(const void* data, std::size_t size, std::basic_ostream< char16_t >& strm); Chris@16: extern BOOST_LOG_API dump_data_char16_t* dump_data_char16; Chris@16: BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char16_t >& strm) Chris@16: { Chris@16: (dump_data_char16)(data, size, strm); Chris@16: } Chris@16: #endif Chris@16: Chris@16: #if !defined(BOOST_NO_CXX11_CHAR32_T) Chris@16: typedef void dump_data_char32_t(const void* data, std::size_t size, std::basic_ostream< char32_t >& strm); Chris@16: extern BOOST_LOG_API dump_data_char32_t* dump_data_char32; Chris@16: BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char32_t >& strm) Chris@16: { Chris@16: (dump_data_char32)(data, size, strm); Chris@16: } Chris@16: #endif Chris@16: Chris@16: template< std::size_t SizeV, typename R > Chris@16: struct enable_dump_size_based Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename R > Chris@16: struct enable_dump_size_based< 1u, R > Chris@16: { Chris@16: typedef R type; Chris@16: }; Chris@16: Chris@16: template< typename T, typename R > Chris@16: struct enable_dump : Chris@16: public enable_dump_size_based< sizeof(T), R > Chris@16: { Chris@16: }; Chris@16: Chris@16: template< typename R > Chris@16: struct enable_dump< void, R > Chris@16: { Chris@16: typedef R type; Chris@16: }; Chris@16: Chris@16: template< typename R > Chris@16: struct enable_dump< const void, R > Chris@16: { Chris@16: typedef R type; Chris@16: }; Chris@16: Chris@16: template< typename R > Chris@16: struct enable_dump< volatile void, R > Chris@16: { Chris@16: typedef R type; Chris@16: }; Chris@16: Chris@16: template< typename R > Chris@16: struct enable_dump< const volatile void, R > Chris@16: { Chris@16: typedef R type; Chris@16: }; Chris@16: Chris@16: } // namespace aux Chris@16: Chris@16: /*! Chris@16: * \brief Manipulator for printing binary representation of the data Chris@16: */ Chris@16: class dump_manip Chris@16: { Chris@16: private: Chris@16: //! Beginning of the data Chris@16: const void* m_data; Chris@16: //! Size of the data, in bytes Chris@16: std::size_t m_size; Chris@16: Chris@16: public: Chris@16: dump_manip(const void* data, std::size_t size) BOOST_NOEXCEPT : m_data(data), m_size(size) {} Chris@16: dump_manip(dump_manip const& that) BOOST_NOEXCEPT : m_data(that.m_data), m_size(that.m_size) {} Chris@16: Chris@16: const void* get_data() const BOOST_NOEXCEPT { return m_data; } Chris@16: std::size_t get_size() const BOOST_NOEXCEPT { return m_size; } Chris@16: }; Chris@16: Chris@16: //! The operator outputs binary data to a stream Chris@16: template< typename CharT, typename TraitsT > Chris@16: inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, dump_manip const& manip) Chris@16: { Chris@16: if (strm.good()) Chris@16: aux::dump_data(manip.get_data(), manip.get_size(), strm); Chris@16: Chris@16: return strm; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \brief Manipulator for printing binary representation of the data with a size limit Chris@16: */ Chris@16: class bounded_dump_manip : Chris@16: public dump_manip Chris@16: { Chris@16: private: Chris@16: //! Maximum size to output, in bytes Chris@16: std::size_t m_max_size; Chris@16: Chris@16: public: Chris@16: 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) {} Chris@16: 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) {} Chris@16: Chris@16: std::size_t get_max_size() const BOOST_NOEXCEPT { return m_max_size; } Chris@16: }; Chris@16: Chris@16: //! The operator outputs binary data to a stream Chris@16: template< typename CharT, typename TraitsT > Chris@16: inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, bounded_dump_manip const& manip) Chris@16: { Chris@16: if (strm.good()) Chris@16: { Chris@16: const std::size_t size = manip.get_size(), max_size = manip.get_max_size(); Chris@16: if (max_size >= size) Chris@16: { Chris@16: aux::dump_data(manip.get_data(), size, strm); Chris@16: } Chris@16: else Chris@16: { Chris@16: aux::dump_data(manip.get_data(), max_size, strm); Chris@16: strm << " and " << (size - max_size) << " bytes more"; Chris@16: } Chris@16: } Chris@16: Chris@16: return strm; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \brief Creates a stream manipulator that will output contents of a memory region in hexadecimal form Chris@16: * \param data The pointer to the beginning of the region Chris@16: * \param size The size of the region, in bytes Chris@16: * \return The manipulator that is to be put to a stream Chris@16: */ Chris@16: template< typename T > Chris@16: inline typename aux::enable_dump< T, dump_manip >::type dump(T* data, std::size_t size) BOOST_NOEXCEPT Chris@16: { Chris@16: return dump_manip((const void*)data, size); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \brief Creates a stream manipulator that will dump elements of an array in hexadecimal form Chris@16: * \param data The pointer to the beginning of the array Chris@16: * \param count The size of the region, in number of \c T elements Chris@16: * \return The manipulator that is to be put to a stream Chris@16: */ Chris@16: template< typename T > Chris@16: inline dump_manip dump_elements(T* data, std::size_t count) BOOST_NOEXCEPT Chris@16: { Chris@16: return dump_manip((const void*)data, count * sizeof(T)); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \brief Creates a stream manipulator that will output contents of a memory region in hexadecimal form Chris@16: * \param data The pointer to the beginning of the region Chris@16: * \param size The size of the region, in bytes Chris@16: * \params max_size The maximum number of bytes of the region to output Chris@16: * \return The manipulator that is to be put to a stream Chris@16: */ Chris@16: template< typename T > Chris@16: inline typename aux::enable_dump< T, bounded_dump_manip >::type dump(T* data, std::size_t size, std::size_t max_size) BOOST_NOEXCEPT Chris@16: { Chris@16: return bounded_dump_manip((const void*)data, size, max_size); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \brief Creates a stream manipulator that will dump elements of an array in hexadecimal form Chris@16: * \param data The pointer to the beginning of the array Chris@16: * \param count The size of the region, in number of \c T elements Chris@16: * \params max_count The maximum number of elements to output Chris@16: * \return The manipulator that is to be put to a stream Chris@16: */ Chris@16: template< typename T > Chris@16: inline bounded_dump_manip dump_elements(T* data, std::size_t count, std::size_t max_count) BOOST_NOEXCEPT Chris@16: { Chris@16: return bounded_dump_manip((const void*)data, count * sizeof(T), max_count * sizeof(T)); Chris@16: } Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_