-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging.c
376 lines (333 loc) · 8.88 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#define _GNU_SOURCE
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#include <pthread.h>
#include "logging.h"
#undef logger_message
typedef logger_result_type (*logger_procedure)(const logger_cls_type*, const logger_ctx_type, const logger_lvl_type, const char*, va_list ap);
struct logger_chain_struct {
const char* name;
logger_procedure logger;
};
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct logger_setup_struct logger_setup;
static logger_cls_type logger = LOGGER_INITIALIZE(__FILE__);
logger_ctx_type logger_noctx = NULL;
logger_ctx_type logger_ctx;
logger_cls_type logger_cls = LOGGER_INITIALIZE("");
int
logger_enabled(logger_cls_type* cls, logger_ctx_type ctx, logger_lvl_type lvl)
{
(void)ctx;
if(cls->setupserial != logger_setup.serial)
logger_resetup(cls);
if(lvl <= cls->minlvl)
return 1;
else
return 0;
}
void
logger_message(logger_cls_type* cls, logger_ctx_type ctx, logger_lvl_type lvl, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if(!logger_enabled(cls, ctx, lvl))
return;
logger_messageinternal(cls,ctx,lvl,fmt,ap);
va_end(ap);
}
void
logger_vmessage(logger_cls_type* cls, logger_ctx_type ctx, logger_lvl_type lvl, const char* fmt, va_list ap)
{
if(!logger_enabled(cls, ctx, lvl))
return;
logger_messageinternal(cls,ctx,lvl,fmt,ap);
}
void
logger_messageinternal(logger_cls_type* cls, logger_ctx_type ctx, logger_lvl_type lvl, const char* fmt, ...)
{
va_list ap;
if(!logger_enabled(cls, ctx, lvl))
return;
va_start(ap,fmt);
vfprintf(stderr,fmt,ap);
va_end(ap);
}
static pthread_key_t currentctx;
struct logger_ctx_struct {
struct logger_ctx_struct* prev;
const char* label;
};
const char*
logger_getcontext(logger_ctx_type ctx)
{
if(ctx == logger_ctx) {
ctx = pthread_getspecific(currentctx);
}
if(ctx != logger_noctx) {
return ctx->label;
} else {
return NULL;
}
}
logger_result_type
logger_log_syslog(const logger_cls_type* cls, const logger_ctx_type ctx, const logger_lvl_type lvl, const char* format, va_list ap)
{
int priority;
(void)cls;
(void)ctx;
switch(lvl) {
case logger_FATAL: priority = LOG_ALERT; break;
case logger_ERROR: priority = LOG_ERR; break;
case logger_WARN: priority = LOG_WARNING; break;
case logger_INFO: priority = LOG_NOTICE; break;
case logger_DEBUG: priority = LOG_INFO; break;
case logger_DIAG: priority = LOG_DEBUG; break;
default:
priority = LOG_ERR;
}
vsyslog(priority, format, ap);
return logger_CONT;
}
logger_result_type
logger_log_stderr(const logger_cls_type* cls, const logger_ctx_type ctx, const logger_lvl_type lvl, const char* format, va_list ap)
{
const char* priority;
char* message;
const char* location;
const char* context;
switch(lvl) {
case logger_FATAL: priority = "fatal error: "; break;
case logger_ERROR: priority = "error: "; break;
case logger_WARN: priority = "warning: "; break;
case logger_INFO: priority = ""; break;
case logger_DEBUG: priority = ""; break;
case logger_DIAG: priority = ""; break;
default:
priority = "unknown problem: ";
}
context = logger_getcontext(ctx);
location = cls->name;
vasprintf(&message, format, ap);
fprintf(stderr,"%s%s%s%s%s%s%s%s\n",priority,(location?"[":""),(location?location:""),(location?"] ":""), message, (context?" (":""), (context), (context?")":""));
fprintf(stderr,"%s",message);
return logger_CONT;
}
void
logger_log_syslog_open(const char* argv0)
{
openlog(argv0, LOG_NDELAY, LOG_DAEMON);
}
void
logger_log_syslog_close(void)
{
closelog();
}
static void
destroyctx(void* arg)
{
logger_ctx_type ctx = (logger_ctx_type)arg;
logger_destroycontext(ctx);
}
void
logger_initialize(const char* programname)
{
logger_setup.serial = 2;
logger_setup.nchains = 1;
logger_setup.chains = malloc(sizeof(struct logger_chain_struct) * logger_setup.nchains);
logger_setup.chains[0].name = "";
logger_setup.chains[0].logger = logger_log_stderr;
logger_log_syslog_open(programname);
//logger_setup.chains[0].logger = logger_log_syslog;
logger_message(&logger, logger_noctx, logger_INFO, "%s started",programname);
pthread_key_create(¤tctx, destroyctx);
logger_ctx = logger_newcontext();
}
void
logger_finalize(void)
{
pthread_key_delete(currentctx);
}
void
logger_resetup(logger_cls_type* cls)
{
pthread_mutex_lock(&mutex);
if(cls->setupserial == 1) {
logger_initialize(NULL);
}
cls->setupserial = logger_setup.serial;
cls->minlvl = logger_WARN;
cls->chain = &logger_setup.chains[0];
pthread_mutex_unlock(&mutex);
}
logger_ctx_type
logger_newcontext(void)
{
logger_ctx_type ctx;
ctx = malloc(sizeof(struct logger_ctx_struct));
ctx->prev = NULL;
ctx->label = NULL;
return ctx;
}
void
logger_destroycontext(logger_ctx_type ctx)
{
free((void*)ctx->label);
free((void*)ctx);
}
void
logger_setcontext(logger_ctx_type ctx)
{
pthread_setspecific(currentctx, ctx);
}
void
logger_pushcontext(logger_ctx_type ctx)
{
logger_ctx_type prev;
if(ctx == logger_noctx) {
ctx = logger_newcontext();
} else if (ctx == logger_ctx) {
ctx = logger_newcontext();
ctx->label = (ctx->label ? strdup(ctx->label) : NULL);
}
prev = pthread_getspecific(currentctx);
ctx->prev = prev;
pthread_setspecific(currentctx, ctx);
}
void
logger_popcontext(void)
{
logger_ctx_type prev, ctx;
ctx = pthread_getspecific(currentctx);
prev = ctx->prev;
logger_destroycontext(ctx);
pthread_setspecific(currentctx, prev);
}
void
logger_clearcontext(void)
{
logger_ctx_type prev, ctx;
for(ctx=pthread_getspecific(currentctx); ctx; ctx=prev) {
prev = ctx->prev;
logger_destroycontext(ctx);
}
pthread_setspecific(currentctx, NULL);
}
void
logger_putcontext(logger_ctx_type ctx, const char* key, const char* value)
{
char* newdesc;
if(ctx->label) {
if(key) {
asprintf(&newdesc,"%s,%s=%s",ctx->label,key,value);
} else {
asprintf(&newdesc,"%s,%s",ctx->label,value);
}
} else {
if(key) {
asprintf(&newdesc,"%s=%s",key,value);
} else {
asprintf(&newdesc,"%s",value);
}
}
free((void*)ctx->label);
ctx->label = newdesc;
}
#ifndef DEPRECATE
#ifdef HAVE_SYSLOG_H
#include <strings.h> /* strncasecmp() */
#include <syslog.h> /* openlog(), closelog(), syslog() */
#else /* !HAVE_SYSLOG_H */
#define LOG_EMERG 0 /* ods_fatal_exit */
#define LOG_ALERT 1 /* ods_log_alert */
#define LOG_CRIT 2 /* ods_log_crit */
#define LOG_ERR 3 /* ods_log_error */
#define LOG_WARNING 4 /* ods_log_warning */
#define LOG_NOTICE 5 /* ods_log_info */
#define LOG_INFO 6 /* ods_log_verbose */
#define LOG_DEBUG 7 /* ods_log_debug */
#endif /* HAVE_SYSLOG_H */
#define LOG_DEEEBUG 8 /* ods_log_deeebug */
void
ods_log_deeebug(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_TRACE, format, ap);
va_end(ap);
}
void
ods_log_debug(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_DEBUG, format, ap);
va_end(ap);
}
void
ods_log_verbose(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_DEBUG, format, ap);
va_end(ap);
}
void
ods_log_info(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_INFO, format, ap);
va_end(ap);
}
void
ods_log_warning(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_WARN, format, ap);
va_end(ap);
}
void
ods_log_error(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_ERROR, format, ap);
va_end(ap);
}
void
ods_log_verror(const char *format, va_list ap)
{
logger_vmessage(&logger_cls, logger_noctx, logger_ERROR, format, ap);
}
void
ods_log_crit(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_FATAL, format, ap);
va_end(ap);
}
void
ods_log_alert(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_ERROR, format, ap);
va_end(ap);
}
void
ods_fatal_exit(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logger_vmessage(&logger_cls, logger_noctx, logger_FATAL, format, ap);
va_end(ap);
abort();
}
#endif