ERROR.C 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #pragma off (unreferenced)
  62. static char rcsid[] = "$Id: error.c 1.12 1994/12/07 18:49:39 matt Exp $";
  63. #pragma on (unreferenced)
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <stdarg.h>
  67. #include <string.h>
  68. #include "mono.h"
  69. #include "error.h"
  70. #define MAX_MSG_LEN 256
  71. int initialized=0;
  72. char exit_message[MAX_MSG_LEN]="";
  73. char warn_message[MAX_MSG_LEN];
  74. //takes string in register, calls printf with string on stack
  75. void warn_printf(char *s)
  76. {
  77. printf("%s\n",s);
  78. }
  79. void (*warn_func)(char *s)=warn_printf;
  80. //provides a function to call with warning messages
  81. void set_warn_func(void (*f)(char *s))
  82. {
  83. warn_func = f;
  84. }
  85. //uninstall warning function - install default printf
  86. void clear_warn_func(void (*f)(char *s))
  87. {
  88. warn_func = warn_printf;
  89. }
  90. void set_exit_message(char *fmt,...)
  91. {
  92. va_list arglist;
  93. int len;
  94. va_start(arglist,fmt);
  95. len = vsprintf(exit_message,fmt,arglist);
  96. va_end(arglist);
  97. if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in set_exit_message (len=%d, max=%d)",len,MAX_MSG_LEN);
  98. }
  99. void _Assert(int expr,char *expr_text,char *filename,int linenum)
  100. {
  101. if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
  102. }
  103. //#ifdef NDEBUG //macros for debugging
  104. //Assert and Int3 Added by KRB because I couldn't get the macros to link
  105. void Assert(int my_expr)
  106. {
  107. //if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
  108. return;
  109. }
  110. void Int3()
  111. {
  112. return;
  113. }
  114. //#endif
  115. void print_exit_message()
  116. {
  117. if (*exit_message)
  118. printf("%s\n",exit_message);
  119. }
  120. //terminates with error code 1, printing message
  121. void Error(char *fmt,...)
  122. {
  123. va_list arglist;
  124. strcpy(exit_message,"\nError: ");
  125. va_start(arglist,fmt);
  126. vsprintf(exit_message+strlen(exit_message),fmt,arglist);
  127. va_end(arglist);
  128. if (!initialized) print_exit_message();
  129. exit(1);
  130. }
  131. //print out warning message to user
  132. void Warning(char *fmt,...)
  133. {
  134. va_list arglist;
  135. if (warn_func == NULL)
  136. return;
  137. strcpy(warn_message,"Warning: ");
  138. va_start(arglist,fmt);
  139. vsprintf(warn_message+strlen(warn_message),fmt,arglist);
  140. va_end(arglist);
  141. mprintf((0, "%s\n", warn_message));
  142. (*warn_func)(warn_message);
  143. }
  144. //initialize error handling system, and set default message. returns 0=ok
  145. int error_init(char *fmt,...)
  146. {
  147. va_list arglist;
  148. int len;
  149. atexit(print_exit_message); //last thing at exit is print message
  150. if (fmt != NULL) {
  151. va_start(arglist,fmt);
  152. len = vsprintf(exit_message,fmt,arglist);
  153. va_end(arglist);
  154. if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in error_init (len=%d, max=%d)",len,MAX_MSG_LEN);
  155. }
  156. initialized=1;
  157. return 0;
  158. }