diagnostic_information.hpp 6.3 KB

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