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