1693
|
1
|
|
2 #include <stdlib.h>
|
|
3 #include <stdio.h>
|
|
4 #include <stdarg.h>
|
|
5
|
|
6 #include "error.h"
|
|
7
|
1860
|
8 int debug_level = 2;
|
1693
|
9 FILE * debug_file;
|
|
10 int debug_stderr = 0;
|
|
11
|
|
12 void defaultErrorHandler( int critical,const char * format, ... )
|
|
13 {
|
|
14 char * p;
|
|
15 va_list ap;
|
|
16
|
|
17 if ( (p=(char *)malloc( 512 ) ) == NULL ) return;
|
|
18 va_start( ap,format );
|
|
19 vsnprintf( p,512,format,ap );
|
|
20 va_end( ap );
|
|
21 fprintf( stderr,"%s",p );
|
|
22 free( p );
|
|
23 if ( critical ) exit( 1 );
|
|
24 }
|
|
25
|
|
26 void defaultDebugHandler( int critical,const char * format, ... )
|
|
27 {
|
|
28 char * p;
|
|
29 va_list ap;
|
|
30
|
|
31 if ( critical >= debug_level ) return;
|
|
32 if ( (p=(char *)malloc( 512 ) ) == NULL ) return;
|
|
33 va_start( ap,format );
|
|
34 vsnprintf( p,512,format,ap );
|
|
35 va_end( ap );
|
|
36 fprintf( debug_file,"%s",p );
|
|
37 free( p );
|
|
38 }
|
|
39
|
|
40 errorTHandler message = defaultErrorHandler;
|
|
41 errorTHandler dbprintf = defaultDebugHandler;
|
|
42
|
|
43 void initDebug( char * name )
|
|
44 {
|
|
45 if ( name )
|
|
46 {
|
|
47 if ( ( debug_file=fopen( name,"wt+" ) ) != NULL )
|
|
48 {
|
|
49 debug_stderr=0;
|
|
50 return;
|
|
51 }
|
|
52 }
|
|
53 debug_file=stderr;
|
|
54 debug_stderr=1;
|
|
55 }
|
|
56 void doneDebug( void )
|
|
57 {
|
|
58 if ( !debug_stderr ) fclose( debug_file );
|
|
59 debug_file=stderr;
|
|
60 debug_stderr=1;
|
|
61 }
|