home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / misc / error.c next >
C/C++ Source or Header  |  1998-06-08  |  5KB  |  193 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/misc/rcs/error.c $
  15.  * $Revision: 1.12 $
  16.  * $Author: matt $
  17.  * $Date: 1994/12/07 18:49:39 $
  18.  *
  19.  * Error handling/printing/exiting code
  20.  *
  21.  * $Log: error.c $
  22.  * Revision 1.12  1994/12/07  18:49:39  matt
  23.  * error_init() can now take NULL as parm
  24.  * 
  25.  * Revision 1.11  1994/11/29  15:42:07  matt
  26.  * Added newline before error message
  27.  * 
  28.  * Revision 1.10  1994/11/27  23:20:39  matt
  29.  * Made changes for new mprintf calling convention
  30.  * 
  31.  * Revision 1.9  1994/06/20  21:20:56  matt
  32.  * Allow NULL for warn func, to kill warnings
  33.  * 
  34.  * Revision 1.8  1994/05/20  15:11:35  mike
  35.  * mprintf Warning message so you can actually see it.
  36.  * 
  37.  * Revision 1.7  1994/02/10  18:02:38  matt
  38.  * Changed 'if DEBUG_ON' to 'ifndef NDEBUG'
  39.  * 
  40.  * Revision 1.6  1993/10/17  18:19:10  matt
  41.  * If error_init() not called, Error() now prints the error message before
  42.  * calling exit()
  43.  * 
  44.  * Revision 1.5  1993/10/14  15:29:11  matt
  45.  * Added new function clear_warn_func()
  46.  * 
  47.  * Revision 1.4  1993/10/08  16:17:19  matt
  48.  * Made Assert() call function _Assert(), rather to do 'if...' inline.
  49.  * 
  50.  * Revision 1.3  1993/09/28  12:45:25  matt
  51.  * Fixed wrong print call, and made Warning() not append a CR to string
  52.  * 
  53.  * Revision 1.2  1993/09/27  11:46:35  matt
  54.  * Added function set_warn_func()
  55.  * 
  56.  * Revision 1.1  1993/09/23  20:17:33  matt
  57.  * Initial revision
  58.  * 
  59.  *
  60.  */
  61.  
  62. #pragma off (unreferenced)
  63. static char rcsid[] = "$Id: error.c 1.12 1994/12/07 18:49:39 matt Exp $";
  64. #pragma on (unreferenced)
  65.  
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <stdarg.h>
  69. #include <string.h>
  70.  
  71. #include "mono.h"
  72. #include "error.h"
  73.  
  74. #define MAX_MSG_LEN 256
  75.  
  76. int initialized=0;
  77.  
  78. char exit_message[MAX_MSG_LEN]="";
  79. char warn_message[MAX_MSG_LEN];
  80.  
  81. //takes string in register, calls printf with string on stack
  82. void warn_printf(char *s)
  83. {
  84.     printf("%s\n",s);
  85. }
  86.  
  87. void (*warn_func)(char *s)=warn_printf;
  88.  
  89. //provides a function to call with warning messages
  90. void set_warn_func(void (*f)(char *s))
  91. {
  92.     warn_func = f;
  93. }
  94.  
  95. //uninstall warning function - install default printf
  96. void clear_warn_func(void (*f)(char *s))
  97. {
  98.     warn_func = warn_printf;
  99. }
  100.  
  101. void set_exit_message(char *fmt,...)
  102. {
  103.     va_list arglist;
  104.     int len;
  105.  
  106.     va_start(arglist,fmt);
  107.     len = vsprintf(exit_message,fmt,arglist);
  108.     va_end(arglist);
  109.  
  110.     if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in set_exit_message (len=%d, max=%d)",len,MAX_MSG_LEN);
  111.  
  112. }
  113.  
  114. void _Assert(int expr,char *expr_text,char *filename,int linenum)
  115. {
  116.     if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
  117.  
  118. }
  119. //#ifdef NDEBUG        //macros for debugging
  120. //Assert and Int3 Added by KRB because I couldn't get the macros to link 
  121. void Assert(int my_expr)
  122. {
  123.     //if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
  124.     
  125.     return;
  126. }
  127. void Int3()
  128. {
  129.     return;
  130. }
  131. //#endif
  132.  
  133. void print_exit_message()
  134. {
  135.     if (*exit_message)
  136.         printf("%s\n",exit_message);
  137. }
  138.  
  139. //terminates with error code 1, printing message
  140. void Error(char *fmt,...)
  141. {
  142.     va_list arglist;
  143.  
  144.     strcpy(exit_message,"\nError: ");
  145.  
  146.     va_start(arglist,fmt);
  147.     vsprintf(exit_message+strlen(exit_message),fmt,arglist);
  148.     va_end(arglist);
  149.  
  150.     if (!initialized) print_exit_message();
  151.  
  152.     exit(1);
  153. }
  154.  
  155. //print out warning message to user
  156. void Warning(char *fmt,...)
  157. {
  158.     va_list arglist;
  159.  
  160.     if (warn_func == NULL)
  161.         return;
  162.  
  163.     strcpy(warn_message,"Warning: ");
  164.  
  165.     va_start(arglist,fmt);
  166.     vsprintf(warn_message+strlen(warn_message),fmt,arglist);
  167.     va_end(arglist);
  168.  
  169.     mprintf((0, "%s\n", warn_message));
  170.     (*warn_func)(warn_message);
  171.  
  172. }
  173.  
  174. //initialize error handling system, and set default message. returns 0=ok
  175. int error_init(char *fmt,...)
  176. {
  177.     va_list arglist;
  178.     int len;
  179.  
  180.     atexit(print_exit_message);        //last thing at exit is print message
  181.  
  182.     if (fmt != NULL) {
  183.         va_start(arglist,fmt);
  184.         len = vsprintf(exit_message,fmt,arglist);
  185.         va_end(arglist);
  186.         if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in error_init (len=%d, max=%d)",len,MAX_MSG_LEN);
  187.     }
  188.  
  189.     initialized=1;
  190.  
  191.     return 0;
  192. }
  193.