1 /*
2 * Copyright Andrey Semashev 2007 - 2015.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
6 */
7 /*!
8 * \file format.hpp
9 * \author Andrey Semashev
10 * \date 15.11.2012
11 *
12 * \brief This header is the Boost.Log library implementation, see the library documentation
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
14 */
15
16 #ifndef BOOST_LOG_DETAIL_FORMAT_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_FORMAT_HPP_INCLUDED_
18
19 #include <cstddef>
20 #include <string>
21 #include <vector>
22 #include <iosfwd>
23 #include <boost/assert.hpp>
24 #include <boost/move/core.hpp>
25 #include <boost/move/utility_core.hpp>
26 #include <boost/core/uncaught_exceptions.hpp>
27 #include <boost/log/detail/config.hpp>
28 #include <boost/log/detail/cleanup_scope_guard.hpp>
29 #include <boost/log/utility/formatting_ostream.hpp>
30 #include <boost/log/detail/header.hpp>
31
32 #ifdef BOOST_HAS_PRAGMA_ONCE
33 #pragma once
34 #endif
35
36 namespace boost {
37
38 BOOST_LOG_OPEN_NAMESPACE
39
40 namespace aux {
41
42 //! An element (either literal or placeholder) of the format string
43 struct format_element
44 {
45 //! Argument placeholder number or -1 if it's not a placeholder (i.e. a literal)
46 int arg_number;
47 //! If the element describes a constant literal, the starting character and length of the literal
48 unsigned int literal_start_pos, literal_len;
49
format_elementboost::aux::format_element50 format_element() : arg_number(0), literal_start_pos(0), literal_len(0)
51 {
52 }
53
literalboost::aux::format_element54 static format_element literal(unsigned int start_pos, unsigned int len)
55 {
56 format_element el;
57 el.arg_number = -1;
58 el.literal_start_pos = start_pos;
59 el.literal_len = len;
60 return el;
61 }
62
positional_argumentboost::aux::format_element63 static format_element positional_argument(unsigned int arg_n)
64 {
65 format_element el;
66 el.arg_number = arg_n;
67 return el;
68 }
69 };
70
71 //! Parsed format string description
72 template< typename CharT >
73 struct format_description
74 {
75 BOOST_COPYABLE_AND_MOVABLE_ALT(format_description)
76
77 public:
78 //! Character type
79 typedef CharT char_type;
80 //! String type
81 typedef std::basic_string< char_type > string_type;
82
83 //! Array of format element descriptors
84 typedef std::vector< format_element > format_element_list;
85
86 //! Characters of all literal parts of the format string
87 string_type literal_chars;
88 //! Format element descriptors
89 format_element_list format_elements;
90
format_descriptionboost::aux::format_description91 BOOST_DEFAULTED_FUNCTION(format_description(), {})
92
93 format_description(format_description const& that) : literal_chars(that.literal_chars), format_elements(that.format_elements)
94 {
95 }
96
format_descriptionboost::aux::format_description97 format_description(BOOST_RV_REF(format_description) that) BOOST_NOEXCEPT
98 {
99 literal_chars.swap(that.literal_chars);
100 format_elements.swap(that.format_elements);
101 }
102
operator =boost::aux::format_description103 format_description& operator= (format_description that) BOOST_NOEXCEPT
104 {
105 literal_chars.swap(that.literal_chars);
106 format_elements.swap(that.format_elements);
107 return *this;
108 }
109 };
110
111 //! Parses format string
112 template< typename CharT >
113 BOOST_LOG_API format_description< CharT > parse_format(const CharT* begin, const CharT* end);
114
115 //! Parses format string
116 template< typename CharT >
parse_format(const CharT * begin)117 BOOST_FORCEINLINE format_description< CharT > parse_format(const CharT* begin)
118 {
119 return parse_format(begin, begin + std::char_traits< CharT >::length(begin));
120 }
121
122 //! Parses format string
123 template< typename CharT, typename TraitsT, typename AllocatorT >
parse_format(std::basic_string<CharT,TraitsT,AllocatorT> const & fmt)124 BOOST_FORCEINLINE format_description< CharT > parse_format(std::basic_string< CharT, TraitsT, AllocatorT > const& fmt)
125 {
126 const CharT* begin = fmt.c_str();
127 return parse_format(begin, begin + fmt.size());
128 }
129
130 //! Formatter object
131 template< typename CharT >
132 class basic_format
133 {
134 public:
135 //! Character type
136 typedef CharT char_type;
137 //! String type
138 typedef std::basic_string< char_type > string_type;
139 //! Stream type
140 typedef basic_formatting_ostream< char_type > stream_type;
141 //! Format description type
142 typedef format_description< char_type > format_description_type;
143
144 //! The pump receives arguments and formats them into strings. At destruction the pump composes the final string in the attached stream.
145 class pump;
146 friend class pump;
147
148 private:
149 //! Formatting params for a single placeholder in the format string
150 struct formatting_params
151 {
152 //! Formatting element index in the format description
153 unsigned int element_idx;
154 //! Formatting result
155 string_type target;
156
formatting_paramsboost::aux::basic_format::formatting_params157 formatting_params() : element_idx(~0u) {}
158 };
159 typedef std::vector< formatting_params > formatting_params_list;
160
161 private:
162 //! Format string description
163 format_description_type m_format;
164 //! Formatting parameters for all placeholders
165 formatting_params_list m_formatting_params;
166 //! Current formatting position
167 unsigned int m_current_idx;
168
169 public:
170 //! Initializing constructor
basic_format(string_type const & fmt)171 explicit basic_format(string_type const& fmt) : m_format(aux::parse_format(fmt)), m_current_idx(0)
172 {
173 init_params();
174 }
175 //! Initializing constructor
basic_format(const char_type * fmt)176 explicit basic_format(const char_type* fmt) : m_format(aux::parse_format(fmt)), m_current_idx(0)
177 {
178 init_params();
179 }
180
181 //! Clears all formatted strings and resets the current formatting position
clear()182 void clear() BOOST_NOEXCEPT
183 {
184 for (typename formatting_params_list::iterator it = m_formatting_params.begin(), end = m_formatting_params.end(); it != end; ++it)
185 {
186 it->target.clear();
187 }
188 m_current_idx = 0;
189 }
190
191 //! Creates a pump that will receive all format arguments and put the formatted string into the stream
make_pump(stream_type & strm)192 pump make_pump(stream_type& strm)
193 {
194 // Flush the stream beforehand so that the pump can safely switch the stream storage string
195 strm.flush();
196 return pump(*this, strm);
197 }
198
199 //! Composes the final string from the formatted pieces
str() const200 string_type str() const
201 {
202 string_type result;
203 compose(result);
204 return BOOST_LOG_NRVO_RESULT(result);
205 }
206
207 private:
208 //! Initializes the formatting params
init_params()209 void init_params()
210 {
211 typename format_description_type::format_element_list::const_iterator it = m_format.format_elements.begin(), end = m_format.format_elements.end();
212 for (; it != end; ++it)
213 {
214 if (it->arg_number >= 0)
215 {
216 if (static_cast< unsigned int >(it->arg_number) >= m_formatting_params.size())
217 m_formatting_params.resize(it->arg_number + 1);
218 m_formatting_params[it->arg_number].element_idx = static_cast< unsigned int >(it - m_format.format_elements.begin());
219 }
220 }
221 }
222
223 //! Composes the final string from the formatted pieces
224 template< typename T >
compose(T & str) const225 void compose(T& str) const
226 {
227 typename format_description_type::format_element_list::const_iterator it = m_format.format_elements.begin(), end = m_format.format_elements.end();
228 for (; it != end; ++it)
229 {
230 if (it->arg_number >= 0)
231 {
232 // This is a placeholder
233 string_type const& target = m_formatting_params[it->arg_number].target;
234 str.append(target.data(), target.size());
235 }
236 else
237 {
238 // This is a literal
239 const char_type* p = m_format.literal_chars.c_str() + it->literal_start_pos;
240 str.append(p, it->literal_len);
241 }
242 }
243 }
244 };
245
246 //! The pump receives arguments and formats them into strings. At destruction the pump composes the final string in the attached stream.
247 template< typename CharT >
248 class basic_format< CharT >::pump
249 {
250 BOOST_MOVABLE_BUT_NOT_COPYABLE(pump)
251
252 private:
253 //! The guard temporarily replaces storage string in the specified stream
254 struct scoped_storage
255 {
scoped_storageboost::aux::basic_format::pump::scoped_storage256 scoped_storage(stream_type& strm, string_type& storage) : m_stream(strm), m_storage_state_backup(strm.rdbuf()->get_storage_state())
257 {
258 strm.attach(storage);
259 }
~scoped_storageboost::aux::basic_format::pump::scoped_storage260 ~scoped_storage()
261 {
262 m_stream.rdbuf()->set_storage_state(m_storage_state_backup);
263 }
264
265 private:
266 stream_type& m_stream;
267 typename stream_type::streambuf_type::storage_state m_storage_state_backup;
268 };
269
270 private:
271 //! Reference to the owner
272 basic_format* m_owner;
273 //! Reference to the stream
274 stream_type* m_stream;
275 //! Unhandled exception count
276 const unsigned int m_exception_count;
277
278 public:
279 //! Initializing constructor
pump(basic_format & owner,stream_type & strm)280 pump(basic_format& owner, stream_type& strm) BOOST_NOEXCEPT : m_owner(&owner), m_stream(&strm), m_exception_count(boost::core::uncaught_exceptions())
281 {
282 }
283
284 //! Move constructor
pump(BOOST_RV_REF (pump)that)285 pump(BOOST_RV_REF(pump) that) BOOST_NOEXCEPT : m_owner(that.m_owner), m_stream(that.m_stream), m_exception_count(that.m_exception_count)
286 {
287 that.m_owner = NULL;
288 that.m_stream = NULL;
289 }
290
291 //! Destructor
BOOST_NOEXCEPT_IF(false)292 ~pump() BOOST_NOEXCEPT_IF(false)
293 {
294 if (m_owner)
295 {
296 // Whether or not the destructor is called because of an exception, the format object has to be cleared
297 boost::log::aux::cleanup_guard< basic_format< char_type > > cleanup1(*m_owner);
298
299 BOOST_ASSERT(m_stream != NULL);
300 if (m_exception_count >= boost::core::uncaught_exceptions())
301 {
302 // Compose the final string in the stream buffer
303 m_stream->flush();
304 m_owner->compose(*m_stream->rdbuf());
305 }
306 }
307 }
308
309 /*!
310 * Puts an argument to the formatter. Note the pump has to be returned by value and not by reference in order this to
311 * work with Boost.Phoenix expressions. Otherwise the pump that is returned from \c basic_format::make_pump is
312 * destroyed after the first call to \c operator%, and the returned reference becomes dangling.
313 */
314 template< typename T >
operator %(T const & val)315 pump operator% (T const& val)
316 {
317 BOOST_ASSERT_MSG(m_owner != NULL && m_stream != NULL, "Boost.Log: This basic_format::pump has already been moved from");
318
319 if (m_owner->m_current_idx < m_owner->m_formatting_params.size())
320 {
321 scoped_storage storage_guard(*m_stream, m_owner->m_formatting_params[m_owner->m_current_idx].target);
322
323 *m_stream << val;
324 m_stream->flush();
325
326 ++m_owner->m_current_idx;
327 }
328
329 return boost::move(*this);
330 }
331 };
332
333 } // namespace aux
334
335 BOOST_LOG_CLOSE_NAMESPACE // namespace log
336
337 } // namespace boost
338
339 #include <boost/log/detail/footer.hpp>
340
341 #endif // BOOST_LOG_DETAIL_FORMAT_HPP_INCLUDED_
342