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