forked from nevali/libsupport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.c
More file actions
285 lines (266 loc) · 5.65 KB
/
log.c
File metadata and controls
285 lines (266 loc) · 5.65 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright 2014-2016 BBC
*
* Copyright 2013 Mo McRoberts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_libsupport.h"
static int log_open(void);
static int log_parse_level(const char *level);
static int log_parse_facility(const char *facility);
static int log_is_open, log_use_config, log_stderr = 0, log_level = LOG_NOTICE, log_facility = LOG_DAEMON, log_syslog = 1;
static char *log_ident;
int
log_set_ident(const char *ident)
{
char *p;
if(ident)
{
p = strdup(ident);
if(!p)
{
return -1;
}
free(log_ident);
log_ident = p;
}
log_use_config = 0;
log_reset();
return 0;
}
int
log_set_level(int level)
{
log_use_config = 0;
log_level = level;
log_reset();
return 0;
}
int
log_set_facility(int facility)
{
log_use_config = 0;
log_facility = facility;
log_reset();
return 0;
}
int
log_set_stderr(int val)
{
log_use_config = 0;
log_stderr = val;
log_reset();
return 0;
}
int
log_set_syslog(int val)
{
log_use_config = 0;
log_syslog = val;
log_reset();
return 0;
}
int
log_set_use_config(int val)
{
if(log_use_config == val)
{
return 0;
}
log_use_config = val;
log_reset();
return 0;
}
int
log_reset(void)
{
if(log_is_open && log_syslog)
{
closelog();
}
log_is_open = 0;
return 0;
}
void
log_vprintf(int level, const char *fmt, va_list ap)
{
if(!log_is_open)
{
log_open();
}
if(level > log_level)
{
return;
}
if(log_syslog)
{
vsyslog(level, fmt, ap);
}
else
{
fprintf(stderr, "%s: ", log_ident);
switch(level)
{
case LOG_DEBUG:
fprintf(stderr, "[Debug] ");
break;
case LOG_INFO:
break;
case LOG_NOTICE:
fprintf(stderr, "Notice: ");
break;
case LOG_WARNING:
fprintf(stderr, "Warning: ");
break;
case LOG_ERR:
fprintf(stderr, "Error: ");
break;
case LOG_CRIT:
fprintf(stderr, "Critical: ");
break;
case LOG_EMERG:
fprintf(stderr, "Emergency: ");
break;
}
vfprintf(stderr, fmt, ap);
}
}
void
log_printf(int level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_vprintf(level, fmt, ap);
va_end(ap);
}
static int
log_open(void)
{
int logopt;
char buf[32];
const char *ident;
if(log_is_open)
{
log_reset();
}
logopt = LOG_NDELAY|LOG_PID;
if(log_use_config)
{
free(log_ident);
log_stderr = config_get_bool("log:stderr", 0);
log_syslog = config_get_bool("log:syslog", 1);
config_get("log:level", "notice", buf, sizeof(buf));
log_level = log_parse_level(buf);
config_get("log:facility", "user", buf, sizeof(buf));
log_facility = log_parse_facility(buf);
config_get("log:ident", "(none)", buf, sizeof(buf));
ident = strdup(buf);
if(!ident)
{
return -1;
}
log_ident = (char *) ident;
}
else
{
if(!log_ident)
{
log_ident = strdup("(none)");
if(!log_ident)
{
return -1;
}
}
}
if(log_stderr)
{
logopt |= LOG_PERROR;
}
if(log_syslog)
{
openlog(log_ident, logopt, log_facility);
}
log_is_open = 1;
return 0;
}
static int
log_parse_level(const char *level)
{
if(!strcasecmp(level, "emerg") || !strcasecmp(level, "emergency")) return LOG_EMERG;
if(!strcasecmp(level, "alert")) return LOG_ALERT;
if(!strcasecmp(level, "crit") || !strcasecmp(level, "critical")) return LOG_CRIT;
if(!strcasecmp(level, "err") || !strcasecmp(level, "error")) return LOG_ERR;
if(!strcasecmp(level, "warn") || !strcasecmp(level, "warning")) return LOG_WARNING;
if(!strcasecmp(level, "notice")) return LOG_NOTICE;
if(!strcasecmp(level, "info")) return LOG_INFO;
if(!strcasecmp(level, "debug")) return LOG_DEBUG;
return atoi(level);
}
static int
log_parse_facility(const char *facility)
{
#ifdef LOG_AUTH
if(!strcasecmp(facility, "auth")) return LOG_AUTH;
#endif
#ifdef LOG_AUTHPRIV
if(!strcasecmp(facility, "authpriv")) return LOG_AUTHPRIV;
#endif
#ifdef LOG_CRON
if(!strcasecmp(facility, "cron")) return LOG_CRON;
#endif
#ifdef LOG_DAEMON
if(!strcasecmp(facility, "daemon")) return LOG_DAEMON;
#endif
#ifdef LOG_FTP
if(!strcasecmp(facility, "ftp")) return LOG_FTP;
#endif
#ifdef LOG_KERN
if(!strcasecmp(facility, "kern")) return LOG_KERN;
#endif
#ifdef LOG_LPR
if(!strcasecmp(facility, "lpr")) return LOG_LPR;
#endif
#ifdef LOG_MAIL
if(!strcasecmp(facility, "mail")) return LOG_MAIL;
#endif
#ifdef LOG_NEWS
if(!strcasecmp(facility, "news")) return LOG_NEWS;
#endif
#ifdef LOG_SECURITY
if(!strcasecmp(facility, "security")) return LOG_SECURITY;
#endif
#ifdef LOG_SYSLOG
if(!strcasecmp(facility, "syslog")) return LOG_SYSLOG;
#endif
#ifdef LOG_REMOTAUTH
if(!strcasecmp(facility, "remoteauth")) return LOG_REMOTEAUTH;
#endif
#ifdef LOG_UUCP
if(!strcasecmp(facility, "uucp")) return LOG_UUCP;
#endif
if(!strcasecmp(facility, "user")) return LOG_USER;
if(!strcasecmp(facility, "local0")) return LOG_LOCAL0;
if(!strcasecmp(facility, "local1")) return LOG_LOCAL1;
if(!strcasecmp(facility, "local2")) return LOG_LOCAL2;
if(!strcasecmp(facility, "local3")) return LOG_LOCAL3;
if(!strcasecmp(facility, "local4")) return LOG_LOCAL4;
if(!strcasecmp(facility, "local5")) return LOG_LOCAL5;
if(!strcasecmp(facility, "local6")) return LOG_LOCAL6;
if(!strcasecmp(facility, "local7")) return LOG_LOCAL7;
return LOG_USER;
}