-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
88 lines (62 loc) · 1.22 KB
/
log.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
/*
* moto-design SGV utils.
*/
#define _GNU_SOURCE
#define _ISOC99_SOURCE
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "log.h"
static bool exit_on_error = false;
static bool verbose_state = false;
void log_set_exit_on_error(bool state)
{
exit_on_error = state;
}
void log_set_verbose(bool state)
{
verbose_state = state;
}
bool log_get_verbose(void)
{
return verbose_state;
}
void __attribute__((unused)) _error(const char *func, int line,
const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "ERROR: %s:%d: ", func, line);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fflush(stderr);
va_end(ap);
if (exit_on_error) {
exit(EXIT_FAILURE);
}
}
void __attribute__((unused)) _log(const char *func, int line,
const char *fmt, ...)
{
va_list ap;
if (!verbose_state) {
return;
}
fprintf(stderr, "%s:%d: ", func, line);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fflush(stderr);
va_end(ap);
}
void __attribute__((unused)) _warn(const char *func, int line,
const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "WARNING: %s:%d: ", func, line);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fflush(stderr);
va_end(ap);
}