diagnostics.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #ifndef BOOST_LEAF_DIAGNOSTICS_HPP_INCLUDED
  2. #define BOOST_LEAF_DIAGNOSTICS_HPP_INCLUDED
  3. // Copyright 2018-2024 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/leaf/config.hpp>
  7. #include <boost/leaf/context.hpp>
  8. #include <boost/leaf/handle_errors.hpp>
  9. namespace boost { namespace leaf {
  10. #if BOOST_LEAF_CFG_DIAGNOSTICS
  11. class diagnostic_info: public error_info
  12. {
  13. void const * tup_;
  14. void (*print_tuple_contents_)(std::ostream &, void const * tup, error_id to_print, char const * & prefix);
  15. protected:
  16. diagnostic_info( diagnostic_info const & ) noexcept = default;
  17. template <class Tup>
  18. BOOST_LEAF_CONSTEXPR diagnostic_info( error_info const & ei, Tup const & tup ) noexcept:
  19. error_info(ei),
  20. tup_(&tup),
  21. print_tuple_contents_(&detail::print_tuple_contents<Tup>)
  22. {
  23. }
  24. template <class CharT, class Traits>
  25. void print_diagnostic_info(std::basic_ostream<CharT, Traits> & os) const
  26. {
  27. print_error_info(os);
  28. char const * prefix = exception() ? nullptr : "\nCaught:" BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER;
  29. print_tuple_contents_(os, tup_, error(), prefix);
  30. }
  31. template <class CharT, class Traits>
  32. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_info const & x )
  33. {
  34. x.print_diagnostic_info(os);
  35. return os << '\n';
  36. }
  37. };
  38. namespace detail
  39. {
  40. struct diagnostic_info_: diagnostic_info
  41. {
  42. template <class Tup>
  43. BOOST_LEAF_CONSTEXPR diagnostic_info_( error_info const & ei, Tup const & tup ) noexcept:
  44. diagnostic_info(ei, tup)
  45. {
  46. }
  47. };
  48. template <>
  49. struct handler_argument_traits<diagnostic_info const &>: handler_argument_always_available<e_source_location>
  50. {
  51. template <class Tup>
  52. BOOST_LEAF_CONSTEXPR static diagnostic_info_ get( Tup const & tup, error_info const & ei ) noexcept
  53. {
  54. return diagnostic_info_(ei, tup);
  55. }
  56. };
  57. }
  58. #else
  59. class diagnostic_info: public error_info
  60. {
  61. protected:
  62. diagnostic_info( diagnostic_info const & ) noexcept = default;
  63. BOOST_LEAF_CONSTEXPR diagnostic_info( error_info const & ei ) noexcept:
  64. error_info(ei)
  65. {
  66. }
  67. template <class CharT, class Traits>
  68. void print_diagnostic_info( std::basic_ostream<CharT, Traits> & os ) const
  69. {
  70. print_error_info(os);
  71. os << "\nboost::leaf::diagnostic_info N/A due to BOOST_LEAF_CFG_DIAGNOSTICS=0";
  72. }
  73. template <class CharT, class Traits>
  74. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_info const & x )
  75. {
  76. x.print_diagnostic_info(os);
  77. return os << "\n";
  78. }
  79. };
  80. namespace detail
  81. {
  82. struct diagnostic_info_: diagnostic_info
  83. {
  84. BOOST_LEAF_CONSTEXPR diagnostic_info_( error_info const & ei ) noexcept:
  85. diagnostic_info(ei)
  86. {
  87. }
  88. };
  89. template <>
  90. struct handler_argument_traits<diagnostic_info const &>: handler_argument_always_available<e_source_location>
  91. {
  92. template <class Tup>
  93. BOOST_LEAF_CONSTEXPR static diagnostic_info_ get( Tup const &, error_info const & ei ) noexcept
  94. {
  95. return diagnostic_info_(ei);
  96. }
  97. };
  98. }
  99. #endif
  100. ////////////////////////////////////////
  101. #if BOOST_LEAF_CFG_DIAGNOSTICS
  102. #if BOOST_LEAF_CFG_CAPTURE
  103. class diagnostic_details: public diagnostic_info
  104. {
  105. detail::dynamic_allocator const * const da_;
  106. protected:
  107. diagnostic_details( diagnostic_details const & ) noexcept = default;
  108. template <class Tup>
  109. BOOST_LEAF_CONSTEXPR diagnostic_details( error_info const & ei, Tup const & tup, detail::dynamic_allocator const * da ) noexcept:
  110. diagnostic_info(ei, tup),
  111. da_(da)
  112. {
  113. }
  114. template <class CharT, class Traits>
  115. void print_diagnostic_details( std::basic_ostream<CharT, Traits> & os) const
  116. {
  117. print_diagnostic_info(os);
  118. if( da_ )
  119. {
  120. char const * prefix = "\nDiagnostic details:" BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER;
  121. da_->print(os, error(), prefix);
  122. }
  123. }
  124. template <class CharT, class Traits>
  125. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_details const & x )
  126. {
  127. x.print_diagnostic_details(os);
  128. return os << '\n';
  129. }
  130. };
  131. namespace detail
  132. {
  133. struct diagnostic_details_: diagnostic_details
  134. {
  135. template <class Tup>
  136. BOOST_LEAF_CONSTEXPR diagnostic_details_( error_info const & ei, Tup const & tup, dynamic_allocator const * da ) noexcept:
  137. diagnostic_details(ei, tup, da)
  138. {
  139. }
  140. };
  141. template <>
  142. struct handler_argument_traits<diagnostic_details const &>: handler_argument_always_available<e_source_location, dynamic_allocator>
  143. {
  144. template <class Tup>
  145. BOOST_LEAF_CONSTEXPR static diagnostic_details_ get( Tup const & tup, error_info const & ei ) noexcept
  146. {
  147. slot<dynamic_allocator> const * da = find_in_tuple<slot<dynamic_allocator>>(tup);
  148. return diagnostic_details_(ei, tup, da ? da->has_value_any_key() : nullptr );
  149. }
  150. };
  151. }
  152. #else
  153. class diagnostic_details: public diagnostic_info
  154. {
  155. protected:
  156. diagnostic_details( diagnostic_details const & ) noexcept = default;
  157. template <class Tup>
  158. BOOST_LEAF_CONSTEXPR diagnostic_details( error_info const & ei, Tup const & tup ) noexcept:
  159. diagnostic_info(ei, tup)
  160. {
  161. }
  162. template <class CharT, class Traits>
  163. void print_diagnostic_details( std::basic_ostream<CharT, Traits> & os ) const
  164. {
  165. print_diagnostic_info(os);
  166. os << "\nboost::leaf::diagnostic_details N/A due to BOOST_LEAF_CFG_CAPTURE=0";
  167. }
  168. template <class CharT, class Traits>
  169. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_details const & x )
  170. {
  171. x.print_diagnostic_details(os);
  172. return os << "\n";
  173. }
  174. };
  175. namespace detail
  176. {
  177. struct diagnostic_details_: diagnostic_details
  178. {
  179. template <class Tup>
  180. BOOST_LEAF_CONSTEXPR diagnostic_details_( error_info const & ei, Tup const & tup ) noexcept:
  181. diagnostic_details(ei, tup)
  182. {
  183. }
  184. };
  185. template <>
  186. struct handler_argument_traits<diagnostic_details const &>: handler_argument_always_available<e_source_location>
  187. {
  188. template <class Tup>
  189. BOOST_LEAF_CONSTEXPR static diagnostic_details_ get( Tup const & tup, error_info const & ei ) noexcept
  190. {
  191. return diagnostic_details_(ei, tup);
  192. }
  193. };
  194. }
  195. #endif
  196. #else
  197. class diagnostic_details: public diagnostic_info
  198. {
  199. protected:
  200. diagnostic_details( diagnostic_details const & ) noexcept = default;
  201. BOOST_LEAF_CONSTEXPR diagnostic_details( error_info const & ei ) noexcept:
  202. diagnostic_info(ei)
  203. {
  204. }
  205. template <class CharT, class Traits>
  206. void print_diagnostic_details( std::basic_ostream<CharT, Traits> & os ) const
  207. {
  208. print_error_info(os);
  209. os << "\nboost::leaf::diagnostic_details N/A due to BOOST_LEAF_CFG_DIAGNOSTICS=0";
  210. }
  211. template <class CharT, class Traits>
  212. friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_details const & x )
  213. {
  214. x.print_diagnostic_details(os);
  215. return os << "\n";
  216. }
  217. };
  218. namespace detail
  219. {
  220. struct diagnostic_details_: diagnostic_details
  221. {
  222. BOOST_LEAF_CONSTEXPR diagnostic_details_( error_info const & ei ) noexcept:
  223. diagnostic_details(ei)
  224. {
  225. }
  226. };
  227. template <>
  228. struct handler_argument_traits<diagnostic_details const &>: handler_argument_always_available<e_source_location>
  229. {
  230. template <class Tup>
  231. BOOST_LEAF_CONSTEXPR static diagnostic_details_ get( Tup const &, error_info const & ei ) noexcept
  232. {
  233. return diagnostic_details_(ei);
  234. }
  235. };
  236. }
  237. #endif
  238. using verbose_diagnostic_info = diagnostic_details;
  239. } }
  240. #endif // BOOST_LEAF_DIAGNOSTICS_HPP_INCLUDED