diagnostic_information.hpp 6.0 KB

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