home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / md / mac / MacErrorHandling.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  16.7 KB  |  634 lines

  1.  
  2. /*********************************************************************
  3.  
  4. FILENAME
  5.     Exceptions.h
  6.     
  7. DESCRIPTION
  8.     A collection of routines and macros to handle assertions and
  9.     exceptions.
  10.  
  11. COPYRIGHT
  12.     Copyright ⌐ Apple Computer, Inc. 1989-1991
  13.     All rights reserved.
  14.  
  15. ROUTINES
  16.     EXTERNALS
  17.         dprintf
  18.         check_dprintf
  19.         checkpos_dprintf
  20.  
  21. MACROS
  22.     EXTERNALS
  23.         check
  24.         ncheck
  25.         check_action
  26.         ncheck_action
  27.         require
  28.         nrequire
  29.         require_action
  30.         nrequire_action
  31.         resume
  32.  
  33. MODIFICATION HISTORY
  34.     Nov 12 95        BKJ        Moved to MetroWerks environment & the NSPR
  35.         
  36. NOTE
  37.     To keep code size down, use these routines and macros with the C
  38.     compiler option -b2 or -b3. This will eliminate duplicate strings
  39.     within a procedure.
  40.  
  41. *********************************************************************/
  42.  
  43. #ifndef __MACERRORHANDLING__
  44. #define __MACERRORHANDLING__
  45.  
  46. /*********************************************************************
  47.  
  48. INCLUDES
  49.  
  50. *********************************************************************/
  51.  
  52. #include    <Types.h>
  53.  
  54. /*<FF>*/
  55. /*********************************************************************
  56.  
  57. CONSTANTS AND CONTROL
  58.  
  59. *********************************************************************/
  60.  
  61. /*
  62.     These defines are used to control the amount of information
  63.     displayed when an assertion fails. DEBUGOFF and WARN will run
  64.     silently. MIN will simply break into the debugger. ON will break
  65.     and display the assertion that failed and the exception (for
  66.     require statements). FULL will also display the source file name
  67.     and line number. SYM does a SysBreak and is usefull when using a
  68.     symbolic debugger like SourceBug or SADE. They should be set into
  69.     DEBUGLEVEL. The default LEVEL is OFF.
  70. */
  71.  
  72. #define DEBUGOFF        0
  73. #define DEBUGWARN        1
  74. #define DEBUGMIN        2
  75. #define DEBUGON            3
  76. #define DEBUGFULL        4
  77. #define DEBUGSYM        6
  78.  
  79. #ifndef    DEBUGLEVEL
  80. #define    DEBUGLEVEL    DEBUGOFF
  81. #endif    DEBUGLEVEL
  82.  
  83. /*
  84.     resumeLabel is used to control the insertion of labels for use with
  85.     the resume macro. If you do not use the resume macro and you wish
  86.     to have multible exceptions per label then you can add the
  87.     following define to you source code.
  88.     
  89. */
  90. #define resumeLabel(exception)                                            // Multiple exceptions per label
  91. // #define resumeLabel(exception)    resume_ ## exception:                // Single exception per label
  92.  
  93.  
  94. /*
  95.     traceon and debugon are used to test for options
  96. */
  97.  
  98. #define    traceon    ((DEBUGLEVEL > DEBUGWARN) && defined(TRACEON))
  99. #define    debugon    (DEBUGLEVEL > DEBUGWARN)
  100.  
  101. /*
  102.     Add some macros for DEBUGMIN and DEBUGSYM to keep the size down.
  103. */
  104.  
  105. #define    __DEBUGSMALL    ((DEBUGLEVEL == DEBUGMIN) ||            \
  106.                                  (DEBUGLEVEL == DEBUGSYM))
  107.  
  108. #if    DEBUGLEVEL == DEBUGMIN
  109. #define    __DebuggerBreak    Debugger()
  110. #elif    DEBUGLEVEL == DEBUGSYM
  111. #define  __DebuggerBreak    SysBreak()
  112. #endif
  113.  
  114.  
  115. /*<FF>*/
  116. /*********************************************************************
  117.  
  118. MACRO
  119.     check(assertion)
  120.  
  121. DESCRIPTION
  122.     If debugging is on then check will test assertion and if it fails
  123.     break into the debugger. Otherwise check does nothing.
  124.  
  125. *********************************************************************/
  126.  
  127. #if    __DEBUGSMALL
  128.  
  129. #define check(assertion)                                                            \
  130.     do {                                                                            \
  131.         if (assertion) ;                                                            \
  132.         else __DebuggerBreak;                                                        \
  133.     } while (false)
  134.  
  135. #elif    DEBUGLEVEL == DEBUGON
  136.  
  137. #define check(assertion)                                                            \
  138.     do {                                                                            \
  139.         if (assertion) ;                                                            \
  140.         else {                                                                        \
  141.             dprintf(notrace, "Assertion \"%s\" Failed",    #assertion);                \
  142.         }                                                                            \
  143.     } while (false)
  144.  
  145. #elif    DEBUGLEVEL == DEBUGFULL
  146.  
  147. #define check(assertion)                                                            \
  148.     do {                                                                            \
  149.         if (assertion) ;                                                            \
  150.         else {                                                                        \
  151.             dprintf(notrace,    "Assertion \"%s\" Failed\n"                            \
  152.                                     "File: %s\n"                                    \
  153.                                     "Line: %d",                                        \
  154.                 #assertion, __FILE__, __LINE__);                                    \
  155.         }                                                                            \
  156.     } while (false)
  157.     
  158. #else
  159.  
  160. #define check(assertion)
  161.  
  162. #endif
  163.  
  164. /*<FF>*/
  165. /*********************************************************************
  166.  
  167. MACRO
  168.     ncheck(assertion)
  169.  
  170. DESCRIPTION
  171.     If debugging is on then ncheck will test !assertion and if it fails
  172.     break into the debugger. Otherwise ncheck does nothing.
  173.  
  174. *********************************************************************/
  175.  
  176. #if    __DEBUGSMALL
  177.  
  178. #define ncheck(assertion)                                                    \
  179.     do {                                                                    \
  180.         if (assertion) __DebuggerBreak;                                        \
  181.     } while (false)
  182.  
  183. #elif    DEBUGLEVEL == DEBUGON
  184.  
  185. #define ncheck(assertion)                                                    \
  186.     do {                                                                    \
  187.         void*    __privateAssertion    = (void*)(assertion);                    \
  188.                                                                             \
  189.         if (__privateAssertion) {                                            \
  190.             dprintf(notrace, "Assertion \"!(%s [= %#08X])\" Failed",        \
  191.                 #assertion, __privateAssertion);                            \
  192.         }                                                                    \
  193.     } while (false)
  194.  
  195. #elif    DEBUGLEVEL == DEBUGFULL
  196.  
  197. #define ncheck(assertion)                                                    \
  198.     do {                                                                    \
  199.         void*    __privateAssertion    = (void*)(assertion);                    \
  200.                                                                             \
  201.         if (__privateAssertion) {                                            \
  202.             dprintf(notrace,    "Assertion \"!(%s [= %#08X])\" Failed\n"    \
  203.                                     "File: %s\n"                            \
  204.                                     "Line: %d",                                \
  205.             #assertion, __privateAssertion, __FILE__, __LINE__);            \
  206.         }                                                                    \
  207.     } while (false)
  208.  
  209. #else
  210.  
  211. #define ncheck(assertion)
  212.  
  213. #endif
  214.  
  215. /*<FF>*/
  216. /*********************************************************************
  217.  
  218. MACRO
  219.     check_action(assertion, action)
  220.  
  221. DESCRIPTION
  222.     If debugging is on then check_action will test assertion and if it
  223.     fails break into the debugger then execute action. Otherwise
  224.     check_action does nothing.
  225.     
  226. *********************************************************************/
  227.  
  228. #if    __DEBUGSMALL
  229.  
  230. #define check_action(assertion, action)                                        \
  231.     do {                                                                    \
  232.         if (assertion) ;                                                    \
  233.         else {                                                                \
  234.             __DebuggerBreak;                                                \
  235.             { action }                                                        \
  236.     } while (false)
  237.  
  238. #elif    DEBUGLEVEL == DEBUGON
  239.  
  240. #define check_action(assertion, action)                                        \
  241.     do {                                                                    \
  242.         if (assertion) ;                                                    \
  243.         else {                                                                \
  244.             dprintf(notrace, "Assertion \"%s\" Failed",    #assertion);        \
  245.             { action }                                                        \
  246.         }                                                                    \
  247.     } while (false)
  248.  
  249. #elif    DEBUGLEVEL == DEBUGFULL
  250.  
  251. #define check_action(assertion, action)                                        \
  252.     do {                                                                    \
  253.         if (assertion) ;                                                    \
  254.         else {                                                                \
  255.             dprintf(notrace,    "Assertion \"%s\" Failed\n"                    \
  256.                                     "File: %s\n"                            \
  257.                                     "Line: %d",                                \
  258.                 #assertion, __FILE__, __LINE__);                            \
  259.             { action }                                                        \
  260.         }                                                                    \
  261.     } while (false)
  262.  
  263. #else
  264.  
  265. #define check_action(assertion, action)
  266.  
  267. #endif
  268.  
  269. /*<FF>*/
  270. /**************************************************************************************
  271.  
  272. MACRO
  273.     ncheck_action(assertion, action)
  274.  
  275. DESCRIPTION
  276.     If debugging is on then ncheck_action will test !assertion and if
  277.     it fails break into the debugger then execute action. Otherwise
  278.     ncheck_action does nothing.
  279.  
  280. *********************************************************************/
  281.  
  282. #if    __DEBUGSMALL
  283.  
  284. #define ncheck_action(assertion, action)                                    \
  285.     do {                                                                    \
  286.         if (assertion) {                                                    \
  287.             __DebuggerBreak;                                                \
  288.             { action }                                                        \
  289.         }                                                                    \
  290.     } while (false)
  291.  
  292. #elif    DEBUGLEVEL == DEBUGON
  293.  
  294. #define ncheck_action(assertion, action)                                    \
  295.     do {                                                                    \
  296.         void*    __privateAssertion    = (void*)(assertion);                    \
  297.                                                                             \
  298.         if (__privateAssertion) {                                            \
  299.             dprintf(notrace, "Assertion \"!(%s [= %#08X])\" Failed",        \
  300.                 #assertion, __privateAssertion);                            \
  301.             { action }                                                        \
  302.         }                                                                    \
  303.     } while (false)
  304.  
  305. #elif DEBUGLEVEL == DEBUGFULL
  306.  
  307. #define ncheck_action(assertion, action)                                    \
  308.     do {                                                                    \
  309.         void*    __privateAssertion    = (void*)(assertion);                    \
  310.                                                                             \
  311.         if (__privateAssertion) {                                            \
  312.             dprintf(notrace,    "Assertion \"!(%s [= %#08X])\" Failed\n"    \
  313.                                     "File: %s\n"                            \
  314.                                     "Line: %d",                                \
  315.             #assertion, __privateAssertion, __FILE__, __LINE__);            \
  316.             { action }                                                        \
  317.         }                                                                    \
  318.     } while (false)
  319.  
  320. #else
  321.  
  322. #define ncheck_action(assertion, action)
  323.  
  324. #endif
  325.  
  326. /*<FF>*/
  327. /*********************************************************************
  328.  
  329. MACRO
  330.     require(assertion, exception)
  331.  
  332. DESCRIPTION
  333.     require will test assertion and if it fails:
  334.         break into the debugger if debugging is on.
  335.         goto exception.
  336.  
  337. *********************************************************************/
  338.  
  339. #if    __DEBUGSMALL
  340.  
  341. #define require(assertion, exception)                                        \
  342.     do {                                                                    \
  343.         if (assertion) ;                                                    \
  344.         else {                                                                \
  345.             __DebuggerBreak;                                                \
  346.             goto exception;                                                    \
  347.             resumeLabel(exception);                                            \
  348.         }                                                                    \
  349.     } while (false)
  350.  
  351. #elif    DEBUGLEVEL == DEBUGON
  352.  
  353. #define require(assertion, exception)                                        \
  354.     do {                                                                    \
  355.         if (assertion) ;                                                    \
  356.         else {                                                                \
  357.             dprintf(notrace,    "Assertion \"%s\" Failed\n"                    \
  358.                                     "Exception \"%s\" Raised",                \
  359.             #assertion, #exception);                                        \
  360.             goto exception;                                                    \
  361.             resumeLabel(exception);                                            \
  362.         }                                                                    \
  363.     } while (false)
  364.  
  365. #elif DEBUGLEVEL == DEBUGFULL
  366.  
  367. #define require(assertion, exception)                                        \
  368.     do {                                                                    \
  369.         if (assertion) ;                                                    \
  370.         else {                                                                \
  371.             dprintf(notrace,    "Assertion \"%s\" Failed\n"                    \
  372.                                     "Exception \"%s\" Raised\n"                \
  373.                                     "File: %s\n"                            \
  374.                                     "Line: %d",                                \
  375.                 #assertion, #exception, __FILE__, __LINE__);                \
  376.             goto exception;                                                    \
  377.             resumeLabel(exception);                                            \
  378.         }                                                                    \
  379.     } while (false)
  380.  
  381. #else
  382.  
  383. #define require(assertion, exception)                                        \
  384.     do {                                                                    \
  385.         if (assertion) ;                                                    \
  386.         else {                                                                \
  387.             goto exception;                                                    \
  388.             resumeLabel(exception);                                            \
  389.         }                                                                    \
  390.     } while (false)
  391.  
  392. #endif
  393.  
  394. /*<FF>*/
  395. /*********************************************************************
  396.  
  397. MACRO
  398.     nrequire(assertion, exception)
  399.  
  400. DESCRIPTION
  401.     nrequire will test !assertion and if it fails:
  402.         break into the debugger if debugging is on.
  403.         goto exception.
  404.  
  405. *********************************************************************/
  406.  
  407. #if    __DEBUGSMALL
  408.  
  409. #define nrequire(assertion, exception)                                        \
  410.     do {                                                                    \
  411.         if (assertion) {                                                    \
  412.             DebugStr();                                                        \
  413.             goto exception;                                                    \
  414.             resumeLabel(exception);                                            \
  415.         }                                                                    \
  416.     } while (false)
  417.  
  418. #elif    DEBUGLEVEL == DEBUGON
  419.  
  420. #define nrequire(assertion, exception)                                        \
  421.     do {                                                                    \
  422.         void*    __privateAssertion    = (void*)(assertion);                    \
  423.                                                                             \
  424.         if (__privateAssertion) {                                            \
  425.             dprintf(notrace,    "Assertion \"!(%s [= %#08X])\" Failed\n"    \
  426.                                     "Exception \"%s\" Raised",                \
  427.                 #assertion, __privateAssertion, #exception);                \
  428.             goto exception;                                                    \
  429.             resumeLabel(exception);                                            \
  430.         }                                                                    \
  431.     } while (false)
  432.  
  433. #elif DEBUGLEVEL == DEBUGFULL
  434.  
  435. #define nrequire(assertion, exception)                                        \
  436.     do {                                                                    \
  437.         void*    __privateAssertion    = (void*)(assertion);                    \
  438.                                                                             \
  439.         if (__privateAssertion) {                                            \
  440.             dprintf(notrace,    "Assertion \"!(%s [= %#08X])\" Failed\n"    \
  441.                                     "Exception \"%s\" Raised\n"                \
  442.                                     "File: %s\n"                            \
  443.                                     "Line: %d",                                \
  444.                 #assertion, __privateAssertion, #exception, __FILE__,        \
  445.                 __LINE__);                                                    \
  446.             goto exception;                                                    \
  447.             resumeLabel(exception);                                            \
  448.         }                                                                    \
  449.     } while (false)
  450.  
  451. #else
  452.  
  453. #define nrequire(assertion, exception)                                        \
  454.     do {                                                                    \
  455.         if (assertion) {                                                    \
  456.             goto exception;                                                    \
  457.             resumeLabel(exception);                                            \
  458.         }                                                                    \
  459.     } while (false)
  460.  
  461. #endif
  462.  
  463. /*<FF>*/
  464. /*********************************************************************
  465.  
  466. MACRO
  467.     require_action(assertion, exception, action)
  468.  
  469. DESCRIPTION
  470.     require_action will test assertion and if it fails:
  471.         break into the debugger if debugging is on.
  472.         execute action.
  473.         goto exception.
  474.  
  475. *********************************************************************/
  476.  
  477. #if    __DEBUGSMALL
  478.  
  479. #define require_action(assertion, exception, action)                        \
  480.     do {                                                                    \
  481.         if (assertion) ;                                                    \
  482.         else {                                                                \
  483.             __DebuggerBreak;                                                \
  484.             { action }                                                        \
  485.             goto exception;                                                    \
  486.             resumeLabel(exception);                                            \
  487.         }                                                                    \
  488.     } while (false)
  489.  
  490. #elif    DEBUGLEVEL == DEBUGON
  491.  
  492. #define require_action(assertion, exception, action)                        \
  493.     do {                                                                    \
  494.         if (assertion) ;                                                    \
  495.         else {                                                                \
  496.             dprintf(notrace,    "Assertion \"%s\" Failed\n"                    \
  497.                                     "Exception \"%s\" Raised",                \
  498.             #assertion, #exception);                                        \
  499.             { action }                                                        \
  500.             goto exception;                                                    \
  501.             resumeLabel(exception);                                            \
  502.         }                                                                    \
  503.     } while (false)
  504.  
  505. #elif DEBUGLEVEL == DEBUGFULL
  506.  
  507. #define require_action(assertion, exception, action)                        \
  508.     do {                                                                    \
  509.         if (assertion) ;                                                    \
  510.         else {                                                                \
  511.             dprintf(notrace,    "Assertion \"%s\" Failed\n"                    \
  512.                                     "Exception \"%s\" Raised\n"                \
  513.                                     "File: %s\n"                            \
  514.                                     "Line: %d",                                \
  515.                 #assertion, #exception, __FILE__, __LINE__);                \
  516.             { action }                                                        \
  517.             goto exception;                                                    \
  518.             resumeLabel(exception);                                            \
  519.         }                                                                    \
  520.     } while (false)
  521.  
  522. #else
  523.  
  524. #define require_action(assertion, exception, action)                        \
  525.     do {                                                                    \
  526.         if (assertion) ;                                                    \
  527.         else {                                                                \
  528.             { action }                                                        \
  529.             goto exception;                                                    \
  530.             resumeLabel(exception);                                            \
  531.         }                                                                    \
  532.     } while (false)
  533.  
  534. #endif
  535.  
  536. /*<FF>*/
  537. /*********************************************************************
  538.  
  539. MACRO
  540.     nrequire_action(assertion, exception, action)
  541.  
  542. DESCRIPTION
  543.     nrequire_action will test !assertion and if it fails:
  544.         break into the debugger if debugging is on.
  545.         execute action.
  546.         goto exception.
  547.  
  548. *********************************************************************/
  549.  
  550. #if    __DEBUGSMALL
  551.  
  552. #define nrequire_action(assertion, exception, action)                        \
  553.     do {                                                                    \
  554.         if (assertion) {                                                    \
  555.             __DebuggerBreak;                                                \
  556.             { action }                                                        \
  557.             goto exception;                                                    \
  558.             resumeLabel(exception);                                            \
  559.         }                                                                    \
  560.     } while (false)
  561.  
  562. #elif DEBUGLEVEL == DEBUGON
  563.  
  564. #define nrequire_action(assertion, exception, action)                        \
  565.     do {                                                                    \
  566.         void*    __privateAssertion    = (void*)(assertion);                    \
  567.                                                                             \
  568.         if (__privateAssertion) {                                            \
  569.             dprintf(notrace,    "Assertion \"!(%s [= %#08X])\" Failed\n"    \
  570.                                     "Exception \"%s\" Raised",                \
  571.                 #assertion, __privateAssertion, #exception);                \
  572.             { action }                                                        \
  573.             goto exception;                                                    \
  574.             resumeLabel(exception);                                            \
  575.         }                                                                    \
  576.     } while (false)
  577.  
  578. #elif DEBUGLEVEL == DEBUGFULL
  579.  
  580. #define nrequire_action(assertion, exception, action)                        \
  581.     do {                                                                    \
  582.         void*    __privateAssertion    = (void*)(assertion);                    \
  583.                                                                             \
  584.         if (__privateAssertion) {                                            \
  585.             dprintf(notrace,    "Assertion \"!(%s [= %#08X])\" Failed\n"    \
  586.                                     "Exception \"%s\" Raised\n"                \
  587.                                     "File: %s\n"                            \
  588.                                     "Line: %d",                                \
  589.                 #assertion, __privateAssertion, #exception, __FILE__,        \
  590.                 __LINE__);                                                    \
  591.             { action }                                                        \
  592.             goto exception;                                                    \
  593.             resumeLabel(exception);                                            \
  594.         }                                                                    \
  595.     } while (false)
  596.  
  597. #else
  598.  
  599. #define nrequire_action(assertion, exception, action)                        \
  600.     do {                                                                    \
  601.         if (assertion) {                                                    \
  602.             { action }                                                        \
  603.             goto exception;                                                    \
  604.             resumeLabel(exception);                                            \
  605.         }                                                                    \
  606.     } while (false)
  607.  
  608. #endif
  609.     
  610. /*<FF>*/
  611. /*********************************************************************
  612.  
  613. MACRO
  614.     resume(exception)
  615.  
  616. DESCRIPTION
  617.     resume will resume execution after the n/require/_action statement
  618.     specified by exception. Resume lables must be on (the default) in
  619.     order to use resume. If an action form of require was used then the
  620.     action will not be re-executed.
  621.  
  622. *********************************************************************/
  623.  
  624.  
  625. #define resume(exception)                                                    \
  626.     do {                                                                    \
  627.         goto resume_ ## exception;                                            \
  628.     } while (false)
  629.  
  630.  
  631. /*<FF>*/
  632. /********************************************************************/
  633. #endif
  634.