diagnostic_information.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //Copyright (c) 2006-2010 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_EXCEPTION_0552D49838DD11DD90146B8956D89593
  5. #define BOOST_EXCEPTION_0552D49838DD11DD90146B8956D89593
  6. #include <boost/config.hpp>
  7. #include <boost/exception/get_error_info.hpp>
  8. #include <boost/exception/info.hpp>
  9. #include <boost/core/enable_if.hpp>
  10. #ifndef BOOST_NO_RTTI
  11. #include <boost/core/demangle.hpp>
  12. #endif
  13. #include <exception>
  14. #include <sstream>
  15. #include <string>
  16. #ifndef BOOST_NO_EXCEPTIONS
  17. #include <boost/exception/current_exception_cast.hpp>
  18. #endif
  19. #ifndef BOOST_EXCEPTION_ENABLE_WARNINGS
  20. #if defined(__GNUC__) && __GNUC__*100+__GNUC_MINOR__>301
  21. #pragma GCC system_header
  22. #endif
  23. #ifdef __clang__
  24. #pragma clang system_header
  25. #endif
  26. #ifdef _MSC_VER
  27. #pragma warning(push,1)
  28. #endif
  29. #endif
  30. #ifndef BOOST_NO_EXCEPTIONS
  31. namespace
  32. boost
  33. {
  34. namespace
  35. exception_detail
  36. {
  37. std::string diagnostic_information_impl( boost::exception const *, std::exception const *, bool, bool );
  38. }
  39. inline
  40. std::string
  41. current_exception_diagnostic_information( bool verbose=true)
  42. {
  43. boost::exception const * be=current_exception_cast<boost::exception const>();
  44. std::exception const * se=current_exception_cast<std::exception const>();
  45. if( be || se )
  46. return exception_detail::diagnostic_information_impl(be,se,true,verbose);
  47. #if defined(__GLIBCXX__) && __cplusplus >= 201103L && !defined(BOOST_NO_RTTI)
  48. else if (auto* p=std::current_exception().__cxa_exception_type())
  49. return "Dynamic exception type: "+boost::core::demangle(p->name());
  50. #endif
  51. else
  52. return "No diagnostic information available.";
  53. }
  54. }
  55. #endif
  56. namespace
  57. boost
  58. {
  59. namespace
  60. exception_detail
  61. {
  62. inline
  63. exception const *
  64. get_boost_exception( exception const * e )
  65. {
  66. return e;
  67. }
  68. inline
  69. exception const *
  70. get_boost_exception( ... )
  71. {
  72. return 0;
  73. }
  74. inline
  75. std::exception const *
  76. get_std_exception( std::exception const * e )
  77. {
  78. return e;
  79. }
  80. inline
  81. std::exception const *
  82. get_std_exception( ... )
  83. {
  84. return 0;
  85. }
  86. inline
  87. char const *
  88. get_diagnostic_information( exception const & x, char const * header )
  89. {
  90. #ifndef BOOST_NO_EXCEPTIONS
  91. try
  92. {
  93. #endif
  94. error_info_container * c=x.data_.get();
  95. if( !c )
  96. x.data_.adopt(c=new exception_detail::error_info_container_impl);
  97. char const * di=c->diagnostic_information(header);
  98. BOOST_ASSERT(di!=0);
  99. return di;
  100. #ifndef BOOST_NO_EXCEPTIONS
  101. }
  102. catch(...)
  103. {
  104. return 0;
  105. }
  106. #endif
  107. }
  108. inline
  109. std::string
  110. diagnostic_information_impl( boost::exception const * be, std::exception const * se, bool with_what, bool verbose )
  111. {
  112. if( !be && !se )
  113. return "Unknown exception.";
  114. #ifndef BOOST_NO_RTTI
  115. if( !be )
  116. be=dynamic_cast<boost::exception const *>(se);
  117. if( !se )
  118. se=dynamic_cast<std::exception const *>(be);
  119. #endif
  120. char const * wh=0;
  121. if( with_what && se )
  122. {
  123. wh=se->what();
  124. if( be && exception_detail::get_diagnostic_information(*be,0)==wh )
  125. return wh;
  126. }
  127. std::ostringstream tmp;
  128. if( be && verbose )
  129. {
  130. char const * const * f=get_error_info<throw_file>(*be);
  131. int const * l=get_error_info<throw_line>(*be);
  132. char const * const * fn=get_error_info<throw_function>(*be);
  133. if( !f && !l && !fn )
  134. tmp << "Throw location unknown (consider using BOOST_THROW_EXCEPTION)\n";
  135. else
  136. {
  137. if( f )
  138. {
  139. tmp << *f;
  140. if( l )
  141. tmp << '(' << *l << "): ";
  142. }
  143. tmp << "Throw in function ";
  144. if( fn )
  145. tmp << *fn;
  146. else
  147. tmp << "(unknown)";
  148. tmp << '\n';
  149. }
  150. }
  151. #ifndef BOOST_NO_RTTI
  152. if ( verbose )
  153. tmp << std::string("Dynamic exception type: ") <<
  154. core::demangle((be?(BOOST_EXCEPTION_DYNAMIC_TYPEID(*be)):(BOOST_EXCEPTION_DYNAMIC_TYPEID(*se))).type_->name()) << '\n';
  155. #endif
  156. if( with_what && se && verbose )
  157. tmp << "std::exception::what: " << (wh ? wh : "(null)") << '\n';
  158. if( be )
  159. if( char const * s=exception_detail::get_diagnostic_information(*be,tmp.str().c_str()) )
  160. if( *s )
  161. return std::string(s);
  162. return tmp.str();
  163. }
  164. }
  165. template <class T>
  166. std::string
  167. diagnostic_information( T const & e, bool verbose=true )
  168. {
  169. return exception_detail::diagnostic_information_impl(exception_detail::get_boost_exception(&e),exception_detail::get_std_exception(&e),true,verbose);
  170. }
  171. inline
  172. char const *
  173. diagnostic_information_what( exception const & e, bool verbose=true ) BOOST_NOEXCEPT_OR_NOTHROW
  174. {
  175. char const * w=0;
  176. #ifndef BOOST_NO_EXCEPTIONS
  177. try
  178. {
  179. #endif
  180. (void) exception_detail::diagnostic_information_impl(&e,0,false,verbose);
  181. if( char const * di=exception_detail::get_diagnostic_information(e,0) )
  182. return di;
  183. else
  184. return "Failed to produce boost::diagnostic_information_what()";
  185. #ifndef BOOST_NO_EXCEPTIONS
  186. }
  187. catch(
  188. ... )
  189. {
  190. }
  191. #endif
  192. return w;
  193. }
  194. }
  195. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  196. #pragma warning(pop)
  197. #endif
  198. #endif