-
Notifications
You must be signed in to change notification settings - Fork 1
/
Error.c
40 lines (31 loc) · 913 Bytes
/
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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "Error.h"
#include "Colors.h"
#define str char*
static char buffer[150]; // Maybe increase size depending on future error text
static str errnames[ERR_IO+1] = {
"DATA FORMAT ERROR",
"MEMORY ERROR",
"IO ERROR"
};
/**
* Includes the arguments passed to handleError into the format string fmsg
* @param fmsg The format string
* @param args The variable arguments
*/
static void formatMessage(const str fmsg, va_list args) {
vsnprintf(buffer, 150, fmsg, args);
}
void handleError(errType err, sevType sev, const str fmsg, ...) {
va_list args;
va_start(args, fmsg);
formatMessage(fmsg, args);
if (sev == FATAL) {
fprintf(stdout, RED "%s: %s" RESET, errnames[err], buffer);
// TODO: write/log errors to files in future if exiting
exit(-1);
}
fprintf(stdout, YELLOW "%s: %s" RESET, errnames[err], buffer);
}