format.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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. #ifndef BOOST_LOG_DETAIL_FORMAT_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_FORMAT_HPP_INCLUDED_
  17. #include <string>
  18. #include <vector>
  19. #include <iosfwd>
  20. #include <boost/assert.hpp>
  21. #include <boost/move/core.hpp>
  22. #include <boost/move/utility.hpp>
  23. #include <boost/log/detail/config.hpp>
  24. #include <boost/log/detail/unhandled_exception_count.hpp>
  25. #include <boost/log/detail/cleanup_scope_guard.hpp>
  26. #include <boost/log/utility/formatting_ostream.hpp>
  27. #include <boost/log/detail/header.hpp>
  28. #ifdef BOOST_HAS_PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. namespace boost {
  32. BOOST_LOG_OPEN_NAMESPACE
  33. namespace aux {
  34. //! An element (either literal or placeholder) of the format string
  35. struct format_element
  36. {
  37. //! Argument placeholder number or -1 if it's not a placeholder (i.e. a literal)
  38. int arg_number;
  39. //! If the element describes a constant literal, the starting character and length of the literal
  40. unsigned int literal_start_pos, literal_len;
  41. format_element() : arg_number(0), literal_start_pos(0), literal_len(0)
  42. {
  43. }
  44. static format_element literal(unsigned int start_pos, unsigned int len)
  45. {
  46. format_element el;
  47. el.arg_number = -1;
  48. el.literal_start_pos = start_pos;
  49. el.literal_len = len;
  50. return el;
  51. }
  52. static format_element positional_argument(unsigned int arg_n)
  53. {
  54. format_element el;
  55. el.arg_number = arg_n;
  56. return el;
  57. }
  58. };
  59. //! Parsed format string description
  60. template< typename CharT >
  61. struct format_description
  62. {
  63. BOOST_COPYABLE_AND_MOVABLE_ALT(format_description)
  64. public:
  65. //! Character type
  66. typedef CharT char_type;
  67. //! String type
  68. typedef std::basic_string< char_type > string_type;
  69. //! Array of format element descriptors
  70. typedef std::vector< format_element > format_element_list;
  71. //! Characters of all literal parts of the format string
  72. string_type literal_chars;
  73. //! Format element descriptors
  74. format_element_list format_elements;
  75. BOOST_DEFAULTED_FUNCTION(format_description(), {})
  76. format_description(format_description const& that) : literal_chars(that.literal_chars), format_elements(that.format_elements)
  77. {
  78. }
  79. format_description(BOOST_RV_REF(format_description) that)
  80. {
  81. literal_chars.swap(that.literal_chars);
  82. format_elements.swap(that.format_elements);
  83. }
  84. format_description& operator= (format_description that)
  85. {
  86. literal_chars.swap(that.literal_chars);
  87. format_elements.swap(that.format_elements);
  88. return *this;
  89. }
  90. };
  91. //! Parses format string
  92. template< typename CharT >
  93. BOOST_LOG_API format_description< CharT > parse_format(const CharT* begin, const CharT* end);
  94. //! Parses format string
  95. template< typename CharT >
  96. BOOST_FORCEINLINE format_description< CharT > parse_format(const CharT* begin)
  97. {
  98. return parse_format(begin, begin + std::char_traits< CharT >::length(begin));
  99. }
  100. //! Parses format string
  101. template< typename CharT, typename TraitsT, typename AllocatorT >
  102. BOOST_FORCEINLINE format_description< CharT > parse_format(std::basic_string< CharT, TraitsT, AllocatorT > const& fmt)
  103. {
  104. const CharT* begin = fmt.c_str();
  105. return parse_format(begin, begin + fmt.size());
  106. }
  107. //! Formatter object
  108. template< typename CharT >
  109. class basic_format
  110. {
  111. public:
  112. //! Character type
  113. typedef CharT char_type;
  114. //! String type
  115. typedef std::basic_string< char_type > string_type;
  116. //! Stream type
  117. typedef basic_formatting_ostream< char_type > stream_type;
  118. //! Format description type
  119. typedef format_description< char_type > format_description_type;
  120. //! The pump receives arguments and formats them into strings. At destruction the pump composes the final string in the attached stream.
  121. class pump;
  122. friend class pump;
  123. private:
  124. //! Formatting params for a single placeholder in the format string
  125. struct formatting_params
  126. {
  127. //! Formatting element index in the format description
  128. unsigned int element_idx;
  129. //! Formatting result
  130. string_type target;
  131. formatting_params() : element_idx(~0u) {}
  132. };
  133. typedef std::vector< formatting_params > formatting_params_list;
  134. private:
  135. //! Format string description
  136. format_description_type m_format;
  137. //! Formatting parameters for all placeholders
  138. formatting_params_list m_formatting_params;
  139. //! Current formatting position
  140. unsigned int m_current_idx;
  141. public:
  142. //! Initializing constructor
  143. explicit basic_format(string_type const& fmt) : m_format(aux::parse_format(fmt)), m_current_idx(0)
  144. {
  145. init_params();
  146. }
  147. //! Initializing constructor
  148. explicit basic_format(const char_type* fmt) : m_format(aux::parse_format(fmt)), m_current_idx(0)
  149. {
  150. init_params();
  151. }
  152. //! Clears all formatted strings and resets the current formatting position
  153. void clear() BOOST_NOEXCEPT
  154. {
  155. for (typename formatting_params_list::iterator it = m_formatting_params.begin(), end = m_formatting_params.end(); it != end; ++it)
  156. {
  157. it->target.clear();
  158. }
  159. m_current_idx = 0;
  160. }
  161. //! Creates a pump that will receive all format arguments and put the formatted string into the stream
  162. pump make_pump(stream_type& strm) BOOST_NOEXCEPT
  163. {
  164. return pump(*this, strm);
  165. }
  166. //! Composes the final string from the formatted pieces
  167. void compose(string_type& str) const
  168. {
  169. typename format_description_type::format_element_list::const_iterator it = m_format.format_elements.begin(), end = m_format.format_elements.end();
  170. for (; it != end; ++it)
  171. {
  172. if (it->arg_number >= 0)
  173. {
  174. // This is a placeholder
  175. str.append(m_formatting_params[it->arg_number].target);
  176. }
  177. else
  178. {
  179. // This is a literal
  180. const char_type* p = m_format.literal_chars.c_str() + it->literal_start_pos;
  181. str.append(p, it->literal_len);
  182. }
  183. }
  184. }
  185. //! Composes the final string from the formatted pieces
  186. string_type str() const
  187. {
  188. string_type result;
  189. compose(result);
  190. return boost::move(result);
  191. }
  192. private:
  193. //! Initializes the formatting params
  194. void init_params()
  195. {
  196. typename format_description_type::format_element_list::const_iterator it = m_format.format_elements.begin(), end = m_format.format_elements.end();
  197. for (; it != end; ++it)
  198. {
  199. if (it->arg_number >= 0)
  200. {
  201. if (static_cast< unsigned int >(it->arg_number) >= m_formatting_params.size())
  202. m_formatting_params.resize(it->arg_number + 1);
  203. m_formatting_params[it->arg_number].element_idx = static_cast< unsigned int >(it - m_format.format_elements.begin());
  204. }
  205. }
  206. }
  207. };
  208. //! The pump receives arguments and formats them into strings. At destruction the pump composes the final string in the attached stream.
  209. template< typename CharT >
  210. class basic_format< CharT >::pump
  211. {
  212. BOOST_MOVABLE_BUT_NOT_COPYABLE(pump)
  213. private:
  214. //! The guard temporarily replaces storage string in the specified stream
  215. struct scoped_storage
  216. {
  217. scoped_storage(stream_type& strm, string_type& storage) : m_stream(strm), m_storage_backup(*strm.rdbuf()->storage())
  218. {
  219. strm.attach(storage);
  220. }
  221. ~scoped_storage()
  222. {
  223. m_stream.attach(m_storage_backup);
  224. }
  225. private:
  226. stream_type& m_stream;
  227. string_type& m_storage_backup;
  228. };
  229. private:
  230. //! Reference to the owner
  231. basic_format* m_owner;
  232. //! Reference to the stream
  233. stream_type* m_stream;
  234. //! Unhandled exception count
  235. const unsigned int m_exception_count;
  236. public:
  237. //! Initializing constructor
  238. pump(basic_format& owner, stream_type& strm) BOOST_NOEXCEPT : m_owner(&owner), m_stream(&strm), m_exception_count(unhandled_exception_count())
  239. {
  240. }
  241. //! Move constructor
  242. 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)
  243. {
  244. that.m_owner = NULL;
  245. that.m_stream = NULL;
  246. }
  247. //! Destructor
  248. ~pump() BOOST_NOEXCEPT_IF(false)
  249. {
  250. if (m_owner)
  251. {
  252. // Whether or not the destructor is called because of an exception, the format object has to be cleared
  253. boost::log::aux::cleanup_guard< basic_format< char_type > > cleanup1(*m_owner);
  254. BOOST_ASSERT(m_stream != NULL);
  255. if (m_exception_count >= unhandled_exception_count())
  256. {
  257. // Compose the final string in the stream buffer
  258. m_stream->flush();
  259. m_owner->compose(*m_stream->rdbuf()->storage());
  260. }
  261. }
  262. }
  263. /*!
  264. * Puts an argument to the formatter. Note the pump has to be returned by value and not by reference in order this to
  265. * work with Boost.Phoenix expressions. Otherwise the pump that is returned from \c basic_format::make_pump is
  266. * destroyed after the first call to \c operator%, and the returned reference becomes dangling.
  267. */
  268. template< typename T >
  269. pump operator% (T const& val)
  270. {
  271. BOOST_ASSERT_MSG(m_owner != NULL && m_stream != NULL, "Boost.Log: This basic_format::pump has already been moved from");
  272. if (m_owner->m_current_idx < m_owner->m_formatting_params.size())
  273. {
  274. scoped_storage storage_guard(*m_stream, m_owner->m_formatting_params[m_owner->m_current_idx].target);
  275. *m_stream << val;
  276. m_stream->flush();
  277. ++m_owner->m_current_idx;
  278. }
  279. return boost::move(*this);
  280. }
  281. };
  282. } // namespace aux
  283. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  284. } // namespace boost
  285. #include <boost/log/detail/footer.hpp>
  286. #endif // BOOST_LOG_DETAIL_SNPRINTF_HPP_INCLUDED_