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