merged tasking branch into trunk
[strongswan.git] / src / charon / bus / listeners / sys_logger.c
1 /**
2 * @file sys_logger.c
3 *
4 * @brief Implementation of sys_logger_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 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 #include <stdio.h>
24 #include <string.h>
25 #include <pthread.h>
26
27 #include "sys_logger.h"
28
29
30 typedef struct private_sys_logger_t private_sys_logger_t;
31
32 /**
33 * Private data of a sys_logger_t object
34 */
35 struct private_sys_logger_t {
36
37 /**
38 * Public data.
39 */
40 sys_logger_t public;
41
42 /**
43 * syslog facility to use
44 */
45 int facility;
46
47 /**
48 * Maximum level to log
49 */
50 level_t levels[DBG_MAX];
51 };
52
53
54 /**
55 * Implementation of bus_listener_t.signal.
56 */
57 static bool signal_(private_sys_logger_t *this, signal_t signal, level_t level,
58 int thread, ike_sa_t* ike_sa, char *format, va_list args)
59 {
60 if (level <= this->levels[SIG_TYPE(signal)])
61 {
62 char buffer[8192];
63 char *current = buffer, *next;
64
65 /* write in memory buffer first */
66 vsnprintf(buffer, sizeof(buffer), format, args);
67
68 /* do a syslog with every line */
69 while (current)
70 {
71 next = strchr(current, '\n');
72 if (next)
73 {
74 *(next++) = '\0';
75 }
76 syslog(this->facility|LOG_INFO, "%.2d[%N] %s\n",
77 thread, signal_names, signal, current);
78 current = next;
79 }
80 }
81 /* always stay registered */
82 return TRUE;
83 }
84
85 /**
86 * Implementation of sys_logger_t.set_level.
87 */
88 static void set_level(private_sys_logger_t *this, signal_t signal, level_t level)
89 {
90 if (signal == SIG_ANY)
91 {
92 int i;
93 for (i = 0; i < DBG_MAX; i++)
94 {
95 this->levels[i] = level;
96 }
97 }
98 else
99 {
100
101 this->levels[SIG_TYPE(signal)] = level;
102 }
103 }
104
105 /**
106 * Implementation of sys_logger_t.destroy.
107 */
108 static void destroy(private_sys_logger_t *this)
109 {
110 closelog();
111 free(this);
112 }
113
114 /*
115 * Described in header.
116 */
117 sys_logger_t *sys_logger_create(int facility)
118 {
119 private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
120
121 /* public functions */
122 this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,char*,va_list))signal_;
123 this->public.set_level = (void(*)(sys_logger_t*,signal_t,level_t))set_level;
124 this->public.destroy = (void(*)(sys_logger_t*))destroy;
125
126 /* private variables */
127 this->facility = facility;
128 set_level(this, SIG_ANY, LEVEL_SILENT);
129
130 return &this->public;
131 }