-
Notifications
You must be signed in to change notification settings - Fork 6
/
log.c
134 lines (111 loc) · 1.75 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <stdlib.h>
#include "log.h"
#include "util.h"
static enum log_level min_log_level = LOG_NOTICE;
static FILE *log_file = NULL;
static int log_do_scrub = 1;
void
log_debug(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
log_msg_va(LOG_DEBUG, 0, msg, ap);
va_end(ap);
}
void
log_info(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
log_msg_va(LOG_INFO, 0, msg, ap);
va_end(ap);
}
void
log_notice(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
log_msg_va(LOG_NOTICE, 0, msg, ap);
va_end(ap);
}
void
log_warn(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
log_msg_va(LOG_WARN, 0, msg, ap);
va_end(ap);
}
void
log_error(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
log_msg_va(LOG_ERROR, 0, msg, ap);
va_end(ap);
}
void
log_socket_error(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
log_msg_va(LOG_ERROR, 1, msg, ap);
va_end(ap);
}
void
log_fatal(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
log_msg_va(LOG_FATAL, 0, msg, ap);
va_end(ap);
}
void
log_msg_va(enum log_level lvl, int serr, const char *msg, va_list ap)
{
if (lvl >= min_log_level) {
vfprintf(log_file, msg, ap);
if (serr)
fprintf(log_file, ": %s", socket_error_string(-1));
fputs("\n", log_file);
fflush(log_file);
if (lvl >= LOG_FATAL)
abort();
}
}
void
log_set_min_level(enum log_level lvl)
{
min_log_level = lvl;
}
enum log_level
log_get_min_level(void)
{
return min_log_level;
}
void
log_set_file(FILE *fp)
{
if (!fp)
log_file = stderr;
else
log_file = fp;
}
void
log_set_scrub(int scrub)
{
log_do_scrub = scrub;
}
int
log_get_scrub(void)
{
return log_do_scrub;
}
const char *
log_scrub(const char *what)
{
if (log_do_scrub)
return "(scrubbed)";
return what;
}