c_decorator.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 formatters/c_decorator.hpp
  9. * \author Andrey Semashev
  10. * \date 18.11.2012
  11. *
  12. * The header contains implementation of C-style character decorators.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_C_DECORATOR_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_FORMATTERS_C_DECORATOR_HPP_INCLUDED_
  16. #include <limits>
  17. #include <boost/range/iterator_range_core.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/detail/snprintf.hpp>
  20. #include <boost/log/expressions/formatters/char_decorator.hpp>
  21. #include <boost/log/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. BOOST_LOG_OPEN_NAMESPACE
  27. namespace expressions {
  28. namespace aux {
  29. template< typename >
  30. struct c_decorator_traits;
  31. #ifdef BOOST_LOG_USE_CHAR
  32. template< >
  33. struct c_decorator_traits< char >
  34. {
  35. static boost::iterator_range< const char* const* > get_patterns()
  36. {
  37. static const char* const patterns[] =
  38. {
  39. "\\", "\a", "\b", "\f", "\n", "\r", "\t", "\v", "'", "\"", "?"
  40. };
  41. return boost::make_iterator_range(patterns);
  42. }
  43. static boost::iterator_range< const char* const* > get_replacements()
  44. {
  45. static const char* const replacements[] =
  46. {
  47. "\\\\", "\\a", "\\b", "\\f", "\\n", "\\r", "\\t", "\\v", "\\'", "\\\"", "\\?"
  48. };
  49. return boost::make_iterator_range(replacements);
  50. }
  51. template< unsigned int N >
  52. static std::size_t print_escaped(char (&buf)[N], char c)
  53. {
  54. int n = boost::log::aux::snprintf(buf, N, "\\x%.2X", static_cast< unsigned int >(static_cast< uint8_t >(c)));
  55. if (n < 0)
  56. {
  57. n = 0;
  58. buf[0] = '\0';
  59. }
  60. return static_cast< unsigned int >(n) >= N ? N - 1 : static_cast< unsigned int >(n);
  61. }
  62. };
  63. #endif // BOOST_LOG_USE_CHAR
  64. #ifdef BOOST_LOG_USE_WCHAR_T
  65. template< >
  66. struct c_decorator_traits< wchar_t >
  67. {
  68. static boost::iterator_range< const wchar_t* const* > get_patterns()
  69. {
  70. static const wchar_t* const patterns[] =
  71. {
  72. L"\\", L"\a", L"\b", L"\f", L"\n", L"\r", L"\t", L"\v", L"'", L"\"", L"?"
  73. };
  74. return boost::make_iterator_range(patterns);
  75. }
  76. static boost::iterator_range< const wchar_t* const* > get_replacements()
  77. {
  78. static const wchar_t* const replacements[] =
  79. {
  80. L"\\\\", L"\\a", L"\\b", L"\\f", L"\\n", L"\\r", L"\\t", L"\\v", L"\\'", L"\\\"", L"\\?"
  81. };
  82. return boost::make_iterator_range(replacements);
  83. }
  84. template< unsigned int N >
  85. static std::size_t print_escaped(wchar_t (&buf)[N], wchar_t c)
  86. {
  87. const wchar_t* format;
  88. unsigned int val;
  89. if (sizeof(wchar_t) == 1)
  90. {
  91. format = L"\\x%.2X";
  92. val = static_cast< uint8_t >(c);
  93. }
  94. else if (sizeof(wchar_t) == 2)
  95. {
  96. format = L"\\x%.4X";
  97. val = static_cast< uint16_t >(c);
  98. }
  99. else
  100. {
  101. format = L"\\x%.8X";
  102. val = static_cast< uint32_t >(c);
  103. }
  104. int n = boost::log::aux::swprintf(buf, N, format, val);
  105. if (n < 0)
  106. {
  107. n = 0;
  108. buf[0] = L'\0';
  109. }
  110. return static_cast< unsigned int >(n) >= N ? N - 1 : static_cast< unsigned int >(n);
  111. }
  112. };
  113. #endif // BOOST_LOG_USE_WCHAR_T
  114. template< typename CharT >
  115. struct c_decorator_gen
  116. {
  117. typedef CharT char_type;
  118. template< typename SubactorT >
  119. BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
  120. {
  121. typedef c_decorator_traits< char_type > traits_type;
  122. typedef pattern_replacer< char_type > replacer_type;
  123. typedef char_decorator_actor< SubactorT, replacer_type > result_type;
  124. typedef typename result_type::terminal_type terminal_type;
  125. typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(traits_type::get_patterns(), traits_type::get_replacements())) }};
  126. return result_type(act);
  127. }
  128. };
  129. } // namespace aux
  130. /*!
  131. * C-style decorator generator object. The decorator replaces characters with specific meaning in C
  132. * language with the corresponding escape sequences. The generator provides <tt>operator[]</tt> that
  133. * can be used to construct the actual decorator. For example:
  134. *
  135. * <code>
  136. * c_decor[ stream << attr< std::string >("MyAttr") ]
  137. * </code>
  138. *
  139. * For wide-character formatting there is the similar \c wc_decor decorator generator object.
  140. */
  141. #ifdef BOOST_LOG_USE_CHAR
  142. const aux::c_decorator_gen< char > c_decor = {};
  143. #endif
  144. #ifdef BOOST_LOG_USE_WCHAR_T
  145. const aux::c_decorator_gen< wchar_t > wc_decor = {};
  146. #endif
  147. /*!
  148. * The function creates a C-style decorator generator for arbitrary character type.
  149. */
  150. template< typename CharT >
  151. BOOST_FORCEINLINE aux::c_decorator_gen< CharT > make_c_decor()
  152. {
  153. return aux::c_decorator_gen< CharT >();
  154. }
  155. /*!
  156. * A character decorator implementation that escapes all non-prontable and non-ASCII characters
  157. * in the output with C-style escape sequences.
  158. */
  159. template< typename CharT >
  160. class c_ascii_pattern_replacer :
  161. public pattern_replacer< CharT >
  162. {
  163. private:
  164. //! Base type
  165. typedef pattern_replacer< CharT > base_type;
  166. public:
  167. //! Result type
  168. typedef typename base_type::result_type result_type;
  169. //! Character type
  170. typedef typename base_type::char_type char_type;
  171. //! String type
  172. typedef typename base_type::string_type string_type;
  173. private:
  174. //! Traits type
  175. typedef aux::c_decorator_traits< char_type > traits_type;
  176. public:
  177. //! Default constructor
  178. c_ascii_pattern_replacer() : base_type(traits_type::get_patterns(), traits_type::get_replacements())
  179. {
  180. }
  181. //! Applies string replacements starting from the specified position
  182. result_type operator() (string_type& str, typename string_type::size_type start_pos = 0) const
  183. {
  184. base_type::operator() (str, start_pos);
  185. typedef typename string_type::iterator string_iterator;
  186. for (string_iterator it = str.begin() + start_pos, end = str.end(); it != end; ++it)
  187. {
  188. char_type c = *it;
  189. if (c < 0x20 || c > 0x7e)
  190. {
  191. char_type buf[(std::numeric_limits< char_type >::digits + 3) / 4 + 3];
  192. std::size_t n = traits_type::print_escaped(buf, c);
  193. std::size_t pos = it - str.begin();
  194. str.replace(pos, 1, buf, n);
  195. it = str.begin() + n - 1;
  196. end = str.end();
  197. }
  198. }
  199. }
  200. };
  201. namespace aux {
  202. template< typename CharT >
  203. struct c_ascii_decorator_gen
  204. {
  205. typedef CharT char_type;
  206. template< typename SubactorT >
  207. BOOST_FORCEINLINE char_decorator_actor< SubactorT, c_ascii_pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
  208. {
  209. typedef c_ascii_pattern_replacer< char_type > replacer_type;
  210. typedef char_decorator_actor< SubactorT, replacer_type > result_type;
  211. typedef typename result_type::terminal_type terminal_type;
  212. typename result_type::base_type act = {{ terminal_type(subactor, replacer_type()) }};
  213. return result_type(act);
  214. }
  215. };
  216. } // namespace aux
  217. /*!
  218. * C-style decorator generator object. Acts similarly to \c c_decor, except that \c c_ascii_decor also
  219. * converts all non-ASCII and non-printable ASCII characters, except for space character, into
  220. * C-style hexadecimal escape sequences. The generator provides <tt>operator[]</tt> that
  221. * can be used to construct the actual decorator. For example:
  222. *
  223. * <code>
  224. * c_ascii_decor[ stream << attr< std::string >("MyAttr") ]
  225. * </code>
  226. *
  227. * For wide-character formatting there is the similar \c wc_ascii_decor decorator generator object.
  228. */
  229. #ifdef BOOST_LOG_USE_CHAR
  230. const aux::c_ascii_decorator_gen< char > c_ascii_decor = {};
  231. #endif
  232. #ifdef BOOST_LOG_USE_WCHAR_T
  233. const aux::c_ascii_decorator_gen< wchar_t > wc_ascii_decor = {};
  234. #endif
  235. /*!
  236. * The function creates a C-style decorator generator for arbitrary character type.
  237. */
  238. template< typename CharT >
  239. BOOST_FORCEINLINE aux::c_ascii_decorator_gen< CharT > make_c_ascii_decor()
  240. {
  241. return aux::c_ascii_decorator_gen< CharT >();
  242. }
  243. } // namespace expressions
  244. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  245. } // namespace boost
  246. #include <boost/log/detail/footer.hpp>
  247. #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_C_DECORATOR_HPP_INCLUDED_