merged tasking branch into trunk
[strongswan.git] / src / charon / bus / listeners / file_logger.c
1 /**
2 * @file file_logger.c
3 *
4 * @brief Implementation of file_logger_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "file_logger.h"
27
28
29 typedef struct private_file_logger_t private_file_logger_t;
30
31 /**
32 * Private data of a file_logger_t object
33 */
34 struct private_file_logger_t {
35
36 /**
37 * Public data.
38 */
39 file_logger_t public;
40
41 /**
42 * output file
43 */
44 FILE *out;
45
46 /**
47 * Maximum level to log
48 */
49 level_t levels[DBG_MAX];
50 };
51
52
53 /**
54 * Implementation of bus_listener_t.signal.
55 */
56 static bool signal_(private_file_logger_t *this, signal_t signal, level_t level,
57 int thread, ike_sa_t* ike_sa, char *format, va_list args)
58 {
59 if (level <= this->levels[SIG_TYPE(signal)])
60 {
61 char buffer[8192];
62 char *current = buffer, *next;
63
64 /* write in memory buffer first */
65 vsnprintf(buffer, sizeof(buffer), format, args);
66
67 /* prepend a prefix in front of every line */
68 while (current)
69 {
70 next = strchr(current, '\n');
71 if (next)
72 {
73 *(next++) = '\0';
74 }
75 fprintf(this->out, "%.2d[%N] %s\n", thread, signal_names, signal, current);
76 current = next;
77 }
78 }
79 /* always stay registered */
80 return TRUE;
81 }
82
83 /**
84 * Implementation of file_logger_t.set_level.
85 */
86 static void set_level(private_file_logger_t *this, signal_t signal, level_t level)
87 {
88 if (signal == SIG_ANY)
89 {
90 int i;
91 for (i = 0; i < DBG_MAX; i++)
92 {
93 this->levels[i] = level;
94 }
95 }
96 else
97 {
98
99 this->levels[SIG_TYPE(signal)] = level;
100 }
101 }
102
103 /**
104 * Implementation of file_logger_t.destroy.
105 */
106 static void destroy(private_file_logger_t *this)
107 {
108 free(this);
109 }
110
111 /*
112 * Described in header.
113 */
114 file_logger_t *file_logger_create(FILE *out)
115 {
116 private_file_logger_t *this = malloc_thing(private_file_logger_t);
117
118 /* public functions */
119 this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,char*,va_list))signal_;
120 this->public.set_level = (void(*)(file_logger_t*,signal_t,level_t))set_level;
121 this->public.destroy = (void(*)(file_logger_t*))destroy;
122
123 /* private variables */
124 this->out = out;
125 set_level(this, SIG_ANY, LEVEL_SILENT);
126
127 return &this->public;
128 }