]> icculus.org git repositories - btb/d2x.git/blob - misc/error.c
Added d1x license
[btb/d2x.git] / misc / error.c
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: /cvs/cvsroot/d2x/misc/error.c,v $
15  * $Revision: 1.2 $
16  * $Author: bradleyb $
17  * $Date: 2001-01-31 15:18:04 $
18  *
19  * Error handling/printing/exiting code
20  *
21  * $Log: not supported by cvs2svn $
22  * Revision 1.1.1.1  2001/01/19 03:30:14  bradleyb
23  * Import of d2x-0.0.8
24  *
25  * Revision 1.1.1.1  1999/06/14 22:13:48  donut
26  * Import of d1x 1.37 source.
27  *
28  * Revision 1.12  1994/12/07  18:49:39  matt
29  * error_init() can now take NULL as parm
30  * 
31  * Revision 1.11  1994/11/29  15:42:07  matt
32  * Added newline before error message
33  * 
34  * Revision 1.10  1994/11/27  23:20:39  matt
35  * Made changes for new mprintf calling convention
36  * 
37  * Revision 1.9  1994/06/20  21:20:56  matt
38  * Allow NULL for warn func, to kill warnings
39  * 
40  * Revision 1.8  1994/05/20  15:11:35  mike
41  * mprintf Warning message so you can actually see it.
42  * 
43  * Revision 1.7  1994/02/10  18:02:38  matt
44  * Changed 'if DEBUG_ON' to 'ifndef NDEBUG'
45  * 
46  * Revision 1.6  1993/10/17  18:19:10  matt
47  * If error_init() not called, Error() now prints the error message before
48  * calling exit()
49  * 
50  * Revision 1.5  1993/10/14  15:29:11  matt
51  * Added new function clear_warn_func()
52  * 
53  * Revision 1.4  1993/10/08  16:17:19  matt
54  * Made Assert() call function _Assert(), rather to do 'if...' inline.
55  * 
56  * Revision 1.3  1993/09/28  12:45:25  matt
57  * Fixed wrong print call, and made Warning() not append a CR to string
58  * 
59  * Revision 1.2  1993/09/27  11:46:35  matt
60  * Added function set_warn_func()
61  * 
62  * Revision 1.1  1993/09/23  20:17:33  matt
63  * Initial revision
64  * 
65  *
66  */
67
68 #ifdef HAVE_CONFIG_H
69 #include <conf.h>
70 #endif
71
72 #ifdef RCS
73 static char rcsid[] = "$Id: error.c,v 1.2 2001-01-31 15:18:04 bradleyb Exp $";
74 #endif
75
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <stdarg.h>
79 #include <string.h>
80
81 #include "pstypes.h"
82 #include "console.h"
83 #include "error.h"
84
85 #define MAX_MSG_LEN 256
86
87 //edited 05/17/99 Matt Mueller added err_ prefix to prevent conflicts with statically linking SDL
88 int err_initialized=0;
89 //end edit -MM
90
91 char exit_message[MAX_MSG_LEN]="";
92 char warn_message[MAX_MSG_LEN];
93
94 //takes string in register, calls printf with string on stack
95 void warn_printf(char *s)
96 {
97         printf("%s\n",s);
98 }
99
100 void (*warn_func)(char *s)=warn_printf;
101
102 //provides a function to call with warning messages
103 void set_warn_func(void (*f)(char *s))
104 {
105         warn_func = f;
106 }
107
108 //uninstall warning function - install default printf
109 void clear_warn_func(void (*f)(char *s))
110 {
111         warn_func = warn_printf;
112 }
113
114 void set_exit_message(char *fmt,...)
115 {
116         va_list arglist;
117         int len;
118
119         va_start(arglist,fmt);
120         len = vsprintf(exit_message,fmt,arglist);
121         va_end(arglist);
122
123         if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in set_exit_message (len=%d, max=%d)",len,MAX_MSG_LEN);
124
125 }
126
127 void _Assert(int expr,char *expr_text,char *filename,int linenum)
128 {
129         if (!(expr)) Error("Assertion failed: %s, file %s, line %d",expr_text,filename,linenum);
130
131 }
132
133 void print_exit_message(void)
134 {
135         if (*exit_message)
136                 con_printf(CON_CRITICAL, "%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         con_printf(CON_CRITICAL, 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         con_printf(CON_URGENT, warn_message);
170
171 }
172
173 //initialize error handling system, and set default message. returns 0=ok
174 int error_init(char *fmt,...)
175 {
176         va_list arglist;
177         int len;
178
179         atexit(print_exit_message);             //last thing at exit is print message
180
181         if (fmt != NULL) {
182                 va_start(arglist,fmt);
183                 len = vsprintf(exit_message,fmt,arglist);
184                 va_end(arglist);
185                 if (len==-1 || len>MAX_MSG_LEN) Error("Message too long in error_init (len=%d, max=%d)",len,MAX_MSG_LEN);
186         }
187
188         err_initialized=1;
189
190         return 0;
191 }