#include <boost/config.hpp>
#include <boost/exception/get_error_info.hpp>
+#include <boost/exception/detail/exception_ptr_base.hpp>
#include <boost/utility/enable_if.hpp>
#include <exception>
#include <sstream>
enum e { value = !enable_boost_exception_overload<T>::value && sizeof(check((T*)0))==sizeof(yes) };
};
-#ifndef BOOST_NO_RTTI
- template <class T>
- inline
- std::string
- dynamic_exception_type( T const & x )
- {
- return std::string("Dynamic exception type: ") + BOOST_EXCEPTION_DYNAMIC_TYPEID(x).name();
- }
-#endif
-
inline
char const *
- get_diagnostic_information( exception const & x )
+ get_diagnostic_information( exception const & x, char const * header )
{
if( error_info_container * c=x.data_.get() )
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
- return c->diagnostic_information();
+ return c->diagnostic_information(header);
#ifndef BOOST_NO_EXCEPTIONS
}
catch(...)
inline
std::string
- boost_diagnostic_information( exception const & x )
+ diagnostic_information_impl( boost::exception const * be, std::exception const * se, bool with_what )
{
- std::ostringstream tmp;
- if( char const * const * f=get_error_info<throw_file>(x) )
- {
- tmp << *f;
- if( int const * l=get_error_info<throw_line>(x) )
- tmp << '(' << *l << "): ";
- }
- tmp << "Throw in function ";
- if( char const * const * fn=get_error_info<throw_function>(x) )
- tmp << *fn;
- else
- tmp << "(unknown)";
- tmp << std::endl;
+ BOOST_ASSERT(be||se);
#ifndef BOOST_NO_RTTI
- tmp << dynamic_exception_type(x) << std::endl;
- if( std::exception const * e=dynamic_cast<std::exception const *>(&x) )
- tmp << "std::exception::what: " << e->what() << std::endl;
+ if( !se )
+ se = dynamic_cast<std::exception const *>(be);
+ if( !be )
+ be = dynamic_cast<boost::exception const *>(se);
#endif
- if( char const * s=exception_detail::get_diagnostic_information(x) )
- if( *s )
- tmp << s;
- return tmp.str();
- }
-
- inline
- std::string
- std_diagnostic_information( std::exception const & x )
- {
+ char const * wh=0;
+ if( with_what && se )
+ {
+ wh=se->what();
+ if( be && exception_detail::get_diagnostic_information(*be,0)==wh )
+ return wh;
+ }
std::ostringstream tmp;
+ if( be )
+ {
+ if( char const * const * f=get_error_info<throw_file>(*be) )
+ {
+ tmp << *f;
+ if( int const * l=get_error_info<throw_line>(*be) )
+ tmp << '(' << *l << "): ";
+ }
+ tmp << "Throw in function ";
+ if( char const * const * fn=get_error_info<throw_function>(*be) )
+ tmp << *fn;
+ else
+ tmp << "(unknown)";
+ tmp << '\n';
+ }
#ifndef BOOST_NO_RTTI
- if( exception const * e=dynamic_cast<exception const *>(&x) )
- return boost_diagnostic_information(*e);
- tmp << dynamic_exception_type(x) << std::endl;
+ tmp << std::string("Dynamic exception type: ") <<
+ (be?BOOST_EXCEPTION_DYNAMIC_TYPEID(*be):BOOST_EXCEPTION_DYNAMIC_TYPEID(*se)).name() << '\n';
#endif
- tmp << "std::exception::what: " << x.what() << std::endl;
+ if( with_what && se )
+ tmp << "std::exception::what: " << wh << '\n';
+ if( be )
+ if( char const * s=exception_detail::get_diagnostic_information(*be,tmp.str().c_str()) )
+ if( *s )
+ return s;
return tmp.str();
}
}
typename enable_if<exception_detail::enable_boost_exception_overload<T>,std::string>::type
diagnostic_information( T const & e )
{
- return exception_detail::boost_diagnostic_information(e);
+ return exception_detail::diagnostic_information_impl(&e,0,true);
}
template <class T>
typename enable_if<exception_detail::enable_std_exception_overload<T>,std::string>::type
diagnostic_information( T const & e )
{
- return exception_detail::std_diagnostic_information(e);
+ return exception_detail::diagnostic_information_impl(0,&e,true);
+ }
+
+ inline
+ char const *
+ diagnostic_information_what( exception const & e ) throw()
+ {
+ char const * w=0;
+#ifndef BOOST_NO_EXCEPTIONS
+ try
+ {
+#endif
+ (void) exception_detail::diagnostic_information_impl(&e,0,false);
+ return exception_detail::get_diagnostic_information(e,0);
+#ifndef BOOST_NO_EXCEPTIONS
+ }
+ catch(
+ ... )
+ {
+ }
+#endif
+ return w;
}
}
std::string
current_exception_diagnostic_information()
{
- if( boost::exception const * e=current_exception_cast<boost::exception const>() )
- return diagnostic_information(*e);
- else if( std::exception const * e=current_exception_cast<std::exception const>() )
- return diagnostic_information(*e);
+ boost::exception const * be=current_exception_cast<boost::exception const>();
+ std::exception const * se=current_exception_cast<std::exception const>();
+ if( be || se )
+ return exception_detail::diagnostic_information_impl(be,se,true);
else
return "No diagnostic information available.";
}
- }
+
+ inline
+ std::string
+ diagnostic_information( exception_detail::exception_ptr_base const & p )
+ {
+ if( !p._empty() )
+ try
+ {
+ p._rethrow();
+ }
+ catch(
+ ... )
+ {
+ return current_exception_diagnostic_information();
+ }
+ return "<empty>";
+ }
+ }
#endif
#endif