-
Notifications
You must be signed in to change notification settings - Fork 6
/
logging.c
148 lines (127 loc) · 3.3 KB
/
logging.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "logging.h"
#include <stdarg.h>
#include <strings.h>
#include <event2/event.h>
#include "config.h"
struct module logging_module = {
.name = "logging",
.conf = logging_setup,
};
#define table_entry(name) { #name, LOG_##name }
struct level {
char *name;
int level;
} level_table[] = {
table_entry(EMERG),
table_entry(ALERT),
table_entry(CRIT),
table_entry(ERR),
table_entry(WARNING),
table_entry(NOTICE),
table_entry(INFO),
table_entry(DEBUG),
/* extra mappings */
{ "CRITICAL", LOG_CRIT },
{ "ERROR", LOG_ERR },
{ NULL }
};
struct facility {
char *name;
int facility;
} facility_table[] = {
table_entry(AUTH),
table_entry(AUTHPRIV),
table_entry(CRON),
table_entry(DAEMON),
table_entry(FTP),
table_entry(KERN),
table_entry(LOCAL0),
table_entry(LOCAL1),
table_entry(LOCAL2),
table_entry(LOCAL3),
table_entry(LOCAL4),
table_entry(LOCAL5),
table_entry(LOCAL6),
table_entry(LOCAL7),
table_entry(LPR),
table_entry(MAIL),
table_entry(NEWS),
table_entry(SYSLOG),
table_entry(USER),
table_entry(UUCP),
{ NULL }
};
typedef void (logger)(int, const char *, va_list);
static logger *skeeter_vlog;
logger skeeter_syslog, skeeter_stderr;
int logging_setup(struct module *module, config_setting_t *conf)
{
config_setting_t *setting;
const char *val;
int num;
if (config.debug < 2)
skeeter_vlog = &skeeter_syslog;
num = LOG_ERR;
setting = config_setting_get_member(conf, "level");
if (setting) {
struct level *level;
val = config_setting_get_string(setting);
for (level = level_table; level->name; level++) {
if (!strcasecmp(val, level->name))
break;
}
if (!level->name) {
skeeter_log(LOG_ERR, "Could not parse loglevel value '%s'", val);
return 1;
}
num = level->level;
}
config.loglevel = num;
num = LOG_MAIL;
setting = config_setting_get_member(conf, "facility");
if (setting) {
struct facility *facility;
val = config_setting_get_string(setting);
for (facility = facility_table; facility->name; facility++) {
if (!strcasecmp(val, facility->name))
break;
}
if (!facility->name) {
skeeter_log(LOG_ERR, "Could not parse log facility value '%s'", val);
return 1;
}
num = facility->facility;
}
config.facility = num;
openlog("skeeter", LOG_PID, config.facility);
event_set_log_callback(skeeter_event_log);
return 0;
}
void
skeeter_log(int level, const char *format, ...)
{
va_list args;
if (level > config.loglevel)
return;
va_start(args, format);
skeeter_vlog(level, format, args);
va_end(args);
}
void
skeeter_syslog(int level, const char *format, va_list args)
{
vsyslog(level, format, args);
}
void
skeeter_stderr(int level, const char *format, va_list args)
{
fprintf(stderr, "%s:\t", level_table[level].name);
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
}
static logger *skeeter_vlog = skeeter_stderr;
void skeeter_event_log(int severity, const char *msg)
{
/* make sure we never interpret msg as a format string */
skeeter_log(severity, "%s", msg);
}