Back to index

lightning-sunbird  0.9+nobinonly
MacErrorHandling.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape Portable Runtime (NSPR).
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*********************************************************************
00038 
00039 FILENAME
00040        Exceptions.h
00041        
00042 DESCRIPTION
00043        A collection of routines and macros to handle assertions and
00044        exceptions.
00045 
00046 COPYRIGHT
00047        Copyright  Apple Computer, Inc. 1989-1991
00048        All rights reserved.
00049 
00050 ROUTINES
00051        EXTERNALS
00052               dprintf
00053               check_dprintf
00054               checkpos_dprintf
00055 
00056 MACROS
00057        EXTERNALS
00058               check
00059               ncheck
00060               check_action
00061               ncheck_action
00062               require
00063               nrequire
00064               require_action
00065               nrequire_action
00066               resume
00067 
00068 MODIFICATION HISTORY
00069        Nov 12 95            BKJ           Moved to MetroWerks environment & the NSPR
00070               
00071 NOTE
00072        To keep code size down, use these routines and macros with the C
00073        compiler option -b2 or -b3. This will eliminate duplicate strings
00074        within a procedure.
00075 
00076 *********************************************************************/
00077 
00078 #ifndef __MACERRORHANDLING__
00079 #define __MACERRORHANDLING__
00080 
00081 /*********************************************************************
00082 
00083 INCLUDES
00084 
00085 *********************************************************************/
00086 
00087 #include      <Types.h>
00088 
00089 /*<FF>*/
00090 /*********************************************************************
00091 
00092 CONSTANTS AND CONTROL
00093 
00094 *********************************************************************/
00095 
00096 /*
00097        These defines are used to control the amount of information
00098        displayed when an assertion fails. DEBUGOFF and WARN will run
00099        silently. MIN will simply break into the debugger. ON will break
00100        and display the assertion that failed and the exception (for
00101        require statements). FULL will also display the source file name
00102        and line number. SYM does a SysBreak and is usefull when using a
00103        symbolic debugger like SourceBug or SADE. They should be set into
00104        DEBUGLEVEL. The default LEVEL is OFF.
00105 */
00106 
00107 #define DEBUGOFF            0
00108 #define DEBUGWARN           1
00109 #define DEBUGMIN            2
00110 #define DEBUGON                    3
00111 #define DEBUGFULL           4
00112 #define DEBUGSYM            6
00113 
00114 #ifndef       DEBUGLEVEL
00115 #define       DEBUGLEVEL    DEBUGOFF
00116 #endif DEBUGLEVEL
00117 
00118 /*
00119        resumeLabel is used to control the insertion of labels for use with
00120        the resume macro. If you do not use the resume macro and you wish
00121        to have multible exceptions per label then you can add the
00122        following define to you source code.
00123        
00124 */
00125 #define resumeLabel(exception)                                                                           // Multiple exceptions per label
00126 // #define resumeLabel(exception)  resume_ ## exception:                            // Single exception per label
00127 
00128 
00129 /*
00130        traceon and debugon are used to test for options
00131 */
00132 
00133 #define       traceon       ((DEBUGLEVEL > DEBUGWARN) && defined(TRACEON))
00134 #define       debugon       (DEBUGLEVEL > DEBUGWARN)
00135 
00136 /*
00137        Add some macros for DEBUGMIN and DEBUGSYM to keep the size down.
00138 */
00139 
00140 #define       __DEBUGSMALL  ((DEBUGLEVEL == DEBUGMIN) ||                     \
00141                                                          (DEBUGLEVEL == DEBUGSYM))
00142 
00143 #if    DEBUGLEVEL == DEBUGMIN
00144 #define       __DebuggerBreak      Debugger()
00145 #elif  DEBUGLEVEL == DEBUGSYM
00146 #define  __DebuggerBreak    SysBreak()
00147 #endif
00148 
00149 
00150 /*<FF>*/
00151 /*********************************************************************
00152 
00153 MACRO
00154        check(assertion)
00155 
00156 DESCRIPTION
00157        If debugging is on then check will test assertion and if it fails
00158        break into the debugger. Otherwise check does nothing.
00159 
00160 *********************************************************************/
00161 
00162 #if    __DEBUGSMALL
00163 
00164 #define check(assertion)                                                                                                      \
00165        do {                                                                                                                                 \
00166               if (assertion) ;                                                                                                       \
00167               else __DebuggerBreak;                                                                                                  \
00168        } while (false)
00169 
00170 #elif  DEBUGLEVEL == DEBUGON
00171 
00172 #define check(assertion)                                                                                                      \
00173        do {                                                                                                                                 \
00174               if (assertion) ;                                                                                                       \
00175               else {                                                                                                                        \
00176                      dprintf(notrace, "Assertion \"%s\" Failed",      #assertion);                       \
00177               }                                                                                                                                    \
00178        } while (false)
00179 
00180 #elif  DEBUGLEVEL == DEBUGFULL
00181 
00182 #define check(assertion)                                                                                                      \
00183        do {                                                                                                                                 \
00184               if (assertion) ;                                                                                                       \
00185               else {                                                                                                                        \
00186                      dprintf(notrace,     "Assertion \"%s\" Failed\n"                                           \
00187                                                                "File: %s\n"                                                          \
00188                                                                "Line: %d",                                                                  \
00189                             #assertion, __FILE__, __LINE__);                                                           \
00190               }                                                                                                                                    \
00191        } while (false)
00192        
00193 #else
00194 
00195 #define check(assertion)
00196 
00197 #endif
00198 
00199 /*<FF>*/
00200 /*********************************************************************
00201 
00202 MACRO
00203        ncheck(assertion)
00204 
00205 DESCRIPTION
00206        If debugging is on then ncheck will test !assertion and if it fails
00207        break into the debugger. Otherwise ncheck does nothing.
00208 
00209 *********************************************************************/
00210 
00211 #if    __DEBUGSMALL
00212 
00213 #define ncheck(assertion)                                                                                       \
00214        do {                                                                                                                   \
00215               if (assertion) __DebuggerBreak;                                                                   \
00216        } while (false)
00217 
00218 #elif  DEBUGLEVEL == DEBUGON
00219 
00220 #define ncheck(assertion)                                                                                       \
00221        do {                                                                                                                   \
00222               void*  __privateAssertion   = (void*)(assertion);                                   \
00223                                                                                                                                      \
00224               if (__privateAssertion) {                                                                         \
00225                      dprintf(notrace, "Assertion \"!(%s [= %#08X])\" Failed",              \
00226                             #assertion, __privateAssertion);                                             \
00227               }                                                                                                                      \
00228        } while (false)
00229 
00230 #elif  DEBUGLEVEL == DEBUGFULL
00231 
00232 #define ncheck(assertion)                                                                                       \
00233        do {                                                                                                                   \
00234               void*  __privateAssertion   = (void*)(assertion);                                   \
00235                                                                                                                                      \
00236               if (__privateAssertion) {                                                                         \
00237                      dprintf(notrace,     "Assertion \"!(%s [= %#08X])\" Failed\n"  \
00238                                                                "File: %s\n"                                            \
00239                                                                "Line: %d",                                                    \
00240                      #assertion, __privateAssertion, __FILE__, __LINE__);                  \
00241               }                                                                                                                      \
00242        } while (false)
00243 
00244 #else
00245 
00246 #define ncheck(assertion)
00247 
00248 #endif
00249 
00250 /*<FF>*/
00251 /*********************************************************************
00252 
00253 MACRO
00254        check_action(assertion, action)
00255 
00256 DESCRIPTION
00257        If debugging is on then check_action will test assertion and if it
00258        fails break into the debugger then execute action. Otherwise
00259        check_action does nothing.
00260        
00261 *********************************************************************/
00262 
00263 #if    __DEBUGSMALL
00264 
00265 #define check_action(assertion, action)                                                                  \
00266        do {                                                                                                                   \
00267               if (assertion) ;                                                                                         \
00268               else {                                                                                                          \
00269                      __DebuggerBreak;                                                                                  \
00270                      { action }                                                                                               \
00271        } while (false)
00272 
00273 #elif  DEBUGLEVEL == DEBUGON
00274 
00275 #define check_action(assertion, action)                                                                  \
00276        do {                                                                                                                   \
00277               if (assertion) ;                                                                                         \
00278               else {                                                                                                          \
00279                      dprintf(notrace, "Assertion \"%s\" Failed",      #assertion);         \
00280                      { action }                                                                                               \
00281               }                                                                                                                      \
00282        } while (false)
00283 
00284 #elif  DEBUGLEVEL == DEBUGFULL
00285 
00286 #define check_action(assertion, action)                                                                  \
00287        do {                                                                                                                   \
00288               if (assertion) ;                                                                                         \
00289               else {                                                                                                          \
00290                      dprintf(notrace,     "Assertion \"%s\" Failed\n"                             \
00291                                                                "File: %s\n"                                            \
00292                                                                "Line: %d",                                                    \
00293                             #assertion, __FILE__, __LINE__);                                             \
00294                      { action }                                                                                               \
00295               }                                                                                                                      \
00296        } while (false)
00297 
00298 #else
00299 
00300 #define check_action(assertion, action)
00301 
00302 #endif
00303 
00304 /*<FF>*/
00305 /**************************************************************************************
00306 
00307 MACRO
00308        ncheck_action(assertion, action)
00309 
00310 DESCRIPTION
00311        If debugging is on then ncheck_action will test !assertion and if
00312        it fails break into the debugger then execute action. Otherwise
00313        ncheck_action does nothing.
00314 
00315 *********************************************************************/
00316 
00317 #if    __DEBUGSMALL
00318 
00319 #define ncheck_action(assertion, action)                                                          \
00320        do {                                                                                                                   \
00321               if (assertion) {                                                                                         \
00322                      __DebuggerBreak;                                                                                  \
00323                      { action }                                                                                               \
00324               }                                                                                                                      \
00325        } while (false)
00326 
00327 #elif  DEBUGLEVEL == DEBUGON
00328 
00329 #define ncheck_action(assertion, action)                                                          \
00330        do {                                                                                                                   \
00331               void*  __privateAssertion   = (void*)(assertion);                                   \
00332                                                                                                                                      \
00333               if (__privateAssertion) {                                                                         \
00334                      dprintf(notrace, "Assertion \"!(%s [= %#08X])\" Failed",              \
00335                             #assertion, __privateAssertion);                                             \
00336                      { action }                                                                                               \
00337               }                                                                                                                      \
00338        } while (false)
00339 
00340 #elif DEBUGLEVEL == DEBUGFULL
00341 
00342 #define ncheck_action(assertion, action)                                                          \
00343        do {                                                                                                                   \
00344               void*  __privateAssertion   = (void*)(assertion);                                   \
00345                                                                                                                                      \
00346               if (__privateAssertion) {                                                                         \
00347                      dprintf(notrace,     "Assertion \"!(%s [= %#08X])\" Failed\n"  \
00348                                                                "File: %s\n"                                            \
00349                                                                "Line: %d",                                                    \
00350                      #assertion, __privateAssertion, __FILE__, __LINE__);                  \
00351                      { action }                                                                                               \
00352               }                                                                                                                      \
00353        } while (false)
00354 
00355 #else
00356 
00357 #define ncheck_action(assertion, action)
00358 
00359 #endif
00360 
00361 /*<FF>*/
00362 /*********************************************************************
00363 
00364 MACRO
00365        require(assertion, exception)
00366 
00367 DESCRIPTION
00368        require will test assertion and if it fails:
00369               break into the debugger if debugging is on.
00370               goto exception.
00371 
00372 *********************************************************************/
00373 
00374 #if    __DEBUGSMALL
00375 
00376 #define require(assertion, exception)                                                                    \
00377        do {                                                                                                                   \
00378               if (assertion) ;                                                                                         \
00379               else {                                                                                                          \
00380                      __DebuggerBreak;                                                                                  \
00381                      goto exception;                                                                                          \
00382                      resumeLabel(exception);                                                                           \
00383               }                                                                                                                      \
00384        } while (false)
00385 
00386 #elif  DEBUGLEVEL == DEBUGON
00387 
00388 #define require(assertion, exception)                                                                    \
00389        do {                                                                                                                   \
00390               if (assertion) ;                                                                                         \
00391               else {                                                                                                          \
00392                      dprintf(notrace,     "Assertion \"%s\" Failed\n"                             \
00393                                                                "Exception \"%s\" Raised",                       \
00394                      #assertion, #exception);                                                                   \
00395                      goto exception;                                                                                          \
00396                      resumeLabel(exception);                                                                           \
00397               }                                                                                                                      \
00398        } while (false)
00399 
00400 #elif DEBUGLEVEL == DEBUGFULL
00401 
00402 #define require(assertion, exception)                                                                    \
00403        do {                                                                                                                   \
00404               if (assertion) ;                                                                                         \
00405               else {                                                                                                          \
00406                      dprintf(notrace,     "Assertion \"%s\" Failed\n"                             \
00407                                                                "Exception \"%s\" Raised\n"                      \
00408                                                                "File: %s\n"                                            \
00409                                                                "Line: %d",                                                    \
00410                             #assertion, #exception, __FILE__, __LINE__);                          \
00411                      goto exception;                                                                                          \
00412                      resumeLabel(exception);                                                                           \
00413               }                                                                                                                      \
00414        } while (false)
00415 
00416 #else
00417 
00418 #define require(assertion, exception)                                                                    \
00419        do {                                                                                                                   \
00420               if (assertion) ;                                                                                         \
00421               else {                                                                                                          \
00422                      goto exception;                                                                                          \
00423                      resumeLabel(exception);                                                                           \
00424               }                                                                                                                      \
00425        } while (false)
00426 
00427 #endif
00428 
00429 /*<FF>*/
00430 /*********************************************************************
00431 
00432 MACRO
00433        nrequire(assertion, exception)
00434 
00435 DESCRIPTION
00436        nrequire will test !assertion and if it fails:
00437               break into the debugger if debugging is on.
00438               goto exception.
00439 
00440 *********************************************************************/
00441 
00442 #if    __DEBUGSMALL
00443 
00444 #define nrequire(assertion, exception)                                                                   \
00445        do {                                                                                                                   \
00446               if (assertion) {                                                                                         \
00447                      DebugStr();                                                                                              \
00448                      goto exception;                                                                                          \
00449                      resumeLabel(exception);                                                                           \
00450               }                                                                                                                      \
00451        } while (false)
00452 
00453 #elif  DEBUGLEVEL == DEBUGON
00454 
00455 #define nrequire(assertion, exception)                                                                   \
00456        do {                                                                                                                   \
00457               void*  __privateAssertion   = (void*)(assertion);                                   \
00458                                                                                                                                      \
00459               if (__privateAssertion) {                                                                         \
00460                      dprintf(notrace,     "Assertion \"!(%s [= %#08X])\" Failed\n"  \
00461                                                                "Exception \"%s\" Raised",                       \
00462                             #assertion, __privateAssertion, #exception);                          \
00463                      goto exception;                                                                                          \
00464                      resumeLabel(exception);                                                                           \
00465               }                                                                                                                      \
00466        } while (false)
00467 
00468 #elif DEBUGLEVEL == DEBUGFULL
00469 
00470 #define nrequire(assertion, exception)                                                                   \
00471        do {                                                                                                                   \
00472               void*  __privateAssertion   = (void*)(assertion);                                   \
00473                                                                                                                                      \
00474               if (__privateAssertion) {                                                                         \
00475                      dprintf(notrace,     "Assertion \"!(%s [= %#08X])\" Failed\n"  \
00476                                                                "Exception \"%s\" Raised\n"                      \
00477                                                                "File: %s\n"                                            \
00478                                                                "Line: %d",                                                    \
00479                             #assertion, __privateAssertion, #exception, __FILE__,          \
00480                             __LINE__);                                                                                        \
00481                      goto exception;                                                                                          \
00482                      resumeLabel(exception);                                                                           \
00483               }                                                                                                                      \
00484        } while (false)
00485 
00486 #else
00487 
00488 #define nrequire(assertion, exception)                                                                   \
00489        do {                                                                                                                   \
00490               if (assertion) {                                                                                         \
00491                      goto exception;                                                                                          \
00492                      resumeLabel(exception);                                                                           \
00493               }                                                                                                                      \
00494        } while (false)
00495 
00496 #endif
00497 
00498 /*<FF>*/
00499 /*********************************************************************
00500 
00501 MACRO
00502        require_action(assertion, exception, action)
00503 
00504 DESCRIPTION
00505        require_action will test assertion and if it fails:
00506               break into the debugger if debugging is on.
00507               execute action.
00508               goto exception.
00509 
00510 *********************************************************************/
00511 
00512 #if    __DEBUGSMALL
00513 
00514 #define require_action(assertion, exception, action)                                       \
00515        do {                                                                                                                   \
00516               if (assertion) ;                                                                                         \
00517               else {                                                                                                          \
00518                      __DebuggerBreak;                                                                                  \
00519                      { action }                                                                                               \
00520                      goto exception;                                                                                          \
00521                      resumeLabel(exception);                                                                           \
00522               }                                                                                                                      \
00523        } while (false)
00524 
00525 #elif  DEBUGLEVEL == DEBUGON
00526 
00527 #define require_action(assertion, exception, action)                                       \
00528        do {                                                                                                                   \
00529               if (assertion) ;                                                                                         \
00530               else {                                                                                                          \
00531                      dprintf(notrace,     "Assertion \"%s\" Failed\n"                             \
00532                                                                "Exception \"%s\" Raised",                       \
00533                      #assertion, #exception);                                                                   \
00534                      { action }                                                                                               \
00535                      goto exception;                                                                                          \
00536                      resumeLabel(exception);                                                                           \
00537               }                                                                                                                      \
00538        } while (false)
00539 
00540 #elif DEBUGLEVEL == DEBUGFULL
00541 
00542 #define require_action(assertion, exception, action)                                       \
00543        do {                                                                                                                   \
00544               if (assertion) ;                                                                                         \
00545               else {                                                                                                          \
00546                      dprintf(notrace,     "Assertion \"%s\" Failed\n"                             \
00547                                                                "Exception \"%s\" Raised\n"                      \
00548                                                                "File: %s\n"                                            \
00549                                                                "Line: %d",                                                    \
00550                             #assertion, #exception, __FILE__, __LINE__);                          \
00551                      { action }                                                                                               \
00552                      goto exception;                                                                                          \
00553                      resumeLabel(exception);                                                                           \
00554               }                                                                                                                      \
00555        } while (false)
00556 
00557 #else
00558 
00559 #define require_action(assertion, exception, action)                                       \
00560        do {                                                                                                                   \
00561               if (assertion) ;                                                                                         \
00562               else {                                                                                                          \
00563                      { action }                                                                                               \
00564                      goto exception;                                                                                          \
00565                      resumeLabel(exception);                                                                           \
00566               }                                                                                                                      \
00567        } while (false)
00568 
00569 #endif
00570 
00571 /*<FF>*/
00572 /*********************************************************************
00573 
00574 MACRO
00575        nrequire_action(assertion, exception, action)
00576 
00577 DESCRIPTION
00578        nrequire_action will test !assertion and if it fails:
00579               break into the debugger if debugging is on.
00580               execute action.
00581               goto exception.
00582 
00583 *********************************************************************/
00584 
00585 #if    __DEBUGSMALL
00586 
00587 #define nrequire_action(assertion, exception, action)                                      \
00588        do {                                                                                                                   \
00589               if (assertion) {                                                                                         \
00590                      __DebuggerBreak;                                                                                  \
00591                      { action }                                                                                               \
00592                      goto exception;                                                                                          \
00593                      resumeLabel(exception);                                                                           \
00594               }                                                                                                                      \
00595        } while (false)
00596 
00597 #elif DEBUGLEVEL == DEBUGON
00598 
00599 #define nrequire_action(assertion, exception, action)                                      \
00600        do {                                                                                                                   \
00601               void*  __privateAssertion   = (void*)(assertion);                                   \
00602                                                                                                                                      \
00603               if (__privateAssertion) {                                                                         \
00604                      dprintf(notrace,     "Assertion \"!(%s [= %#08X])\" Failed\n"  \
00605                                                                "Exception \"%s\" Raised",                       \
00606                             #assertion, __privateAssertion, #exception);                          \
00607                      { action }                                                                                               \
00608                      goto exception;                                                                                          \
00609                      resumeLabel(exception);                                                                           \
00610               }                                                                                                                      \
00611        } while (false)
00612 
00613 #elif DEBUGLEVEL == DEBUGFULL
00614 
00615 #define nrequire_action(assertion, exception, action)                                      \
00616        do {                                                                                                                   \
00617               void*  __privateAssertion   = (void*)(assertion);                                   \
00618                                                                                                                                      \
00619               if (__privateAssertion) {                                                                         \
00620                      dprintf(notrace,     "Assertion \"!(%s [= %#08X])\" Failed\n"  \
00621                                                                "Exception \"%s\" Raised\n"                      \
00622                                                                "File: %s\n"                                            \
00623                                                                "Line: %d",                                                    \
00624                             #assertion, __privateAssertion, #exception, __FILE__,          \
00625                             __LINE__);                                                                                        \
00626                      { action }                                                                                               \
00627                      goto exception;                                                                                          \
00628                      resumeLabel(exception);                                                                           \
00629               }                                                                                                                      \
00630        } while (false)
00631 
00632 #else
00633 
00634 #define nrequire_action(assertion, exception, action)                                      \
00635        do {                                                                                                                   \
00636               if (assertion) {                                                                                         \
00637                      { action }                                                                                               \
00638                      goto exception;                                                                                          \
00639                      resumeLabel(exception);                                                                           \
00640               }                                                                                                                      \
00641        } while (false)
00642 
00643 #endif
00644        
00645 /*<FF>*/
00646 /*********************************************************************
00647 
00648 MACRO
00649        resume(exception)
00650 
00651 DESCRIPTION
00652        resume will resume execution after the n/require/_action statement
00653        specified by exception. Resume lables must be on (the default) in
00654        order to use resume. If an action form of require was used then the
00655        action will not be re-executed.
00656 
00657 *********************************************************************/
00658 
00659 
00660 #define resume(exception)                                                                                       \
00661        do {                                                                                                                   \
00662               goto resume_ ## exception;                                                                        \
00663        } while (false)
00664 
00665 
00666 /*<FF>*/
00667 /********************************************************************/
00668 #endif