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, for each group
45 level_t levels
[DBG_MAX
];
49 * Implementation of listener_t.log.
51 static bool log_(private_sys_logger_t
*this, debug_t group
, level_t level
,
52 int thread
, ike_sa_t
* ike_sa
, char *format
, va_list args
)
54 if (level
<= this->levels
[group
])
57 char *current
= buffer
, *next
;
59 /* write in memory buffer first */
60 vsnprintf(buffer
, sizeof(buffer
), format
, args
);
62 /* do a syslog with every line */
65 next
= strchr(current
, '\n');
70 syslog(this->facility
|LOG_INFO
, "%.2d[%N] %s\n",
71 thread
, debug_names
, group
, current
);
75 /* always stay registered */
80 * Implementation of sys_logger_t.set_level.
82 static void set_level(private_sys_logger_t
*this, debug_t group
, level_t level
)
86 this->levels
[group
] = level
;
90 for (group
= 0; group
< DBG_MAX
; group
++)
92 this->levels
[group
] = level
;
98 * Implementation of sys_logger_t.destroy.
100 static void destroy(private_sys_logger_t
*this)
107 * Described in header.
109 sys_logger_t
*sys_logger_create(int facility
)
111 private_sys_logger_t
*this = malloc_thing(private_sys_logger_t
);
113 /* public functions */
114 memset(&this->public.listener
, 0, sizeof(listener_t
));
115 this->public.listener
.log
= (bool(*)(listener_t
*,debug_t
,level_t
,int,ike_sa_t
*,char*,va_list))log_
;
116 this->public.set_level
= (void(*)(sys_logger_t
*,debug_t
,level_t
))set_level
;
117 this->public.destroy
= (void(*)(sys_logger_t
*))destroy
;
119 /* private variables */
120 this->facility
= facility
;
121 set_level(this, DBG_ANY
, LEVEL_SILENT
);
123 return &this->public;