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
377
378
379
380
381
382
383
384
|
/**
* @file logger.c
*
* @brief Implementation of logger_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <syslog.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include "logger.h"
/**
* Maximum length of a log entry (only used for logger_s.log).
*/
#define MAX_LOG 8192
/**
* Maximum number of logged bytes per line
*/
#define MAX_BYTES 16
typedef struct private_logger_t private_logger_t;
/**
* @brief Private data of a logger_t object.
*/
struct private_logger_t {
/**
* Public data.
*/
logger_t public;
/**
* Detail-level of logger.
*/
log_level_t level;
/**
* Name of logger.
*/
char *name;
/**
* File to write log output to.
* NULL for syslog.
*/
FILE *output;
/**
* Should a thread_id be included in the log?
*/
bool log_thread_id;
};
/**
* thread local storage for get_thread_number
*/
static pthread_key_t thread_ids;
static void make_key(void)
{
pthread_key_create(&thread_ids, NULL);
}
/**
* Get a unique thread number for a calling thread. Since
* pthread_self returns large and ugly numbers, use this function
* for logging; these numbers are incremental starting at 1
*/
static int get_thread_number(void)
{
static int current_num = 0;
static pthread_once_t key_once = PTHREAD_ONCE_INIT;
int stored_num;
pthread_once(&key_once, make_key);
stored_num = (int)pthread_getspecific(thread_ids);
if (stored_num == 0)
{
pthread_setspecific(thread_ids, (void*)++current_num);
return current_num;
}
else
{
return stored_num;
}
}
/**
* prepend the logging prefix to string and store it in buffer
*/
static void prepend_prefix(private_logger_t *this, log_level_t loglevel, const char *string, char *buffer)
{
char thread_id[3] = "";
char log_type, log_details;
char *separator = (strlen(this->name) == 0)? "" : ":";
if (loglevel & CONTROL)
{
log_type = 'C';
}
else if (loglevel & ERROR)
{
log_type = 'E';
}
else if (loglevel & RAW)
{
log_type = 'R';
}
else if (loglevel & PRIVATE)
{
log_type = 'P';
}
else if (loglevel & AUDIT)
{
log_type = 'A';
}
else
{
log_type = '-';
}
if (loglevel & (LEVEL3 - LEVEL2))
{
log_details = '3';
}
else if (loglevel & (LEVEL2 - LEVEL1))
{
log_details = '2';
}
else if (loglevel & LEVEL1)
{
log_details = '1';
}
else
{
log_details = '0';
}
if (this->log_thread_id)
{
snprintf(thread_id, sizeof(thread_id), "%02d", get_thread_number());
}
snprintf(buffer, MAX_LOG, "%s[%c%c%s%s] %s",
thread_id, log_type, log_details, separator, this->name, string);
}
/**
* Convert a charon-loglevel to a syslog priority
*/
static int get_priority(log_level_t loglevel)
{
if (loglevel & ERROR)
{
return LOG_AUTHPRIV|LOG_ERR;
}
if (loglevel & AUDIT)
{
return LOG_AUTHPRIV|LOG_INFO;
}
return LOG_AUTHPRIV|LOG_DEBUG;
}
/**
* Implementation of logger_t.logv.
*/
static void logv(private_logger_t *this, log_level_t loglevel, const char *format, va_list args)
{
if ((this->level & loglevel) == loglevel)
{
char buffer[MAX_LOG];
if (this->output == NULL)
{
/* syslog */
prepend_prefix(this, loglevel, format, buffer);
vsyslog(get_priority(loglevel), buffer, args);
}
else
{
/* File output */
prepend_prefix(this, loglevel, format, buffer);
vfprintf(this->output, buffer, args);
fprintf(this->output, "\n");
}
}
}
/**
* Implementation of logger_t.log.
*/
static void logg(private_logger_t *this, log_level_t loglevel, const char *format, ...)
{
va_list args;
va_start(args, format);
logv(this, loglevel, format, args);
va_end(args);
}
/**
* Implementation of logger_t.log_bytes.
*/
static void log_bytes(private_logger_t *this, log_level_t loglevel, const char *label, const char *bytes, size_t len)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
if ((this->level & loglevel) == loglevel)
{
char thread_id[3] = "";
char buffer[MAX_LOG];
char ascii_buffer[MAX_BYTES+1];
char *buffer_pos = buffer;
const char format[] = "%s %d bytes @ %p";
const char *bytes_pos = bytes;
const char *bytes_roof = bytes + len;
int line_start = 0;
int i = 0;
/* since me can't do multi-line output to syslog,
* we must do multiple syslogs. To avoid
* problems in output order, lock this by a mutex.
*/
pthread_mutex_lock(&mutex);
prepend_prefix(this, loglevel, format, buffer);
if (this->log_thread_id)
{
snprintf(thread_id, sizeof(thread_id), "%02d", get_thread_number());
}
if (this->output == NULL)
{
syslog(get_priority(loglevel), buffer, label, len, bytes);
}
else
{
fprintf(this->output, buffer, label, len, bytes);
fprintf(this->output, "\n");
}
while (bytes_pos < bytes_roof)
{
static char hexdig[] = "0123456789ABCDEF";
*buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
*buffer_pos++ = hexdig[ *bytes_pos & 0xF];
ascii_buffer[i++] = (*bytes_pos > 31 && *bytes_pos < 127)
? *bytes_pos : '.';
if (++bytes_pos == bytes_roof || i == MAX_BYTES)
{
int padding = 3 * (MAX_BYTES - i);
while (padding--)
{
*buffer_pos++ = ' ';
}
*buffer_pos++ = '\0';
ascii_buffer[i] = '\0';
if (this->output == NULL)
{
syslog(get_priority(loglevel), "%s[ :%5d] %s %s", thread_id, line_start, buffer, ascii_buffer);
}
else
{
fprintf(this->output, "%s[ :%5d] %s %s\n", thread_id, line_start, buffer, ascii_buffer);
}
buffer_pos = buffer;
line_start += MAX_BYTES;
i = 0;
}
else
{
*buffer_pos++ = ' ';
}
}
pthread_mutex_unlock(&mutex);
}
}
/**
* Implementation of logger_t.log_chunk.
*/
static void log_chunk(logger_t *this, log_level_t loglevel, const char *label, chunk_t chunk)
{
this->log_bytes(this, loglevel, label, chunk.ptr, chunk.len);
}
/**
* Implementation of logger_t.enable_level.
*/
static void enable_level(private_logger_t *this, log_level_t log_level)
{
this->level |= log_level;
}
/**
* Implementation of logger_t.disable_level.
*/
static void disable_level(private_logger_t *this, log_level_t log_level)
{
this->level &= ~log_level;
}
/**
* Implementation of logger_t.set_output.
*/
static void set_output(private_logger_t *this, FILE * output)
{
this->output = output;
}
/**
* Implementation of logger_t.get_level.
*/
static log_level_t get_level(private_logger_t *this)
{
return this->level;
}
/**
* Implementation of logger_t.destroy.
*/
static void destroy(private_logger_t *this)
{
free(this->name);
free(this);
}
/*
* Described in header.
*/
logger_t *logger_create(char *logger_name, log_level_t log_level, bool log_thread_id, FILE * output)
{
private_logger_t *this = malloc_thing(private_logger_t);
/* public functions */
this->public.log = (void(*)(logger_t*,log_level_t,const char*,...))logg;
this->public.logv = (void(*)(logger_t*,log_level_t,const char*,va_list))logv;
this->public.log_bytes = (void(*)(logger_t*, log_level_t, const char*, const char*,size_t))log_bytes;
this->public.log_chunk = log_chunk;
this->public.enable_level = (void(*)(logger_t*,log_level_t))enable_level;
this->public.disable_level = (void(*)(logger_t*,log_level_t))disable_level;
this->public.get_level = (log_level_t(*)(logger_t*))get_level;
this->public.set_output = (void(*)(logger_t*,FILE*))set_output;
this->public.destroy = (void(*)(logger_t*))destroy;
if (logger_name == NULL)
{
logger_name = "";
}
/* private variables */
this->level = log_level;
this->log_thread_id = log_thread_id;
this->name = malloc(strlen(logger_name) + 1);
strcpy(this->name,logger_name);
this->output = output;
return (logger_t*)this;
}
|