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