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
22 #include "sys_logger.h"
25 typedef struct private_sys_logger_t private_sys_logger_t
;
28 * Private data of a sys_logger_t object
30 struct private_sys_logger_t
{
38 * syslog facility to use
43 * Maximum level to log
45 level_t levels
[DBG_MAX
];
50 * Implementation of bus_listener_t.signal.
52 static bool signal_(private_sys_logger_t
*this, signal_t signal
, level_t level
,
53 int thread
, ike_sa_t
* ike_sa
, void *data
,
54 char *format
, va_list args
)
56 if (level
<= this->levels
[SIG_TYPE(signal
)])
59 char *current
= buffer
, *next
;
61 /* write in memory buffer first */
62 vsnprintf(buffer
, sizeof(buffer
), format
, args
);
64 /* do a syslog with every line */
67 next
= strchr(current
, '\n');
72 syslog(this->facility
|LOG_INFO
, "%.2d[%N] %s\n",
73 thread
, signal_names
, signal
, current
);
77 /* always stay registered */
82 * Implementation of sys_logger_t.set_level.
84 static void set_level(private_sys_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 sys_logger_t.destroy.
104 static void destroy(private_sys_logger_t
*this)
111 * Described in header.
113 sys_logger_t
*sys_logger_create(int facility
)
115 private_sys_logger_t
*this = malloc_thing(private_sys_logger_t
);
117 /* public functions */
118 this->public.listener
.signal
= (bool(*)(bus_listener_t
*,signal_t
,level_t
,int,ike_sa_t
*,void*,char*,va_list))signal_
;
119 this->public.set_level
= (void(*)(sys_logger_t
*,signal_t
,level_t
))set_level
;
120 this->public.destroy
= (void(*)(sys_logger_t
*))destroy
;
122 /* private variables */
123 this->facility
= facility
;
124 set_level(this, SIG_ANY
, LEVEL_SILENT
);
126 return &this->public;