[go: up one dir, main page]

Menu

[r9]: / engine / ithelib / debug.c  Maximize  Restore  History

Download this file

293 lines (228 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Debugging and log routines
*/
#define ITHE_VERSION "V1.50 (built " __DATE__ " at " __TIME__ ")"
#define ITHE_VERSIOND "***DEBUG***" ITHE_VERSION
#ifdef _WIN32
#define WIN32_EXTRA_LEAN
#include <windows.h>
#include <conio.h>
#else
#include <signal.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "ithelib.h"
// Variables
char ilog_nopanic=0;
char ilog_logoff=0;
static char *bootlog=NULL;
// Functions
void (*ilog_printf)(const char *msg, ...);
void ilog_start(const char *logname, const char *prevlog);
void ilog_stop(void);
void ilog_text(const char *msg, ...);
void ilog_quiet(const char *msg, ...);
void ithe_panic(const char *m,const char *n);
void ithe_safepanic(const char *m,const char *n);
void ithe_superpanic(void);
extern void ithe_userexitfunction(void);
// Code
char *ithe_get_version()
{
#ifdef ITHE_DEBUG
return ITHE_VERSIOND;
#else
return ITHE_VERSION;
#endif
}
/*
* Start the logging system
*/
void ilog_start(const char *logname, const char *prevlog)
{
char newlog[1024];
char oldlog[1024];
if(bootlog)
ithe_panic("Bootlog already started",NULL);
strcpy(newlog,logname);
strcpy(oldlog,prevlog);
ilog_printf=ilog_text; // Set default logger, backup last log
remove(oldlog);
rename(newlog,oldlog);
// Create the bootlog
bootlog=(char *)calloc(1,strlen(newlog)+1);
strcpy(bootlog,newlog);
}
/*
* Stop the logging system
*/
void ilog_stop(void)
{
if(bootlog)
free(bootlog);
bootlog=NULL;
}
/*
* Log to file and using printf
*/
void ilog_text(const char *msg, ...)
{
FILE *blt;
va_list ap;
if(ilog_logoff)
return;
if(!bootlog)
ithe_panic("ilog_text() - Missing call to ilog_start",NULL);
va_start(ap, msg);
vprintf(msg,ap);
va_end(ap);
blt=fopen(bootlog,"a");
if(!blt)
ithe_panic("Could not append to log file",bootlog);
va_start(ap, msg);
vfprintf(blt,msg,ap);
va_end(ap);
fclose(blt);
}
/*
* Log to file but don't print it onscreen
*/
void ilog_quiet(const char *msg, ...)
{
FILE *blt;
va_list ap;
if(ilog_logoff)
return;
if(!bootlog)
return;
// ithe_panic("ilog_quiet() - Missing call to ilog_start",NULL);
va_start(ap, msg);
blt=fopen(bootlog,"a");
if(!blt)
ithe_panic("Could not append to log file",bootlog);
vfprintf(blt,msg,ap);
fclose(blt);
va_end(ap);
}
/*
* quit and report the problem
*/
void ithe_panic(const char *m,const char *n)
{
static int pip=0;
#ifdef _WIN32
char msg[1024];
#endif
if(ilog_nopanic) // Quit without ceremony
exit(1);
if(pip)
{
printf("Panic Panic\n");
printf("%s\n",m);
if(n)
printf("%s\n",n);
exit(1);
}
pip = 1; // Safety valve, in case panic panics
ithe_userexitfunction();
#ifdef _WIN32
strcpy(msg,m);
if(n)
{
strcat(msg,"\n");
strcat(msg,n);
}
MessageBox(NULL,msg,"IRE Panic:",MB_ICONSTOP);
#else
#ifdef __DJGPP__
printf("(!)\n");
printf("ÚÄPANIC:ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n");
printf("³ ³\r³ %s\n",m);
if(n)
printf("³ ³\r³ %s\n",n);
printf("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n\n\n");
#else
printf("(!)\n");
printf("* PANIC: **********************************************************************\n");
printf("* *\r* %s\n",m);
if(n)
printf("* *\r* %s\n",n);
printf("*******************************************************************************\n\n\n");
#endif
#endif
ilog_quiet("PANIC:\n");
ilog_quiet("\n");
ilog_quiet(m);
ilog_quiet("\n");
if(n) ilog_quiet(n);
ilog_quiet("\n");
if(getenv("SUPERPANIC"))
ithe_superpanic();
exit(1);
}
/*
* 'Safe' panic - for C++ destructors
*/
void ithe_safepanic(const char *m,const char *n)
{
#ifdef _WIN32
char msg[1024];
#endif
if(ilog_nopanic)
exit(1);
#ifdef _WIN32
strcpy(msg,m);
if(n)
{
strcat(msg,"\n");
strcat(msg,n);
}
MessageBox(NULL,msg,"IRE panic:",MB_ICONSTOP);
#else
#ifdef __DJGPP__
printf("ÚÄSAFEPANIC:ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n");
printf("³ ³\r³ %s\n",m);
if(n)
printf("³ ³\r³ %s\n",n);
printf("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n\n\n");
#else
printf("* SAFEPANIC: ******************************************************************\n");
printf("* *\r* %s\n",m);
if(n)
printf("* *\r* %s\n",n);
printf("*******************************************************************************\n\n\n");
#endif
#endif
exit(1);
}
/*
* Crash the system to get a core/stack trace, so we can examine what happened
*/
void ithe_superpanic(void)
{
fprintf(stderr,"Causing an exception...\n");
#ifdef _WIN32
DebugBreak();
#else
raise(SIGTRAP);
#endif
}
/*
* Complain: whinge at the user
*/
void ithe_complain(const char *m)
{
int tmp,tmp2;
// MessageBox(NULL,m,"Warning:",MB_ICONEXCLAMATION);
printf("%s\n",m);
printf("Press enter to continue.\n");
#if defined(__DJGPP__) || defined(_WIN32)
tmp2=tmp=getch();
#else
tmp2=read(0,&tmp,1); // blocking keypress (read byte from stdin)
#endif
}