xref: /trunk/main/tools/inc/tools/diagnose_ex.h (revision 6f51c329)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef TOOLS_DIAGNOSE_EX_H
25 #define TOOLS_DIAGNOSE_EX_H
26 
27 #include <osl/diagnose.h>
28 #include <rtl/ustring.hxx>
29 
30 #include <com/sun/star/uno/RuntimeException.hpp>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 
33 #include <boost/current_function.hpp>
34 
35 
36 #define OSL_UNUSED( expression ) \
37     (void)(expression)
38 
39 #if OSL_DEBUG_LEVEL > 0
40 
41     #include <cppuhelper/exc_hlp.hxx>
42     #include <osl/diagnose.h>
43     #include <osl/thread.h>
44     #include <boost/current_function.hpp>
45     #include <typeinfo>
46 
47     /** reports a caught UNO exception via OSL diagnostics
48 
49         Note that whenever you use this, it might be an indicator that your error
50         handling is not correct ....
51     */
52     #define DBG_UNHANDLED_EXCEPTION()   \
53         ::com::sun::star::uno::Any caught( ::cppu::getCaughtException() ); \
54 	    ::rtl::OString sMessage( "caught an exception!" ); \
55         sMessage += "\nin function:"; \
56         sMessage += BOOST_CURRENT_FUNCTION; \
57 	    sMessage += "\ntype: "; \
58 	    sMessage += ::rtl::OString( caught.getValueTypeName().getStr(), caught.getValueTypeName().getLength(), osl_getThreadTextEncoding() ); \
59         ::com::sun::star::uno::Exception exception; \
60         caught >>= exception; \
61         if ( exception.Message.getLength() ) \
62         { \
63 	        sMessage += "\nmessage: "; \
64 	        sMessage += ::rtl::OString( exception.Message.getStr(), exception.Message.getLength(), osl_getThreadTextEncoding() ); \
65         } \
66         if ( exception.Context.is() ) \
67         { \
68             const char* pContext = typeid( *exception.Context.get() ).name(); \
69             sMessage += "\ncontext: "; \
70             sMessage += pContext; \
71         } \
72         sMessage += "\n"; \
73 	    OSL_ENSURE( false, sMessage )
74 
75 #else   // OSL_DEBUG_LEVEL
76 
77     #define DBG_UNHANDLED_EXCEPTION()
78 
79 #endif  // OSL_DEBUG_LEVEL
80 
81 
82 /** This macro asserts the given condition (in debug mode), and throws
83     an IllegalArgumentException afterwards.
84  */
85 #define ENSURE_ARG_OR_THROW(c, m) if( !(c) ) { \
86                                      OSL_ENSURE(c, m); \
87                                      throw ::com::sun::star::lang::IllegalArgumentException( \
88                                      ::rtl::OUString::createFromAscii(BOOST_CURRENT_FUNCTION) + \
89                                      ::rtl::OUString::createFromAscii( ",\n" m), \
90                                      ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >(), \
91                                      0 ); }
92 #define ENSURE_ARG_OR_THROW2(c, m, ifc, arg) if( !(c) ) { \
93                                                OSL_ENSURE(c, m); \
94                                                throw ::com::sun::star::lang::IllegalArgumentException( \
95                                                ::rtl::OUString::createFromAscii(BOOST_CURRENT_FUNCTION) + \
96                                                ::rtl::OUString::createFromAscii( ",\n" m), \
97                                                ifc, \
98                                                arg ); }
99 
100 /** This macro asserts the given condition (in debug mode), and throws
101     an RuntimeException afterwards.
102  */
103 #define ENSURE_OR_THROW(c, m) if( !(c) ) { \
104                                      OSL_ENSURE(c, m); \
105                                      throw ::com::sun::star::uno::RuntimeException( \
106                                      ::rtl::OUString::createFromAscii(BOOST_CURRENT_FUNCTION) + \
107                                      ::rtl::OUString::createFromAscii( ",\n" m), \
108                                      ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() ); }
109 #define ENSURE_OR_THROW2(c, m, ifc) if( !(c) ) { \
110                                           OSL_ENSURE(c, m); \
111                                           throw ::com::sun::star::uno::RuntimeException( \
112                                           ::rtl::OUString::createFromAscii(BOOST_CURRENT_FUNCTION) + \
113                                           ::rtl::OUString::createFromAscii( ",\n" m), \
114                                           ifc ); }
115 
116 /** This macro asserts the given condition (in debug mode), and
117     returns the given value afterwards.
118  */
119 #define ENSURE_OR_RETURN(c, m, r) if( !(c) ) { \
120                                      OSL_ENSURE(c, m); \
121                                      return r; }
122 
123 /** This macro asserts the given condition (in debug mode), and
124     returns false afterwards.
125  */
126 #define ENSURE_OR_RETURN_FALSE(c, m) \
127     ENSURE_OR_RETURN(c, m, false)
128 
129 /** This macro asserts the given condition (in debug mode), and
130     returns afterwards, without return value "void".
131  */
132 #define ENSURE_OR_RETURN_VOID( c, m ) \
133     if( !(c) )  \
134     { \
135         OSL_ENSURE( c, m ); \
136         return;   \
137     }
138 
139 
140 
141 /** This macro asserts the given condition (in debug mode), and
142     returns afterwards, without return value "void".
143  */
144 #define ENSURE_OR_RETURN_VOID( c, m ) \
145     if( !(c) )  \
146     { \
147         OSL_ENSURE( c, m ); \
148         return;   \
149     }
150 
151 /** asserts a given condition (in debug mode), and continues the most-inner
152     loop if the condition is not met
153 */
154 #define ENSURE_OR_CONTINUE( c, m ) \
155     if ( !(c) ) \
156     {   \
157         OSL_ENSURE( false, m ); \
158         continue;   \
159     }
160 
161 /** asserts a given condition (in debug mode), and continues the most-inner
162     loop if the condition is not met
163 */
164 #define ENSURE_OR_BREAK( c, m ) \
165     if ( !(c) ) \
166     {   \
167         OSL_ENSURE( false, m ); \
168         break;  \
169     }
170 
171 #endif // TOOLS_DIAGNOSE_EX_H
172