diagnostic_information.hpp 6.1 KB

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