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