9ce8397b32fa9715e6d7982655c400573e1e2b83
[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, for each group
43 */
44 level_t levels[DBG_MAX];
45 };
46
47 /**
48 * Implementation of bus_listener_t.log.
49 */
50 static bool log_(private_file_logger_t *this, debug_t group, level_t level,
51 int thread, ike_sa_t* ike_sa, char *format, va_list args)
52 {
53 if (level <= this->levels[group])
54 {
55 char buffer[8192];
56 char *current = buffer, *next;
57
58 /* write in memory buffer first */
59 vsnprintf(buffer, sizeof(buffer), format, args);
60
61 /* prepend a prefix in front of every line */
62 while (current)
63 {
64 next = strchr(current, '\n');
65 if (next)
66 {
67 *(next++) = '\0';
68 }
69 fprintf(this->out, "%.2d[%N] %s\n",
70 thread, debug_names, group, 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, debug_t group, level_t level)
82 {
83 if (group < DBG_ANY)
84 {
85 this->levels[group] = level;
86 }
87 else
88 {
89 for (group = 0; group < DBG_MAX; group++)
90 {
91 this->levels[group] = level;
92 }
93 }
94 }
95
96 /**
97 * Implementation of file_logger_t.destroy.
98 */
99 static void destroy(private_file_logger_t *this)
100 {
101 free(this);
102 }
103
104 /*
105 * Described in header.
106 */
107 file_logger_t *file_logger_create(FILE *out)
108 {
109 private_file_logger_t *this = malloc_thing(private_file_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(*)(file_logger_t*,debug_t,level_t))set_level;
115 this->public.destroy = (void(*)(file_logger_t*))destroy;
116
117 /* private variables */
118 this->out = out;
119 set_level(this, DBG_ANY, LEVEL_SILENT);
120
121 return &this->public;
122 }
123