-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.c
60 lines (53 loc) · 1.04 KB
/
err.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
#include "as_wdc6502.h"
#include <stdarg.h>
static int max_err = 10;
static int errs = 0;
static char *path = "<unknown>";
static int line = 1;
// Set maximum allowed number of errors
void err_set_max(int max)
{
max_err = max;
}
int err_total()
{
return errs;
}
// Set error source file location
void err_set_loc(char *p, int l)
{
path = p;
line = l;
}
// Check if the maximum amount of errors has been reached
static void chk()
{
if (errs >= max_err) {
fprintf(stderr, "maximum amount of errors reached, aborting\n");
exit(EXIT_FAILURE);
}
}
// Print error located at `set_err_loc(...)`
void err_at(char *fmt, ...)
{
va_list va;
va_start(va, fmt);
fprintf(stderr, "error: (%s:%d): ", path, line);
vfprintf(stderr, fmt, va);
fprintf(stderr, "\n");
va_end(va);
errs++;
chk();
}
// Print error
void err(char *fmt, ...)
{
va_list va;
va_start(va, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, va);
fprintf(stderr, "\n");
va_end(va);
errs++;
chk();
}