improved signal handling and emitting
[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 void 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 }
82
83 /**
84 * Implementation of sys_logger_t.set_level.
85 */
86 static void set_level(private_sys_logger_t *this, signal_t signal, level_t level)
87 {
88 if (signal == SIG_ANY)
89 {
90 int i;
91 for (i = 0; i < DBG_MAX; i++)
92 {
93 this->levels[i] = level;
94 }
95 }
96 else
97 {
98
99 this->levels[SIG_TYPE(signal)] = level;
100 }
101 }
102
103 /**
104 * Implementation of sys_logger_t.destroy.
105 */
106 static void destroy(private_sys_logger_t *this)
107 {
108 closelog();
109 free(this);
110 }
111
112 /*
113 * Described in header.
114 */
115 sys_logger_t *sys_logger_create(int facility)
116 {
117 private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
118
119 /* public functions */
120 this->public.listener.signal = (void(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,char*,va_list))signal_;
121 this->public.set_level = (void(*)(sys_logger_t*,signal_t,level_t))set_level;
122 this->public.destroy = (void(*)(sys_logger_t*))destroy;
123
124 /* private variables */
125 this->facility = facility;
126 set_level(this, SIG_ANY, LEVEL_SILENT);
127
128 return &this->public;
129 }