4399b93fbbc5092118eb13ccea6b4500e7b03ce4
[strongswan.git] / Source / charon / 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 <definitions.h>
27 #include <utils/allocator.h>
28 #include <utils/linked_list.h>
29
30 mapping_t logger_context_t_mappings[] = {
31 {PARSER, "PARSER"},
32 {GENERATOR, "GENRAT"},
33 {IKE_SA, "IKE_SA"},
34 {IKE_SA_MANAGER, "ISAMGR"},
35 {MESSAGE, "MESSAG"},
36 {THREAD_POOL, "THPOOL"},
37 {WORKER, "WORKER"},
38 {SCHEDULER, "SCHEDU"},
39 {SENDER, "SENDER"},
40 {RECEIVER, "RECEVR"},
41 {SOCKET, "SOCKET"},
42 {TESTER, "TESTER"},
43 {DAEMON, "DAEMON"},
44 {CONFIGURATION_MANAGER, "CONFIG"},
45 };
46
47 /**
48 * Maximum length of a logger name
49 */
50 #define MAX_LOGGER_NAME 45
51
52
53 typedef struct private_logger_manager_t private_logger_manager_t;
54
55 /**
56 * Private data of logger_manager_t object.
57 */
58 struct private_logger_manager_t {
59 /**
60 * Public data.
61 */
62 logger_manager_t public;
63
64 /**
65 * Managed loggers.
66 */
67 linked_list_t *loggers;
68
69 /**
70 * Log Levels.
71 */
72 linked_list_t *logger_levels;
73
74 /**
75 * Used to manage logger list.
76 */
77 pthread_mutex_t mutex;
78
79 /**
80 * Default logger level for a created logger used
81 * if no specific logger_level is set.
82 */
83 logger_level_t default_log_level;
84
85 /**
86 * Sets set logger_level of a specific context.
87 *
88 * @param this calling object
89 * @param context context to set level
90 * @param logger_level logger_level to set
91 * @param enable enable specific level or disable it
92 * @return
93 * - SUCCESS
94 * - OUT_OF_RES
95 */
96 status_t (*set_logger_level) (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level,bool enable);
97
98 };
99
100
101 typedef struct logger_levels_entry_t logger_levels_entry_t;
102
103 /**
104 * Entry in the logger_levels linked list.
105 *
106 * This entry specifies the current log level for
107 * logger_t objects in specific context.
108 */
109 struct logger_levels_entry_t {
110 logger_context_t context;
111 logger_level_t level;
112 };
113
114 typedef struct loggers_entry_t loggers_entry_t;
115
116 /**
117 * Entry in the loggers linked list.
118 */
119 struct loggers_entry_t {
120 logger_context_t context;
121 logger_t *logger;
122 };
123
124 /**
125 * Implementation of logger_manager_t.create_logger.
126 */
127 static logger_t *create_logger(private_logger_manager_t *this, logger_context_t context, char * name)
128 {
129
130 char * context_name;
131 bool log_thread_ids = TRUE;
132 FILE * output = NULL;
133 char buffer[MAX_LOGGER_NAME];
134 loggers_entry_t *entry;
135 logger_t *logger;
136 logger_level_t logger_level;
137
138 context_name = mapping_find(logger_context_t_mappings,context);
139
140 /* output to stdout, since we are debugging all days */
141 output = stdout;
142
143 switch(context)
144 {
145 case TESTER:
146 log_thread_ids = FALSE;
147 output = stdout;
148 logger_level = FULL;
149 break;
150 case PARSER:
151 case GENERATOR:
152 case IKE_SA:
153 case IKE_SA_MANAGER:
154 case MESSAGE:
155 case THREAD_POOL:
156 case WORKER:
157 case SCHEDULER:
158 case SENDER:
159 case RECEIVER:
160 case SOCKET:
161 case DAEMON:
162 case CONFIGURATION_MANAGER:
163 log_thread_ids = FALSE;
164 logger_level = ERROR|CONTROL;
165 break;
166 }
167
168
169 /* reduce to global definiton of loglevel */
170 logger_level &= this->public.get_logger_level(&(this->public),context);
171
172 /* logger manager is thread save */
173 pthread_mutex_lock(&(this->mutex));
174 if (name != NULL)
175 {
176 snprintf(buffer, MAX_LOGGER_NAME, "%s - %s",context_name,name);
177 /* create logger with default log_level */
178 logger = logger_create(buffer,logger_level,log_thread_ids,output);
179 }
180 else
181 {
182 logger = logger_create(context_name,logger_level,log_thread_ids,output);
183 }
184
185
186 if (logger == NULL)
187 {
188 pthread_mutex_unlock(&(this->mutex));
189 return NULL;
190 }
191
192 entry = allocator_alloc_thing(loggers_entry_t);
193
194 if (entry == NULL)
195 {
196 logger->destroy(logger);
197 pthread_mutex_unlock(&(this->mutex));
198 return NULL;
199 }
200
201 entry->context = context;
202 entry->logger = logger;
203
204 if (this->loggers->insert_last(this->loggers,entry) != SUCCESS)
205 {
206 allocator_free(entry);
207 logger->destroy(logger);
208 pthread_mutex_unlock(&(this->mutex));
209 return NULL;
210 }
211
212 pthread_mutex_unlock(&(this->mutex));
213 return logger;
214
215 }
216
217 /**
218 * Implementation of logger_manager_t.get_logger_level.
219 */
220 static logger_level_t get_logger_level (private_logger_manager_t *this, logger_context_t context)
221 {
222 iterator_t *iterator;
223 /* set logger_level to default logger_level */
224 logger_level_t logger_level = this->default_log_level;
225
226 pthread_mutex_lock(&(this->mutex));
227
228 if (this->logger_levels->create_iterator(this->logger_levels,&iterator,TRUE) != SUCCESS)
229 {
230 pthread_mutex_unlock(&(this->mutex));
231 return logger_level;
232 }
233
234 /* check for existing logger_level entry */
235 while (iterator->has_next(iterator))
236 {
237
238 logger_levels_entry_t * entry;
239 if (iterator->current(iterator,(void **)&entry) != SUCCESS)
240 {
241 break;
242 }
243 if (entry->context == context)
244 {
245 logger_level = entry->level;
246 break;
247 }
248 }
249
250 iterator->destroy(iterator);
251
252 pthread_mutex_unlock(&(this->mutex));
253 return logger_level;
254 }
255
256 /**
257 * Implementation of logger_manager_t.destroy_logger.
258 */
259 static status_t destroy_logger (private_logger_manager_t *this,logger_t *logger)
260 {
261
262 iterator_t *iterator;
263 status_t status = NOT_FOUND;
264
265 pthread_mutex_lock(&(this->mutex));
266 if (this->loggers->create_iterator(this->loggers,&iterator,TRUE) != SUCCESS)
267 {
268 pthread_mutex_unlock(&(this->mutex));
269 return OUT_OF_RES;
270 }
271
272 while (iterator->has_next(iterator))
273 {
274
275 loggers_entry_t * entry;
276 status = iterator->current(iterator,(void **)&entry);
277 if (status != SUCCESS)
278 {
279 break;
280 }
281 status = NOT_FOUND;
282 if (entry->logger == logger)
283 {
284 iterator->remove(iterator);
285 allocator_free(entry);
286 logger->destroy(logger);
287 status = SUCCESS;
288 break;
289 }
290 }
291 iterator->destroy(iterator);
292 pthread_mutex_unlock(&(this->mutex));
293 return status;
294 }
295
296 /**
297 * Implementation of private_logger_manager_t.set_logger_level.
298 */
299 static status_t set_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level,bool enable)
300 {
301 iterator_t *iterator;
302 status_t status;
303
304 pthread_mutex_lock(&(this->mutex));
305 if (this->logger_levels->create_iterator(this->logger_levels,&iterator,TRUE) != SUCCESS)
306 {
307 pthread_mutex_unlock(&(this->mutex));
308 return OUT_OF_RES;
309 }
310
311 status = NOT_FOUND;
312 /* find existing logger_level entry */
313 while (iterator->has_next(iterator))
314 {
315 logger_levels_entry_t * entry;
316 status = iterator->current(iterator,(void **)&entry);
317 if (status != SUCCESS)
318 {
319 iterator->destroy(iterator);
320 pthread_mutex_unlock(&(this->mutex));
321 return status;
322 }
323 status = NOT_FOUND;
324 if (entry->context == context)
325 {
326 if (enable)
327 {
328 entry->level |= logger_level;
329 }
330 else
331 {
332 entry->level &= ~logger_level;
333 }
334
335 status = SUCCESS;
336 break;
337 }
338 }
339 iterator->destroy(iterator);
340
341 if (status == NOT_FOUND)
342 {
343 /* logger_levels entry not existing for current context */
344 logger_levels_entry_t *entry = allocator_alloc_thing(logger_levels_entry_t);
345 if (entry == NULL)
346 {
347 pthread_mutex_unlock(&(this->mutex));
348 return OUT_OF_RES;
349 }
350 entry->context = context;
351 entry->level = (enable) ? logger_level : (this->default_log_level & (~logger_level));
352
353 status = this->logger_levels->insert_last(this->logger_levels,entry);
354 if (status != SUCCESS)
355 {
356 allocator_free(entry);
357 pthread_mutex_unlock(&(this->mutex));
358 return status;
359 }
360 }
361
362 if (this->loggers->create_iterator(this->loggers,&iterator,TRUE) != SUCCESS)
363 {
364 pthread_mutex_unlock(&(this->mutex));
365 return OUT_OF_RES;
366 }
367
368 while (iterator->has_next(iterator))
369 {
370
371 loggers_entry_t * entry;
372 status = iterator->current(iterator,(void **)&entry);
373 if (status != SUCCESS)
374 {
375 iterator->destroy(iterator);
376 pthread_mutex_unlock(&(this->mutex));
377 return status;
378 }
379 if (entry->context == context)
380 {
381 if (enable)
382 {
383 status = entry->logger->enable_level(entry->logger,logger_level);
384 }
385 else
386 {
387 status = entry->logger->disable_level(entry->logger,logger_level);
388 }
389
390 }
391 }
392
393 iterator->destroy(iterator);
394 pthread_mutex_unlock(&(this->mutex));
395 return SUCCESS;
396 }
397
398 /**
399 * Implementation of logger_manager_t.enable_logger_level.
400 */
401 static status_t enable_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level)
402 {
403 return set_logger_level(this,context,logger_level,TRUE);
404 }
405
406 /**
407 * Implementation of logger_manager_t.disable_logger_level.
408 */
409 static status_t disable_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level)
410 {
411 return set_logger_level(this,context,logger_level,FALSE);
412 }
413
414 /**
415 * Implementation of logger_manager_t.destroy.
416 */
417 static status_t destroy(private_logger_manager_t *this)
418 {
419
420 while (this->loggers->get_count(this->loggers) > 0)
421 {
422 loggers_entry_t *current_entry;
423
424 this->loggers->remove_first(this->loggers,(void **)&current_entry);
425
426 /* destroy logger object */
427 current_entry->logger->destroy(current_entry->logger);
428
429 /* entry can be destroyed */
430 allocator_free(current_entry);
431 }
432
433 while (this->logger_levels->get_count(this->logger_levels) > 0)
434 {
435 logger_levels_entry_t *current_entry;
436
437 this->logger_levels->remove_first(this->logger_levels,(void **)&current_entry);
438
439 /* entry can be destroyed */
440 allocator_free(current_entry);
441 }
442
443 this->loggers->destroy(this->loggers);
444 this->logger_levels->destroy(this->logger_levels);
445 pthread_mutex_destroy(&(this->mutex));
446
447 allocator_free(this);
448 return SUCCESS;
449 }
450
451 /*
452 * Described in header.
453 */
454 logger_manager_t *logger_manager_create(logger_level_t default_log_level)
455 {
456 private_logger_manager_t *this = allocator_alloc_thing(private_logger_manager_t);
457
458 if (this == NULL)
459 {
460 return NULL;
461 }
462
463 this->public.create_logger = (logger_t *(*)(logger_manager_t*,logger_context_t context, char *))create_logger;
464 this->public.destroy_logger = (status_t(*)(logger_manager_t*,logger_t *logger))destroy_logger;
465 this->public.destroy = (status_t(*)(logger_manager_t*))destroy;
466 this->public.get_logger_level = (logger_level_t (*)(logger_manager_t *, logger_context_t)) get_logger_level;
467 this->public.enable_logger_level = (status_t (*)(logger_manager_t *, logger_context_t,logger_level_t)) enable_logger_level;
468 this->public.disable_logger_level = (status_t (*)(logger_manager_t *, logger_context_t,logger_level_t)) disable_logger_level;
469 this->set_logger_level = (status_t (*)(private_logger_manager_t *, logger_context_t,logger_level_t,bool)) set_logger_level;
470
471 /* private variables */
472 this->loggers = linked_list_create();
473
474 if (this->loggers == NULL)
475 {
476 allocator_free(this);
477 return NULL;
478 }
479 this->logger_levels = linked_list_create();
480 if (this->logger_levels == NULL)
481 {
482 this->loggers->destroy(this->loggers);
483 allocator_free(this);
484 return NULL;
485 }
486 this->default_log_level = default_log_level;
487
488 pthread_mutex_init(&(this->mutex), NULL);
489
490 return (logger_manager_t*)this;
491 }
492