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