typo
[strongswan.git] / src / charon / bus / listeners / file_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
21 #include "file_logger.h"
22
23
24 typedef struct private_file_logger_t private_file_logger_t;
25
26 /**
27 * Private data of a file_logger_t object
28 */
29 struct private_file_logger_t {
30
31 /**
32 * Public data.
33 */
34 file_logger_t public;
35
36 /**
37 * output file
38 */
39 FILE *out;
40
41 /**
42 * Maximum level to log
43 */
44 level_t levels[DBG_MAX];
45 };
46
47
48 /**
49 * Implementation of bus_listener_t.signal.
50 */
51 static bool signal_(private_file_logger_t *this, signal_t signal, level_t level,
52 int thread, ike_sa_t* ike_sa, char *format, va_list args)
53 {
54 if (level <= this->levels[SIG_TYPE(signal)])
55 {
56 char buffer[8192];
57 char *current = buffer, *next;
58
59 /* write in memory buffer first */
60 vsnprintf(buffer, sizeof(buffer), format, args);
61
62 /* prepend a prefix in front of every line */
63 while (current)
64 {
65 next = strchr(current, '\n');
66 if (next)
67 {
68 *(next++) = '\0';
69 }
70 fprintf(this->out, "%.2d[%N] %s\n", thread, signal_names, signal, current);
71 current = next;
72 }
73 }
74 /* always stay registered */
75 return TRUE;
76 }
77
78 /**
79 * Implementation of file_logger_t.set_level.
80 */
81 static void set_level(private_file_logger_t *this, signal_t signal, level_t level)
82 {
83 if (signal == SIG_ANY)
84 {
85 int i;
86 for (i = 0; i < DBG_MAX; i++)
87 {
88 this->levels[i] = level;
89 }
90 }
91 else
92 {
93
94 this->levels[SIG_TYPE(signal)] = level;
95 }
96 }
97
98 /**
99 * Implementation of file_logger_t.destroy.
100 */
101 static void destroy(private_file_logger_t *this)
102 {
103 free(this);
104 }
105
106 /*
107 * Described in header.
108 */
109 file_logger_t *file_logger_create(FILE *out)
110 {
111 private_file_logger_t *this = malloc_thing(private_file_logger_t);
112
113 /* public functions */
114 this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,char*,va_list))signal_;
115 this->public.set_level = (void(*)(file_logger_t*,signal_t,level_t))set_level;
116 this->public.destroy = (void(*)(file_logger_t*))destroy;
117
118 /* private variables */
119 this->out = out;
120 set_level(this, SIG_ANY, LEVEL_SILENT);
121
122 return &this->public;
123 }