-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
58 lines (45 loc) · 1.14 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
/* Error reporting facilities.
*
* Copyright (c) 2010 James E. Ingram
* See the LICENSE file for terms of use.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include "error.h"
#include "port.h"
#ifdef DEBUG
static error_level current_level = INFO;
#else
static error_level current_level = WARNING;
#endif
static char *level_names[] = {
"ERROR",
"WARNING",
"INFO"
};
void set_error_level(error_level level)
{
if (level > INFO) {
warn("ignoring invalid error reporting level %d", level);
return;
}
current_level = level;
}
void print_error(error_level level, char const * const file, int line,
char const * const func, char const * const fmt, ...)
{
if (level > current_level)
return;
(void)fflush(get_output_port()->value.port.file);
write_error("%s\t%s:%d:%s: ", level_names[level], file, line, func);
va_list arg_list;
va_start(arg_list, fmt);
va_write_error(fmt, arg_list);
va_end(arg_list);
if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':') {
write_error(" %s", strerror(errno));
}
write_error("\n");
}