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