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