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