../svn-commit.tmp
[strongswan.git] / Source / lib / utils / logger_manager.c
1 /**
2 * @file logger_manager.c
3 *
4 * @brief Implementation of logger_manager_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, 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
24 #include "logger_manager.h"
25
26 #include <daemon.h>
27 #include <definitions.h>
28 #include <utils/allocator.h>
29 #include <utils/linked_list.h>
30
31 /**
32 * String mappings for logger_context_t
33 */
34 mapping_t logger_context_t_mappings[] = {
35 {PARSER, "PARSER"},
36 {GENERATOR, "GENERATOR"},
37 {IKE_SA, "IKE_SA"},
38 {IKE_SA_MANAGER, "IKE_SA_MANAGER"},
39 {CHILD_SA, "CHILD_SA"},
40 {MESSAGE, "MESSAGE"},
41 {THREAD_POOL, "THREAD_POOL"},
42 {WORKER, "WORKER"},
43 {SCHEDULER, "SCHEDULER"},
44 {SENDER, "SENDER"},
45 {RECEIVER, "RECEIVER"},
46 {SOCKET, "SOCKET"},
47 {TESTER, "TESTER"},
48 {DAEMON, "DAEMON"},
49 {CONFIG, "CONFIG"},
50 {ENCRYPTION_PAYLOAD, "ENCRYPTION_PAYLOAD"},
51 {PAYLOAD, "PAYLOAD"},
52 {DER_DECODER, "DER_DECODER"},
53 {DER_ENCODER, "DER_ENCODER"},
54 {MAPPING_END, NULL},
55 };
56
57 #define DEFAULT_OUTPUT NULL
58
59 struct {
60 char *name;
61 log_level_t level;
62 bool log_thread_ids;
63 FILE *output;
64 } logger_defaults[] = {
65 { "PARSR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* PARSER */
66 { "GNRAT", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* GENERATOR */
67 { "IKESA", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* IKE_SA */
68 { "SAMGR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* IKE_SA_MANAGER */
69 { "CHDSA", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* CHILD_SA */
70 { "MESSG", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* MESSAGE */
71 { "TPOOL", ERROR|CONTROL|AUDIT|LEVEL0, FALSE, DEFAULT_OUTPUT}, /* THREAD_POOL */
72 { "WORKR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* WORKER */
73 { "SCHED", ERROR|CONTROL|AUDIT|LEVEL0, FALSE, DEFAULT_OUTPUT}, /* SCHEDULER */
74 { "SENDR", ERROR|CONTROL|AUDIT|LEVEL0, FALSE, DEFAULT_OUTPUT}, /* SENDER */
75 { "RECVR", ERROR|CONTROL|AUDIT|LEVEL0, FALSE, DEFAULT_OUTPUT}, /* RECEIVER */
76 { "SOCKT", ERROR|CONTROL|AUDIT|LEVEL0, FALSE, DEFAULT_OUTPUT}, /* SOCKET */
77 { "TESTR", ERROR|CONTROL|AUDIT|LEVEL0, FALSE, DEFAULT_OUTPUT}, /* TESTER */
78 { "DAEMN", ERROR|CONTROL|AUDIT|LEVEL0, FALSE, DEFAULT_OUTPUT}, /* DAEMON */
79 { "CONFG", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* CONFIG */
80 { "ENCPL", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* ENCRYPTION_PAYLOAD */
81 { "PAYLD", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* PAYLOAD */
82 { "DERDC", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* DER_DECODER */
83 { "DEREC", ERROR|CONTROL|AUDIT|LEVEL0, TRUE, DEFAULT_OUTPUT}, /* DER_ENCODER */
84 };
85
86
87 typedef struct private_logger_manager_t private_logger_manager_t;
88
89 /**
90 * Private data of logger_manager_t object.
91 */
92 struct private_logger_manager_t {
93 /**
94 * Public data.
95 */
96 logger_manager_t public;
97
98 /**
99 * Array of loggers, one for each context
100 */
101 logger_t *loggers[LOGGER_CONTEXT_ROOF];
102
103 };
104
105 /**
106 * Implementation of logger_manager_t.get_logger.
107 */
108 static logger_t *get_logger(private_logger_manager_t *this, logger_context_t context)
109 {
110 return this->loggers[context];
111 }
112
113 /**
114 * Implementation of logger_manager_t.get_log_level.
115 */
116 static log_level_t get_log_level (private_logger_manager_t *this, logger_context_t context)
117 {
118 return this->loggers[context]->get_level(this->loggers[context]);
119 }
120
121 /**
122 * Implementation of private_logger_manager_t.enable_log_level.
123 */
124 static void enable_log_level(private_logger_manager_t *this, logger_context_t context, log_level_t level)
125 {
126 if (context == ALL_LOGGERS)
127 {
128 for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
129 {
130 this->loggers[context]->enable_level(this->loggers[context], level);
131 }
132 }
133 else
134 {
135 this->loggers[context]->enable_level(this->loggers[context], level);
136 }
137 }
138
139 /**
140 * Implementation of private_logger_manager_t.disable_log_level.
141 */
142 static void disable_log_level(private_logger_manager_t *this, logger_context_t context, log_level_t level)
143 {
144 if (context == ALL_LOGGERS)
145 {
146 for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
147 {
148 this->loggers[context]->disable_level(this->loggers[context], level);
149 }
150 }
151 else
152 {
153 this->loggers[context]->disable_level(this->loggers[context], level);
154 }
155 }
156
157 /**
158 * Implementation of private_logger_manager_t.set_output.
159 */
160 static void set_output(private_logger_manager_t *this, logger_context_t context, FILE *output)
161 {
162 if (context == ALL_LOGGERS)
163 {
164 for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
165 {
166 this->loggers[context]->set_output(this->loggers[context], output);
167 }
168 }
169 else
170 {
171 this->loggers[context]->set_output(this->loggers[context], output);
172 }
173 }
174
175
176 /**
177 * Implementation of logger_manager_t.destroy.
178 */
179 static void destroy(private_logger_manager_t *this)
180 {
181 int i;
182 for (i = 0; i < LOGGER_CONTEXT_ROOF; i++)
183 {
184 this->loggers[i]->destroy(this->loggers[i]);
185 }
186 allocator_free(this);
187 }
188
189 /*
190 * Described in header.
191 */
192 logger_manager_t *logger_manager_create(log_level_t default_log_level)
193 {
194 private_logger_manager_t *this = allocator_alloc_thing(private_logger_manager_t);
195 int i;
196
197 this->public.get_logger = (logger_t *(*)(logger_manager_t*,logger_context_t context))get_logger;
198 this->public.get_log_level = (log_level_t (*)(logger_manager_t *, logger_context_t)) get_log_level;
199 this->public.enable_log_level = (void (*)(logger_manager_t *, logger_context_t, log_level_t)) enable_log_level;
200 this->public.disable_log_level = (void (*)(logger_manager_t *, logger_context_t, log_level_t)) disable_log_level;
201 this->public.set_output = (void (*)(logger_manager_t *, logger_context_t, FILE*)) set_output;
202 this->public.destroy = (void(*)(logger_manager_t*))destroy;
203
204 for (i = 0; i < LOGGER_CONTEXT_ROOF; i++)
205 {
206 this->loggers[i] = logger_create(logger_defaults[i].name, logger_defaults[i].level,
207 logger_defaults[i].log_thread_ids, stdout);//logger_defaults[i].output);
208 }
209
210 return &this->public;
211 }
212