reintegrated bus-refactoring branch
[strongswan.git] / src / charon / bus / listeners / sys_logger.c
1 /*
2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <pthread.h>
21
22 #include "sys_logger.h"
23
24
25 typedef struct private_sys_logger_t private_sys_logger_t;
26
27 /**
28 * Private data of a sys_logger_t object
29 */
30 struct private_sys_logger_t {
31
32 /**
33 * Public data.
34 */
35 sys_logger_t public;
36
37 /**
38 * syslog facility to use
39 */
40 int facility;
41
42 /**
43 * Maximum level to log, for each group
44 */
45 level_t levels[DBG_MAX];
46 };
47
48 /**
49 * Implementation of listener_t.log.
50 */
51 static bool log_(private_sys_logger_t *this, debug_t group, level_t level,
52 int thread, ike_sa_t* ike_sa, char *format, va_list args)
53 {
54 if (level <= this->levels[group])
55 {
56 char buffer[8192];
57 char *current = buffer, *next;
58
59 /* write in memory buffer first */
60 vsnprintf(buffer, sizeof(buffer), format, args);
61
62 /* do a syslog with every line */
63 while (current)
64 {
65 next = strchr(current, '\n');
66 if (next)
67 {
68 *(next++) = '\0';
69 }
70 syslog(this->facility|LOG_INFO, "%.2d[%N] %s\n",
71 thread, debug_names, group, current);
72 current = next;
73 }
74 }
75 /* always stay registered */
76 return TRUE;
77 }
78
79 /**
80 * Implementation of sys_logger_t.set_level.
81 */
82 static void set_level(private_sys_logger_t *this, debug_t group, level_t level)
83 {
84 if (group < DBG_ANY)
85 {
86 this->levels[group] = level;
87 }
88 else
89 {
90 for (group = 0; group < DBG_MAX; group++)
91 {
92 this->levels[group] = level;
93 }
94 }
95 }
96
97 /**
98 * Implementation of sys_logger_t.destroy.
99 */
100 static void destroy(private_sys_logger_t *this)
101 {
102 closelog();
103 free(this);
104 }
105
106 /*
107 * Described in header.
108 */
109 sys_logger_t *sys_logger_create(int facility)
110 {
111 private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
112
113 /* public functions */
114 memset(&this->public.listener, 0, sizeof(listener_t));
115 this->public.listener.log = (bool(*)(listener_t*,debug_t,level_t,int,ike_sa_t*,char*,va_list))log_;
116 this->public.set_level = (void(*)(sys_logger_t*,debug_t,level_t))set_level;
117 this->public.destroy = (void(*)(sys_logger_t*))destroy;
118
119 /* private variables */
120 this->facility = facility;
121 set_level(this, DBG_ANY, LEVEL_SILENT);
122
123 return &this->public;
124 }