format.hpp 11 KB

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