update dev300-m58
[ooovba.git] / framework / inc / macros / debug / assertion.hxx
blobb61319ef0310bb0fe8b12d15b8688149e6029671
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: assertion.hxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __FRAMEWORK_MACROS_DEBUG_ASSERTION_HXX_
32 #define __FRAMEWORK_MACROS_DEBUG_ASSERTION_HXX_
34 //_________________________________________________________________________________________________________________
35 // includes
36 //_________________________________________________________________________________________________________________
38 #if defined( ENABLE_ASSERTIONS ) || defined( ENABLE_WARNINGS )
40 #ifndef _OSL_DIAGNOSE_H_
41 #include <osl/diagnose.h>
42 #endif
44 #ifndef _RTL_STRBUF_HXX_
45 #include <rtl/strbuf.hxx>
46 #endif
48 #endif
50 //*****************************************************************************************************************
51 // special macros for assertion handling
52 // 1) LOGTYPE use it to define the output of all assertions, errors, exception infos
53 // 2) LOGFILE_ASSERTIONS use it to define the file name to log assertions if LOGTYPE=LOGTYPE_FILE...
54 // 3) LOGFILE_WARNINGS use it to define the file name to log warnings if LOGTYPE=LOGTYPE_FILE...
56 // active for "non product":
58 // 4) LOG_ASSERT( BCONDITION, STEXT ) assert some critical errors wich depend from given condition
59 // 4a) LOG_ASSERT2( BCONDITION, SMETHOD, STEXT ) same like 4) + additional location of error
60 // 5) LOG_ERROR( SMETHOD, STEXT ) show errors without any condition
62 // active for debug only!
64 // 6) LOG_EXCEPTION( SMETHOD, SOWNMESSAGE, SEXCEPTIONMESSAGE ) show/log an exception for easier debug
65 // 7) LOG_WARNING( SMETHOD, STEXT ) should be used to detect leaks in algorithm, mechanism or operation handling
66 //*****************************************************************************************************************
68 //_________________________________________________________________________________________________________________
69 #if defined( ENABLE_ASSERTIONS ) || defined( ENABLE_WARNINGS )
71 /*_____________________________________________________________________________________________________________
72 LOGFILE_ASSERTIONS
74 For follow macros we need a special log file. If user forget to specify anyone, we must do it for him!
75 _____________________________________________________________________________________________________________*/
77 #ifndef LOGFILE_ASSERTIONS
78 #define LOGFILE_ASSERTIONS "_framework_assertions.log"
79 #endif
81 /*_____________________________________________________________________________________________________________
82 LOG_ASSERT ( BCONDITION, STEXT )
83 LOG_ASSERT2( BCONDITION, SMETHOD, STEXT )
85 Forward assertion to logfile (if condition is FALSE - like a DBG_ASSERT!) and continue with program.
86 Set LOGTYPE to LOGTYPE_FILECONTINUE to do this.
87 BCONDITION is inserted in "(...)" because user can call this macro with an complex expression!
88 _____________________________________________________________________________________________________________*/
89 #if LOGTYPE==LOGTYPE_FILECONTINUE
91 #define LOG_ASSERT( BCONDITION, STEXT ) \
92 if ( ( BCONDITION ) == sal_False ) \
93 { \
94 WRITE_LOGFILE( LOGFILE_ASSERTIONS, STEXT ) \
97 #define LOG_ASSERT2( BCONDITION, SMETHOD, STEXT ) \
98 if ( ( BCONDITION ) == sal_True ) \
99 { \
100 ::rtl::OStringBuffer _sAssertBuffer( 256 ); \
101 _sAssertBuffer.append( "ASSERT:\n\t" ); \
102 _sAssertBuffer.append( SMETHOD ); \
103 _sAssertBuffer.append( "\n\t\"" ); \
104 _sAssertBuffer.append( STEXT ); \
105 _sAssertBuffer.append( "\"\n" ); \
106 WRITE_LOGFILE( LOGFILE_ASSERTIONS, _sAssertBuffer.makeStringAndClear() ) \
109 #endif
111 /*_____________________________________________________________________________________________________________
112 LOG_ASSERT ( BCONDITION, STEXT )
113 LOG_ASSERT2( BCONDITION, SMETHOD, STEXT )
115 Forward assertion to file and exit the program.
116 Set LOGTYPE to LOGTYPE_FILEEXIT to do this.
117 BCONDITION is inserted in "(...)" because user can call this macro with an complex expression!
118 _____________________________________________________________________________________________________________*/
119 #if LOGTYPE==LOGTYPE_FILEEXIT
121 #define LOG_ASSERT( BCONDITION, STEXT ) \
122 if ( ( BCONDITION ) == sal_False ) \
124 WRITE_LOGFILE( LOGFILE_ASSERTIONS, STEXT ) \
125 exit(-1); \
128 #define LOG_ASSERT2( BCONDITION, SMETHODE, STEXT ) \
129 if ( ( BCONDITION ) == sal_True ) \
131 ::rtl::OStringBuffer _sAssertBuffer( 256 ); \
132 _sAssertBuffer.append( "ASSERT:\n\t" ); \
133 _sAssertBuffer.append( SMETHOD ); \
134 _sAssertBuffer.append( "\n\t\"" ); \
135 _sAssertBuffer.append( STEXT ); \
136 _sAssertBuffer.append( "\"\n" ); \
137 WRITE_LOGFILE( LOGFILE_ASSERTIONS, _sAssertBuffer.makeStringAndClear() ) \
138 exit(-1); \
141 #endif
143 /*_____________________________________________________________________________________________________________
144 LOG_ASSERT ( BCONDITION, STEXT )
145 LOG_ASSERT2( BCONDITION, SMETHOD, STEXT )
147 Forward assertions to messagebox. (We use OSL_ENSURE to do this.)
148 Set LOGTYPE to LOGTYPE_MESSAGEBOX to do this.
149 BCONDITION is inserted in "(...)" because user can call this macro with an complex expression!
150 _____________________________________________________________________________________________________________*/
151 #if LOGTYPE==LOGTYPE_MESSAGEBOX
153 #define LOG_ASSERT( BCONDITION, STEXT ) \
154 OSL_ENSURE( ( BCONDITION ), STEXT );
156 #define LOG_ASSERT2( BCONDITION, SMETHOD, STEXT ) \
158 ::rtl::OStringBuffer _sAssertBuffer( 256 ); \
159 _sAssertBuffer.append( "ASSERT:\n\t" ); \
160 _sAssertBuffer.append( SMETHOD ); \
161 _sAssertBuffer.append( "\n\t\"" ); \
162 _sAssertBuffer.append( STEXT ); \
163 _sAssertBuffer.append( "\"\n" ); \
164 OSL_ENSURE( !( BCONDITION ), _sAssertBuffer.makeStringAndClear() ); \
167 #endif
169 /*_____________________________________________________________________________________________________________
170 LOG_ERROR( SMETHOD, STEXT )
172 Show an error by using current set output mode by define LOGTYPE!
173 _____________________________________________________________________________________________________________*/
175 #define LOG_ERROR( SMETHOD, STEXT ) \
176 LOG_ASSERT2( sal_True, SMETHOD, STEXT )
178 #else
180 // If right testmode is'nt set - implements these macros empty!
181 #undef LOGFILE_ASSERTIONS
182 #define LOG_ASSERT( BCONDITION, STEXT )
183 #define LOG_ASSERT2( BCONDITION, SMETHOD, STEXT )
184 #define LOG_ERROR( SMETHOD, STEXT )
186 #endif // ENABLE_ASSERTIONS
188 //_________________________________________________________________________________________________________________
189 #if defined( ENABLE_WARNINGS )
191 /*_____________________________________________________________________________________________________________
192 LOGFILE_WARNINGS
194 For follow macros we need a special log file. If user forget to specify anyone, we must do it for him!
195 _____________________________________________________________________________________________________________*/
197 #ifndef LOGFILE_WARNINGS
198 #define LOGFILE_WARNINGS "_framework_warnings.log"
199 #endif
201 /*_____________________________________________________________________________________________________________
202 LOG_EXCEPTION( SMETHOD, SOWNMESSAGE, SEXCEPTIONMESSAGE )
204 Show some exception info by using current set output mode by define LOGTYPE!
205 We use a seperated scope {} do protect us against multiple variable definitions.
206 _____________________________________________________________________________________________________________*/
208 #define LOG_EXCEPTION( SMETHOD, SOWNMESSAGE, SEXCEPTIONMESSAGE ) \
210 ::rtl::OStringBuffer _sAssertBuffer2( 256 ); \
211 _sAssertBuffer2.append( SOWNMESSAGE ); \
212 _sAssertBuffer2.append( "\n" ); \
213 _sAssertBuffer2.append( U2B(SEXCEPTIONMESSAGE) ); \
214 LOG_ERROR( SMETHOD, _sAssertBuffer2.makeStringAndClear() ) \
217 /*_____________________________________________________________________________________________________________
218 LOG_WARNING( SMETHOD, STEXT )
220 Use it to show/log warnings for programmer for follow reasons:
221 - algorithm errors
222 - undefined states
223 - unknown errors from other modules ...
224 _____________________________________________________________________________________________________________*/
226 #define LOG_WARNING( SMETHOD, STEXT ) \
227 LOG_ERROR( SMETHOD, STEXT )
229 #else
231 // If right testmode is'nt set - implements these macros empty!
232 #undef LOGFILE_WARNINGS
233 #define LOG_EXCEPTION( SMETHOD, SOWNMESSAGE, SEXCEPTIONMESSAGE )
234 #define LOG_WARNING( SMETHOD, STEXT )
236 #endif // ENABLE_WARNINGS
238 #endif // #ifndef __FRAMEWORK_MACROS_DEBUG_ASSERTION_HXX_