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