4 * @brief Implementation of sys_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
27 #include "sys_logger.h"
30 typedef struct private_sys_logger_t private_sys_logger_t
;
33 * Private data of a sys_logger_t object
35 struct private_sys_logger_t
{
43 * syslog facility to use
48 * Maximum level to log
50 level_t levels
[DBG_MAX
];
55 * Implementation of bus_listener_t.signal.
57 static bool signal_(private_sys_logger_t
*this, signal_t signal
, level_t level
,
58 int thread
, ike_sa_t
* ike_sa
, char *format
, va_list args
)
60 if (level
<= this->levels
[SIG_TYPE(signal
)])
63 char *current
= buffer
, *next
;
65 /* write in memory buffer first */
66 vsnprintf(buffer
, sizeof(buffer
), format
, args
);
68 /* do a syslog with every line */
71 next
= strchr(current
, '\n');
76 syslog(this->facility
|LOG_INFO
, "%.2d[%N] %s\n",
77 thread
, signal_names
, signal
, current
);
81 /* always stay registered */
86 * Implementation of sys_logger_t.set_level.
88 static void set_level(private_sys_logger_t
*this, signal_t signal
, level_t level
)
90 if (signal
== SIG_ANY
)
93 for (i
= 0; i
< DBG_MAX
; i
++)
95 this->levels
[i
] = level
;
101 this->levels
[SIG_TYPE(signal
)] = level
;
106 * Implementation of sys_logger_t.destroy.
108 static void destroy(private_sys_logger_t
*this)
115 * Described in header.
117 sys_logger_t
*sys_logger_create(int facility
)
119 private_sys_logger_t
*this = malloc_thing(private_sys_logger_t
);
121 /* public functions */
122 this->public.listener
.signal
= (bool(*)(bus_listener_t
*,signal_t
,level_t
,int,ike_sa_t
*,char*,va_list))signal_
;
123 this->public.set_level
= (void(*)(sys_logger_t
*,signal_t
,level_t
))set_level
;
124 this->public.destroy
= (void(*)(sys_logger_t
*))destroy
;
126 /* private variables */
127 this->facility
= facility
;
128 set_level(this, SIG_ANY
, LEVEL_SILENT
);
130 return &this->public;