-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.c
130 lines (117 loc) · 3.15 KB
/
error.c
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
/*
* error.c - standard error printing routines
*/
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "error.h"
static char *program = NULL;
static char *filename = "-";
static FILE *error_stream = NULL;
static int initialized = 0;
static enum error_level elevel = ERROR_LEVEL_NOTICE;
int n_error = 0;
int n_warning = 0;
void initialize_error_subsystem(char *prog,char *filename){
FILE *new_stream;
program = prog;
initialized = 1;
if (!strcmp(filename,"-")){
error_stream = stderr;
} else {
if (new_stream = fopen(filename,"a+")){
error_stream = new_stream;
} else {
/* avoid looping in call to fatal */
fatal("can not open error file: %s\n",filename);
}
}
return;
}
void fatal(char *message,...){
va_list ap;
if (elevel >= ERROR_LEVEL_FATAL){
if (!initialized) initialize_error_subsystem(NULL,"-");
va_start(ap,message);
if (program != NULL) fprintf(error_stream,"%s: ",program);
fprintf(error_stream,"fatal error: ");
vfprintf(error_stream,message,ap);
va_end(ap);
}
exit(1);
}
void error(char *message,...){
va_list ap;
if (elevel >= ERROR_LEVEL_ERROR){
if (!initialized) initialize_error_subsystem(NULL,"-");
va_start(ap,message);
if (program != NULL) fprintf(error_stream,"%s: ",program);
fprintf(error_stream,"error: ");
vfprintf(error_stream,message,ap);
va_end(ap);
}
n_error++;
}
void warning(char *message,...){
va_list ap;
if (elevel >= ERROR_LEVEL_WARNING){
if (!initialized) initialize_error_subsystem(NULL,"-");
va_start(ap,message);
if (program != NULL) fprintf(error_stream,"%s: ",program);
fprintf(error_stream,"warning: ");
vfprintf(error_stream,message,ap);
va_end(ap);
}
n_warning++;
}
void notice(char *message,...){
va_list ap;
if (elevel >= ERROR_LEVEL_NOTICE){
if (!initialized) initialize_error_subsystem(NULL,"-");
va_start(ap,message);
if (program != NULL) fprintf(error_stream,"%s: ",program);
vfprintf(error_stream,message,ap);
va_end(ap);
}
}
void notice_noprogram(char *message,...){
va_list ap;
if (elevel >= ERROR_LEVEL_NOTICE){
if (!initialized) initialize_error_subsystem(NULL,"-");
va_start(ap,message);
// if (program != NULL) fprintf(error_stream,"%s: ",program);
vfprintf(error_stream,message,ap);
va_end(ap);
}
}
void debug(char *message,...){
va_list ap;
if (elevel >= ERROR_LEVEL_DEBUG){
if (!initialized) initialize_error_subsystem(NULL,"-");
va_start(ap,message);
if (program != NULL) fprintf(error_stream,"%s: ",program);
fprintf(error_stream,"debug: ");
vfprintf(error_stream,message,ap);
va_end(ap);
}
}
void debug2(char *message,...){
va_list ap;
if (elevel >= ERROR_LEVEL_DEBUG2){
if (!initialized) initialize_error_subsystem(NULL,"-");
va_start(ap,message);
if (program != NULL) fprintf(error_stream,"%s: ",program);
fprintf(error_stream,"debug: ");
vfprintf(error_stream,message,ap);
va_end(ap);
}
}
void set_error_stream(FILE *fp){
error_stream = fp;
}
void set_error_level(enum error_level level){
elevel = level;
}
enum error_level get_error_level(void){
return elevel;
}