Added a get_sa() method to the bus, allowing a thread to lookup its IKE_SA
[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
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "sys_logger.h"
20
21
22 typedef struct private_sys_logger_t private_sys_logger_t;
23
24 /**
25 * Private data of a sys_logger_t object
26 */
27 struct private_sys_logger_t {
28
29 /**
30 * Public data.
31 */
32 sys_logger_t public;
33
34 /**
35 * syslog facility to use
36 */
37 int facility;
38
39 /**
40 * Maximum level to log, for each group
41 */
42 level_t levels[DBG_MAX];
43 };
44
45 /**
46 * Implementation of listener_t.log.
47 */
48 static bool log_(private_sys_logger_t *this, debug_t group, level_t level,
49 int thread, ike_sa_t* ike_sa, char *format, va_list args)
50 {
51 if (level <= this->levels[group])
52 {
53 char buffer[8192];
54 char *current = buffer, *next;
55
56 /* write in memory buffer first */
57 vsnprintf(buffer, sizeof(buffer), format, args);
58
59 /* do a syslog with every line */
60 while (current)
61 {
62 next = strchr(current, '\n');
63 if (next)
64 {
65 *(next++) = '\0';
66 }
67 syslog(this->facility|LOG_INFO, "%.2d[%N] %s\n",
68 thread, debug_names, group, current);
69 current = next;
70 }
71 }
72 /* always stay registered */
73 return TRUE;
74 }
75
76 /**
77 * Implementation of sys_logger_t.set_level.
78 */
79 static void set_level(private_sys_logger_t *this, debug_t group, level_t level)
80 {
81 if (group < DBG_ANY)
82 {
83 this->levels[group] = level;
84 }
85 else
86 {
87 for (group = 0; group < DBG_MAX; group++)
88 {
89 this->levels[group] = level;
90 }
91 }
92 }
93
94 /**
95 * Implementation of sys_logger_t.destroy.
96 */
97 static void destroy(private_sys_logger_t *this)
98 {
99 closelog();
100 free(this);
101 }
102
103 /*
104 * Described in header.
105 */
106 sys_logger_t *sys_logger_create(int facility)
107 {
108 private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
109
110 /* public functions */
111 memset(&this->public.listener, 0, sizeof(listener_t));
112 this->public.listener.log = (bool(*)(listener_t*,debug_t,level_t,int,ike_sa_t*,char*,va_list))log_;
113 this->public.set_level = (void(*)(sys_logger_t*,debug_t,level_t))set_level;
114 this->public.destroy = (void(*)(sys_logger_t*))destroy;
115
116 /* private variables */
117 this->facility = facility;
118 set_level(this, DBG_ANY, LEVEL_SILENT);
119
120 return &this->public;
121 }