4 * @brief Implementation of file_logger_t.
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
26 #include "file_logger.h"
29 typedef struct private_file_logger_t private_file_logger_t
;
32 * Private data of a file_logger_t object
34 struct private_file_logger_t
{
47 * Maximum level to log
49 level_t levels
[DBG_MAX
];
54 * Implementation of bus_listener_t.signal.
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
)
59 if (level
<= this->levels
[SIG_TYPE(signal
)])
62 char *current
= buffer
, *next
;
64 /* write in memory buffer first */
65 vsnprintf(buffer
, sizeof(buffer
), format
, args
);
67 /* prepend a prefix in front of every line */
70 next
= strchr(current
, '\n');
75 fprintf(this->out
, "%.2d[%N] %s\n", thread
, signal_names
, signal
, current
);
82 * Implementation of file_logger_t.set_level.
84 static void set_level(private_file_logger_t
*this, signal_t signal
, level_t level
)
86 if (signal
== SIG_ANY
)
89 for (i
= 0; i
< DBG_MAX
; i
++)
91 this->levels
[i
] = level
;
97 this->levels
[SIG_TYPE(signal
)] = level
;
102 * Implementation of file_logger_t.destroy.
104 static void destroy(private_file_logger_t
*this)
110 * Described in header.
112 file_logger_t
*file_logger_create(FILE *out
)
114 private_file_logger_t
*this = malloc_thing(private_file_logger_t
);
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
;
121 /* private variables */
123 set_level(this, SIG_ANY
, LEVEL_SILENT
);
125 return &this->public;