Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file dump.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 03.05.2013
|
Chris@16
|
11 *
|
Chris@16
|
12 * This header contains the \c dump output manipulator.
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_
|
Chris@16
|
16 #define BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_
|
Chris@16
|
17
|
Chris@16
|
18 #include <cstddef>
|
Chris@16
|
19 #include <iosfwd>
|
Chris@16
|
20 #include <boost/log/detail/config.hpp>
|
Chris@16
|
21 #include <boost/log/detail/header.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
24 #pragma once
|
Chris@16
|
25 #endif
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28
|
Chris@16
|
29 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
30
|
Chris@16
|
31 namespace aux {
|
Chris@16
|
32
|
Chris@16
|
33 typedef void dump_data_char_t(const void* data, std::size_t size, std::basic_ostream< char >& strm);
|
Chris@16
|
34 extern BOOST_LOG_API dump_data_char_t* dump_data_char;
|
Chris@16
|
35 BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char >& strm)
|
Chris@16
|
36 {
|
Chris@16
|
37 (dump_data_char)(data, size, strm);
|
Chris@16
|
38 }
|
Chris@16
|
39
|
Chris@16
|
40 typedef void dump_data_wchar_t(const void* data, std::size_t size, std::basic_ostream< wchar_t >& strm);
|
Chris@16
|
41 extern BOOST_LOG_API dump_data_wchar_t* dump_data_wchar;
|
Chris@16
|
42 BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< wchar_t >& strm)
|
Chris@16
|
43 {
|
Chris@16
|
44 (dump_data_wchar)(data, size, strm);
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 #if !defined(BOOST_NO_CXX11_CHAR16_T)
|
Chris@16
|
48 typedef void dump_data_char16_t(const void* data, std::size_t size, std::basic_ostream< char16_t >& strm);
|
Chris@16
|
49 extern BOOST_LOG_API dump_data_char16_t* dump_data_char16;
|
Chris@16
|
50 BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char16_t >& strm)
|
Chris@16
|
51 {
|
Chris@16
|
52 (dump_data_char16)(data, size, strm);
|
Chris@16
|
53 }
|
Chris@16
|
54 #endif
|
Chris@16
|
55
|
Chris@16
|
56 #if !defined(BOOST_NO_CXX11_CHAR32_T)
|
Chris@16
|
57 typedef void dump_data_char32_t(const void* data, std::size_t size, std::basic_ostream< char32_t >& strm);
|
Chris@16
|
58 extern BOOST_LOG_API dump_data_char32_t* dump_data_char32;
|
Chris@16
|
59 BOOST_FORCEINLINE void dump_data(const void* data, std::size_t size, std::basic_ostream< char32_t >& strm)
|
Chris@16
|
60 {
|
Chris@16
|
61 (dump_data_char32)(data, size, strm);
|
Chris@16
|
62 }
|
Chris@16
|
63 #endif
|
Chris@16
|
64
|
Chris@16
|
65 template< std::size_t SizeV, typename R >
|
Chris@16
|
66 struct enable_dump_size_based
|
Chris@16
|
67 {
|
Chris@16
|
68 };
|
Chris@16
|
69
|
Chris@16
|
70 template< typename R >
|
Chris@16
|
71 struct enable_dump_size_based< 1u, R >
|
Chris@16
|
72 {
|
Chris@16
|
73 typedef R type;
|
Chris@16
|
74 };
|
Chris@16
|
75
|
Chris@16
|
76 template< typename T, typename R >
|
Chris@16
|
77 struct enable_dump :
|
Chris@16
|
78 public enable_dump_size_based< sizeof(T), R >
|
Chris@16
|
79 {
|
Chris@16
|
80 };
|
Chris@16
|
81
|
Chris@16
|
82 template< typename R >
|
Chris@16
|
83 struct enable_dump< void, R >
|
Chris@16
|
84 {
|
Chris@16
|
85 typedef R type;
|
Chris@16
|
86 };
|
Chris@16
|
87
|
Chris@16
|
88 template< typename R >
|
Chris@16
|
89 struct enable_dump< const void, R >
|
Chris@16
|
90 {
|
Chris@16
|
91 typedef R type;
|
Chris@16
|
92 };
|
Chris@16
|
93
|
Chris@16
|
94 template< typename R >
|
Chris@16
|
95 struct enable_dump< volatile void, R >
|
Chris@16
|
96 {
|
Chris@16
|
97 typedef R type;
|
Chris@16
|
98 };
|
Chris@16
|
99
|
Chris@16
|
100 template< typename R >
|
Chris@16
|
101 struct enable_dump< const volatile void, R >
|
Chris@16
|
102 {
|
Chris@16
|
103 typedef R type;
|
Chris@16
|
104 };
|
Chris@16
|
105
|
Chris@16
|
106 } // namespace aux
|
Chris@16
|
107
|
Chris@16
|
108 /*!
|
Chris@16
|
109 * \brief Manipulator for printing binary representation of the data
|
Chris@16
|
110 */
|
Chris@16
|
111 class dump_manip
|
Chris@16
|
112 {
|
Chris@16
|
113 private:
|
Chris@16
|
114 //! Beginning of the data
|
Chris@16
|
115 const void* m_data;
|
Chris@16
|
116 //! Size of the data, in bytes
|
Chris@16
|
117 std::size_t m_size;
|
Chris@16
|
118
|
Chris@16
|
119 public:
|
Chris@16
|
120 dump_manip(const void* data, std::size_t size) BOOST_NOEXCEPT : m_data(data), m_size(size) {}
|
Chris@16
|
121 dump_manip(dump_manip const& that) BOOST_NOEXCEPT : m_data(that.m_data), m_size(that.m_size) {}
|
Chris@16
|
122
|
Chris@16
|
123 const void* get_data() const BOOST_NOEXCEPT { return m_data; }
|
Chris@16
|
124 std::size_t get_size() const BOOST_NOEXCEPT { return m_size; }
|
Chris@16
|
125 };
|
Chris@16
|
126
|
Chris@16
|
127 //! The operator outputs binary data to a stream
|
Chris@16
|
128 template< typename CharT, typename TraitsT >
|
Chris@16
|
129 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, dump_manip const& manip)
|
Chris@16
|
130 {
|
Chris@16
|
131 if (strm.good())
|
Chris@16
|
132 aux::dump_data(manip.get_data(), manip.get_size(), strm);
|
Chris@16
|
133
|
Chris@16
|
134 return strm;
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 /*!
|
Chris@16
|
138 * \brief Manipulator for printing binary representation of the data with a size limit
|
Chris@16
|
139 */
|
Chris@16
|
140 class bounded_dump_manip :
|
Chris@16
|
141 public dump_manip
|
Chris@16
|
142 {
|
Chris@16
|
143 private:
|
Chris@16
|
144 //! Maximum size to output, in bytes
|
Chris@16
|
145 std::size_t m_max_size;
|
Chris@16
|
146
|
Chris@16
|
147 public:
|
Chris@16
|
148 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
|
149 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
|
150
|
Chris@16
|
151 std::size_t get_max_size() const BOOST_NOEXCEPT { return m_max_size; }
|
Chris@16
|
152 };
|
Chris@16
|
153
|
Chris@16
|
154 //! The operator outputs binary data to a stream
|
Chris@16
|
155 template< typename CharT, typename TraitsT >
|
Chris@16
|
156 inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, bounded_dump_manip const& manip)
|
Chris@16
|
157 {
|
Chris@16
|
158 if (strm.good())
|
Chris@16
|
159 {
|
Chris@16
|
160 const std::size_t size = manip.get_size(), max_size = manip.get_max_size();
|
Chris@16
|
161 if (max_size >= size)
|
Chris@16
|
162 {
|
Chris@16
|
163 aux::dump_data(manip.get_data(), size, strm);
|
Chris@16
|
164 }
|
Chris@16
|
165 else
|
Chris@16
|
166 {
|
Chris@16
|
167 aux::dump_data(manip.get_data(), max_size, strm);
|
Chris@16
|
168 strm << " and " << (size - max_size) << " bytes more";
|
Chris@16
|
169 }
|
Chris@16
|
170 }
|
Chris@16
|
171
|
Chris@16
|
172 return strm;
|
Chris@16
|
173 }
|
Chris@16
|
174
|
Chris@16
|
175 /*!
|
Chris@16
|
176 * \brief Creates a stream manipulator that will output contents of a memory region in hexadecimal form
|
Chris@16
|
177 * \param data The pointer to the beginning of the region
|
Chris@16
|
178 * \param size The size of the region, in bytes
|
Chris@16
|
179 * \return The manipulator that is to be put to a stream
|
Chris@16
|
180 */
|
Chris@16
|
181 template< typename T >
|
Chris@16
|
182 inline typename aux::enable_dump< T, dump_manip >::type dump(T* data, std::size_t size) BOOST_NOEXCEPT
|
Chris@16
|
183 {
|
Chris@16
|
184 return dump_manip((const void*)data, size);
|
Chris@16
|
185 }
|
Chris@16
|
186
|
Chris@16
|
187 /*!
|
Chris@16
|
188 * \brief Creates a stream manipulator that will dump elements of an array in hexadecimal form
|
Chris@16
|
189 * \param data The pointer to the beginning of the array
|
Chris@16
|
190 * \param count The size of the region, in number of \c T elements
|
Chris@16
|
191 * \return The manipulator that is to be put to a stream
|
Chris@16
|
192 */
|
Chris@16
|
193 template< typename T >
|
Chris@16
|
194 inline dump_manip dump_elements(T* data, std::size_t count) BOOST_NOEXCEPT
|
Chris@16
|
195 {
|
Chris@16
|
196 return dump_manip((const void*)data, count * sizeof(T));
|
Chris@16
|
197 }
|
Chris@16
|
198
|
Chris@16
|
199 /*!
|
Chris@16
|
200 * \brief Creates a stream manipulator that will output contents of a memory region in hexadecimal form
|
Chris@16
|
201 * \param data The pointer to the beginning of the region
|
Chris@16
|
202 * \param size The size of the region, in bytes
|
Chris@16
|
203 * \params max_size The maximum number of bytes of the region to output
|
Chris@16
|
204 * \return The manipulator that is to be put to a stream
|
Chris@16
|
205 */
|
Chris@16
|
206 template< typename T >
|
Chris@16
|
207 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
|
208 {
|
Chris@16
|
209 return bounded_dump_manip((const void*)data, size, max_size);
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 /*!
|
Chris@16
|
213 * \brief Creates a stream manipulator that will dump elements of an array in hexadecimal form
|
Chris@16
|
214 * \param data The pointer to the beginning of the array
|
Chris@16
|
215 * \param count The size of the region, in number of \c T elements
|
Chris@16
|
216 * \params max_count The maximum number of elements to output
|
Chris@16
|
217 * \return The manipulator that is to be put to a stream
|
Chris@16
|
218 */
|
Chris@16
|
219 template< typename T >
|
Chris@16
|
220 inline bounded_dump_manip dump_elements(T* data, std::size_t count, std::size_t max_count) BOOST_NOEXCEPT
|
Chris@16
|
221 {
|
Chris@16
|
222 return bounded_dump_manip((const void*)data, count * sizeof(T), max_count * sizeof(T));
|
Chris@16
|
223 }
|
Chris@16
|
224
|
Chris@16
|
225 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
226
|
Chris@16
|
227 } // namespace boost
|
Chris@16
|
228
|
Chris@16
|
229 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
230
|
Chris@16
|
231 #endif // BOOST_LOG_UTILITY_MANIPULATORS_DUMP_HPP_INCLUDED_
|