introduced printf() specifiers for:
[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 /* for open_memstream() */
24 #define _GNU_SOURCE
25 #include <stdio.h>
26 #include <string.h>
27 #include <pthread.h>
28
29 #include "sys_logger.h"
30
31 #include <bus/listeners/stream_logger.h>
32
33
34 typedef struct private_sys_logger_t private_sys_logger_t;
35
36 /**
37 * Private data of a sys_logger_t object
38 */
39 struct private_sys_logger_t {
40
41 /**
42 * Public data.
43 */
44 sys_logger_t public;
45
46 /**
47 * syslog facility to use
48 */
49 int facility;
50
51 /**
52 * Internal used stream logger that does the dirty work
53 */
54 stream_logger_t *logger;
55
56 /**
57 * Memory stream used for stream_logger
58 */
59 FILE *stream;
60
61 /**
62 * Underlying buffer for stream
63 */
64 char buffer[4096];
65 };
66
67
68 /**
69 * Implementation of bus_listener_t.signal.
70 */
71 static void signal_(private_sys_logger_t *this, int thread, ike_sa_t* ike_sa,
72 signal_t signal, level_t level,
73 char *format, va_list args)
74 {
75 char line[512];
76 char *prefix;
77 FILE *reader;
78
79 switch (signal)
80 {
81 case SIG_IKE_UP:
82 case SIG_IKE_DOWN:
83 case SIG_IKE_REKEY:
84 case SIG_DBG_IKE:
85 prefix = "IKE";
86 break;
87 case SIG_DBG_CHD:
88 prefix = "CHD";
89 break;
90 case SIG_DBG_JOB:
91 prefix = "JOG";
92 break;
93 case SIG_DBG_CFG:
94 prefix = "CFG";
95 break;
96 case SIG_DBG_KNL:
97 prefix = "KNL";
98 break;
99 case SIG_DBG_NET:
100 prefix = "NET";
101 break;
102 case SIG_DBG_ENC:
103 prefix = "ENC";
104 break;
105 default:
106 prefix = "???";
107 break;
108 }
109
110 flockfile(this->stream);
111 /* reset memory stream */
112 rewind(this->stream);
113 memset(this->buffer, '\0', sizeof(this->buffer));
114 /* log to memstream */
115 this->logger->listener.signal(&this->logger->listener, thread, ike_sa,
116 signal, level, format, args);
117 /* flush is needed to append a '\0' */
118 fflush(this->stream);
119
120 /* create a reader stream that reads out line by line */
121 reader = fmemopen(this->buffer, sizeof(this->buffer), "r");
122
123 while (fgets(line, sizeof(line), reader))
124 {
125 if (line[0] == '\0')
126 {
127 /* abort on EOF */
128 break;
129 }
130 else if (line[0] != '\n')
131 {
132 syslog(this->facility|LOG_INFO, "%.2d[%s] %s", thread, prefix, line);
133 }
134 }
135 fclose(reader);
136 funlockfile(this->stream);
137 }
138
139 /**
140 * Implementation of sys_logger_t.set_level.
141 */
142 static void set_level(private_sys_logger_t *this, signal_t signal, level_t max)
143 {
144 this->logger->set_level(this->logger, signal, max);
145 }
146
147 /**
148 * Implementation of sys_logger_t.destroy.
149 */
150 static void destroy(private_sys_logger_t *this)
151 {
152 closelog();
153 fclose(this->stream);
154 this->logger->destroy(this->logger);
155 free(this);
156 }
157
158 /*
159 * Described in header.
160 */
161 sys_logger_t *sys_logger_create(int facility)
162 {
163 private_sys_logger_t *this = malloc_thing(private_sys_logger_t);
164
165 /* public functions */
166 this->public.listener.signal = (void(*)(bus_listener_t*,int,ike_sa_t*,signal_t,level_t,char*,va_list))signal_;
167 this->public.set_level = (void(*)(sys_logger_t*,signal_t,level_t))set_level;
168 this->public.destroy = (void(*)(sys_logger_t*))destroy;
169
170 /* private variables */
171 this->facility = facility;
172 this->stream = fmemopen(this->buffer, sizeof(this->buffer), "w");
173 if (this->stream == NULL)
174 {
175 /* fallback to stderr */
176 this->stream = stderr;
177 }
178 this->logger = stream_logger_create(this->stream);
179
180 return &this->public;
181 }