introduced an additional bus->signal parameter for signal specific data
[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
44 */
45 level_t levels[DBG_MAX];
46 };
47
48
49 /**
50 * Implementation of bus_listener_t.signal.
51 */
52 static bool signal_(private_sys_logger_t *this, signal_t signal, level_t level,
53 int thread, ike_sa_t* ike_sa, void *data,
54 char *format, va_list args)
55 {
56 if (level <= this->levels[SIG_TYPE(signal)])
57 {
58 char buffer[8192];
59 char *current = buffer, *next;
60
61 /* write in memory buffer first */
62 vsnprintf(buffer, sizeof(buffer), format, args);
63
64 /* do a syslog with every line */
65 while (current)
66 {
67 next = strchr(current, '\n');
68 if (next)
69 {
70 *(next++) = '\0';
71 }
72 syslog(this->facility|LOG_INFO, "%.2d[%N] %s\n",
73 thread, signal_names, signal, current);
74 current = next;
75 }
76 }
77 /* always stay registered */
78 return TRUE;
79 }
80
81 /**
82 * Implementation of sys_logger_t.set_level.
83 */
84 static void set_level(private_sys_logger_t *this, signal_t signal, level_t level)
85 {
86 if (signal == SIG_ANY)
87 {
88 int i;
89 for (i = 0; i < DBG_MAX; i++)
90 {
91 this->levels[i] = level;
92 }
93 }
94 else
95 {
96
97 this->levels[SIG_TYPE(signal)] = level;
98 }
99 }
100
101 /**
102 * Implementation of sys_logger_t.destroy.
103 */
104 static void destroy(private_sys_logger_t *this)
105 {
106 closelog();
107 free(this);
108 }
109
110 /*
111 * Described in header.
112 */
113 sys_logger_t *sys_logger_create(int facility)
114 {
115 private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
116
117 /* public functions */
118 this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,void*,char*,va_list))signal_;
119 this->public.set_level = (void(*)(sys_logger_t*,signal_t,level_t))set_level;
120 this->public.destroy = (void(*)(sys_logger_t*))destroy;
121
122 /* private variables */
123 this->facility = facility;
124 set_level(this, SIG_ANY, LEVEL_SILENT);
125
126 return &this->public;
127 }