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)
9 * \author Andrey Semashev
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.
16 #ifndef BOOST_LOG_DETAIL_FORMAT_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_FORMAT_HPP_INCLUDED_
22 #include <boost/assert.hpp>
23 #include <boost/move/core.hpp>
24 #include <boost/move/utility_core.hpp>
25 #include <boost/log/detail/config.hpp>
26 #include <boost/log/detail/unhandled_exception_count.hpp>
27 #include <boost/log/detail/cleanup_scope_guard.hpp>
28 #include <boost/log/utility/formatting_ostream.hpp>
29 #include <boost/log/detail/header.hpp>
31 #ifdef BOOST_HAS_PRAGMA_ONCE
37 BOOST_LOG_OPEN_NAMESPACE
41 //! An element (either literal or placeholder) of the format string
44 //! Argument placeholder number or -1 if it's not a placeholder (i.e. a literal)
46 //! If the element describes a constant literal, the starting character and length of the literal
47 unsigned int literal_start_pos, literal_len;
49 format_element() : arg_number(0), literal_start_pos(0), literal_len(0)
53 static format_element literal(unsigned int start_pos, unsigned int len)
57 el.literal_start_pos = start_pos;
62 static format_element positional_argument(unsigned int arg_n)
65 el.arg_number = arg_n;
70 //! Parsed format string description
71 template< typename CharT >
72 struct format_description
74 BOOST_COPYABLE_AND_MOVABLE_ALT(format_description)
78 typedef CharT char_type;
80 typedef std::basic_string< char_type > string_type;
82 //! Array of format element descriptors
83 typedef std::vector< format_element > format_element_list;
85 //! Characters of all literal parts of the format string
86 string_type literal_chars;
87 //! Format element descriptors
88 format_element_list format_elements;
90 BOOST_DEFAULTED_FUNCTION(format_description(), {})
92 format_description(format_description const& that) : literal_chars(that.literal_chars), format_elements(that.format_elements)
96 format_description(BOOST_RV_REF(format_description) that)
98 literal_chars.swap(that.literal_chars);
99 format_elements.swap(that.format_elements);
102 format_description& operator= (format_description that)
104 literal_chars.swap(that.literal_chars);
105 format_elements.swap(that.format_elements);
110 //! Parses format string
111 template< typename CharT >
112 BOOST_LOG_API format_description< CharT > parse_format(const CharT* begin, const CharT* end);
114 //! Parses format string
115 template< typename CharT >
116 BOOST_FORCEINLINE format_description< CharT > parse_format(const CharT* begin)
118 return parse_format(begin, begin + std::char_traits< CharT >::length(begin));
121 //! Parses format string
122 template< typename CharT, typename TraitsT, typename AllocatorT >
123 BOOST_FORCEINLINE format_description< CharT > parse_format(std::basic_string< CharT, TraitsT, AllocatorT > const& fmt)
125 const CharT* begin = fmt.c_str();
126 return parse_format(begin, begin + fmt.size());
130 template< typename CharT >
135 typedef CharT char_type;
137 typedef std::basic_string< char_type > string_type;
139 typedef basic_formatting_ostream< char_type > stream_type;
140 //! Format description type
141 typedef format_description< char_type > format_description_type;
143 //! The pump receives arguments and formats them into strings. At destruction the pump composes the final string in the attached stream.
148 //! Formatting params for a single placeholder in the format string
149 struct formatting_params
151 //! Formatting element index in the format description
152 unsigned int element_idx;
153 //! Formatting result
156 formatting_params() : element_idx(~0u) {}
158 typedef std::vector< formatting_params > formatting_params_list;
161 //! Format string description
162 format_description_type m_format;
163 //! Formatting parameters for all placeholders
164 formatting_params_list m_formatting_params;
165 //! Current formatting position
166 unsigned int m_current_idx;
169 //! Initializing constructor
170 explicit basic_format(string_type const& fmt) : m_format(aux::parse_format(fmt)), m_current_idx(0)
174 //! Initializing constructor
175 explicit basic_format(const char_type* fmt) : m_format(aux::parse_format(fmt)), m_current_idx(0)
180 //! Clears all formatted strings and resets the current formatting position
181 void clear() BOOST_NOEXCEPT
183 for (typename formatting_params_list::iterator it = m_formatting_params.begin(), end = m_formatting_params.end(); it != end; ++it)
190 //! Creates a pump that will receive all format arguments and put the formatted string into the stream
191 pump make_pump(stream_type& strm)
193 // Flush the stream beforehand so that the pump can safely switch the stream storage string
195 return pump(*this, strm);
198 //! Composes the final string from the formatted pieces
199 string_type str() const
203 return BOOST_LOG_NRVO_RESULT(result);
207 //! Initializes the formatting params
210 typename format_description_type::format_element_list::const_iterator it = m_format.format_elements.begin(), end = m_format.format_elements.end();
211 for (; it != end; ++it)
213 if (it->arg_number >= 0)
215 if (static_cast< unsigned int >(it->arg_number) >= m_formatting_params.size())
216 m_formatting_params.resize(it->arg_number + 1);
217 m_formatting_params[it->arg_number].element_idx = static_cast< unsigned int >(it - m_format.format_elements.begin());
222 //! Composes the final string from the formatted pieces
223 template< typename T >
224 void compose(T& str) const
226 typename format_description_type::format_element_list::const_iterator it = m_format.format_elements.begin(), end = m_format.format_elements.end();
227 for (; it != end; ++it)
229 if (it->arg_number >= 0)
231 // This is a placeholder
232 string_type const& target = m_formatting_params[it->arg_number].target;
233 str.append(target.data(), target.size());
238 const char_type* p = m_format.literal_chars.c_str() + it->literal_start_pos;
239 str.append(p, it->literal_len);
245 //! The pump receives arguments and formats them into strings. At destruction the pump composes the final string in the attached stream.
246 template< typename CharT >
247 class basic_format< CharT >::pump
249 BOOST_MOVABLE_BUT_NOT_COPYABLE(pump)
252 //! The guard temporarily replaces storage string in the specified stream
253 struct scoped_storage
255 scoped_storage(stream_type& strm, string_type& storage) : m_stream(strm), m_storage_state_backup(strm.rdbuf()->get_storage_state())
257 strm.attach(storage);
261 m_stream.rdbuf()->set_storage_state(m_storage_state_backup);
265 stream_type& m_stream;
266 typename stream_type::streambuf_type::storage_state m_storage_state_backup;
270 //! Reference to the owner
271 basic_format* m_owner;
272 //! Reference to the stream
273 stream_type* m_stream;
274 //! Unhandled exception count
275 const unsigned int m_exception_count;
278 //! Initializing constructor
279 pump(basic_format& owner, stream_type& strm) BOOST_NOEXCEPT : m_owner(&owner), m_stream(&strm), m_exception_count(unhandled_exception_count())
284 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)
287 that.m_stream = NULL;
291 ~pump() BOOST_NOEXCEPT_IF(false)
295 // Whether or not the destructor is called because of an exception, the format object has to be cleared
296 boost::log::aux::cleanup_guard< basic_format< char_type > > cleanup1(*m_owner);
298 BOOST_ASSERT(m_stream != NULL);
299 if (m_exception_count >= unhandled_exception_count())
301 // Compose the final string in the stream buffer
303 m_owner->compose(*m_stream->rdbuf());
309 * Puts an argument to the formatter. Note the pump has to be returned by value and not by reference in order this to
310 * work with Boost.Phoenix expressions. Otherwise the pump that is returned from \c basic_format::make_pump is
311 * destroyed after the first call to \c operator%, and the returned reference becomes dangling.
313 template< typename T >
314 pump operator% (T const& val)
316 BOOST_ASSERT_MSG(m_owner != NULL && m_stream != NULL, "Boost.Log: This basic_format::pump has already been moved from");
318 if (m_owner->m_current_idx < m_owner->m_formatting_params.size())
320 scoped_storage storage_guard(*m_stream, m_owner->m_formatting_params[m_owner->m_current_idx].target);
325 ++m_owner->m_current_idx;
328 return boost::move(*this);
334 BOOST_LOG_CLOSE_NAMESPACE // namespace log
338 #include <boost/log/detail/footer.hpp>
340 #endif // BOOST_LOG_DETAIL_FORMAT_HPP_INCLUDED_