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