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
20 #include "sys_logger.h"
23 typedef struct private_sys_logger_t private_sys_logger_t
;
26 * Private data of a sys_logger_t object
28 struct private_sys_logger_t
{
36 * syslog facility to use
41 * Maximum level to log, for each group
43 level_t levels
[DBG_MAX
];
47 * Implementation of listener_t.log.
49 static bool log_(private_sys_logger_t
*this, debug_t group
, level_t level
,
50 int thread
, ike_sa_t
* ike_sa
, char *format
, va_list args
)
52 if (level
<= this->levels
[group
])
55 char *current
= buffer
, *next
;
57 /* write in memory buffer first */
58 vsnprintf(buffer
, sizeof(buffer
), format
, args
);
60 /* do a syslog with every line */
63 next
= strchr(current
, '\n');
68 syslog(this->facility
|LOG_INFO
, "%.2d[%N] %s\n",
69 thread
, debug_names
, group
, current
);
73 /* always stay registered */
78 * Implementation of sys_logger_t.set_level.
80 static void set_level(private_sys_logger_t
*this, debug_t group
, level_t level
)
84 this->levels
[group
] = level
;
88 for (group
= 0; group
< DBG_MAX
; group
++)
90 this->levels
[group
] = level
;
96 * Implementation of sys_logger_t.destroy.
98 static void destroy(private_sys_logger_t
*this)
105 * Described in header.
107 sys_logger_t
*sys_logger_create(int facility
)
109 private_sys_logger_t
*this = malloc_thing(private_sys_logger_t
);
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(*)(sys_logger_t
*,debug_t
,level_t
))set_level
;
115 this->public.destroy
= (void(*)(sys_logger_t
*))destroy
;
117 /* private variables */
118 this->facility
= facility
;
119 set_level(this, DBG_ANY
, LEVEL_SILENT
);
121 return &this->public;