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