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