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