Removed strayed code fragment
[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
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "file_logger.h"
20
21
22 typedef struct private_file_logger_t private_file_logger_t;
23
24 /**
25 * Private data of a file_logger_t object
26 */
27 struct private_file_logger_t {
28
29 /**
30 * Public data.
31 */
32 file_logger_t public;
33
34 /**
35 * output file
36 */
37 FILE *out;
38
39 /**
40 * Maximum level to log, for each group
41 */
42 level_t levels[DBG_MAX];
43 };
44
45 /**
46 * Implementation of bus_listener_t.log.
47 */
48 static bool log_(private_file_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 /* prepend a prefix in front of every line */
60 while (current)
61 {
62 next = strchr(current, '\n');
63 if (next)
64 {
65 *(next++) = '\0';
66 }
67 fprintf(this->out, "%.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 file_logger_t.set_level.
78 */
79 static void set_level(private_file_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 file_logger_t.destroy.
96 */
97 static void destroy(private_file_logger_t *this)
98 {
99 if (this->out != stdout && this->out != stderr)
100 {
101 fclose(this->out);
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 memset(&this->public.listener, 0, sizeof(listener_t));
115 this->public.listener.log = (bool(*)(listener_t*,debug_t,level_t,int,ike_sa_t*,char*,va_list))log_;
116 this->public.set_level = (void(*)(file_logger_t*,debug_t,level_t))set_level;
117 this->public.destroy = (void(*)(file_logger_t*))destroy;
118
119 /* private variables */
120 this->out = out;
121 set_level(this, DBG_ANY, LEVEL_SILENT);
122
123 return &this->public;
124 }
125