2 * Copyright (C) 2006 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
21 #include "file_logger.h"
24 typedef struct private_file_logger_t private_file_logger_t
;
27 * Private data of a file_logger_t object
29 struct private_file_logger_t
{
42 * Maximum level to log
44 level_t levels
[DBG_MAX
];
49 * Implementation of bus_listener_t.signal.
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
)
54 if (level
<= this->levels
[SIG_TYPE(signal
)])
57 char *current
= buffer
, *next
;
59 /* write in memory buffer first */
60 vsnprintf(buffer
, sizeof(buffer
), format
, args
);
62 /* prepend a prefix in front of every line */
65 next
= strchr(current
, '\n');
70 fprintf(this->out
, "%.2d[%N] %s\n", thread
, signal_names
, signal
, current
);
74 /* always stay registered */
79 * Implementation of file_logger_t.set_level.
81 static void set_level(private_file_logger_t
*this, signal_t signal
, level_t level
)
83 if (signal
== SIG_ANY
)
86 for (i
= 0; i
< DBG_MAX
; i
++)
88 this->levels
[i
] = level
;
94 this->levels
[SIG_TYPE(signal
)] = level
;
99 * Implementation of file_logger_t.destroy.
101 static void destroy(private_file_logger_t
*this)
107 * Described in header.
109 file_logger_t
*file_logger_create(FILE *out
)
111 private_file_logger_t
*this = malloc_thing(private_file_logger_t
);
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
;
118 /* private variables */
120 set_level(this, SIG_ANY
, LEVEL_SILENT
);
122 return &this->public;