forked from CS429-S2022/CI-Lab-Student
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherr_handler.c
More file actions
77 lines (65 loc) · 1.88 KB
/
Copy patherr_handler.c
File metadata and controls
77 lines (65 loc) · 1.88 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
/**************************************************************************
* C S 429 EEL interpreter
*
* err_handler.c - The error-handling module.
*
* Copyright (c) 2021. S. Chatterjee, X. Shen, T. Byrd. All rights reserved.
* May not be used, modified, or copied without permission.
**************************************************************************/
#include "ci.h"
#include "ansicolors.h"
bool terminate = false;
bool ignore_input = false;
static char printbuf[100];
static char *sevnames[LOG_FATAL+1] = {
"INFO",
"WARNING",
"ERROR",
"FATAL"
};
static char *sevcolors[LOG_FATAL+1] = {
ANSI_COLOR_CYAN,
ANSI_COLOR_YELLOW,
ANSI_COLOR_RED,
ANSI_BOLD ANSI_COLOR_RED
};
static char *errnames[ERR_UNDEFINED+1] = {
"Failed Lexical Analysis",
"Failed Syntactic Analysis",
"Failed Type Inference",
"Failed Evaluation",
"Undefined Variable"
};
static char* format_log_message(log_lev_t sev, char *msg) {
sprintf(printbuf, "%s\t[%s] %s" ANSI_RESET, sevcolors[sev], sevnames[sev], msg);
return printbuf;
}
int logging(log_lev_t sev, char* msg) {
if (terminate) return 0;
switch (sev) {
case LOG_INFO:
break;
case LOG_WARNING:
case LOG_ERROR:
if (ignore_input) return 0;
ignore_input = true;
break;
case LOG_FATAL:
terminate = true;
break;
default:
break;
}
if (outfile != stdout && sev == LOG_ERROR) {
fprintf(outfile, "\t[ERROR]\n");
}
return fprintf(errfile, "%s\n", format_log_message(sev, msg));
}
int handle_error(err_type_t err) {
if (ignore_input) return 0;
ignore_input = true;
if (outfile != stdout) {
return fprintf(outfile, "\tERROR: %s\n", errnames[err]);
}
return fprintf(outfile, ANSI_COLOR_RED "\tERROR: %s\n" ANSI_RESET, errnames[err]);
}