4 * @brief Logger object, allows fine-controlled logging
9 * Copyright (C) 2005 Jan Hutter, 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 "allocator.h"
35 * @brief The logger object
37 typedef struct private_logger_s private_logger_t
;
38 struct private_logger_s
{
44 * fd to log, NULL for syslog
48 * detail-level of logger
59 static status_t
logg(private_logger_t
*this, logger_level_t loglevel
, char *format
, ...)
61 if ((this->level
& loglevel
) == loglevel
)
64 va_start(args
, format
);
68 fprintf(this->target
, format
, args
);
69 fprintf(this->target
, "\n");
73 syslog(LOG_INFO
, format
, args
);
82 static status_t
enable_level(private_logger_t
*this, logger_level_t log_level
)
84 this->level
|= log_level
;
88 static status_t
disable_level(private_logger_t
*this, logger_level_t log_level
)
90 this->level
&= (~log_level
);
94 static status_t
destroy(private_logger_t
*this)
100 allocator_free(this);
105 logger_t
*logger_create(char *logger_name
, char *file
, logger_level_t log_level
)
107 private_logger_t
*this = allocator_alloc_thing(private_logger_t
);
114 this->public.log
= (status_t(*)(logger_t
*,logger_level_t
,char*,...))logg
;
115 this->public.enable_level
= (status_t(*)(logger_t
*,logger_level_t
))enable_level
;
116 this->public.disable_level
= (status_t(*)(logger_t
*,logger_level_t
))disable_level
;
117 this->public.destroy
= (status_t(*)(logger_t
*))destroy
;
119 this->level
= log_level
;
120 this->name
= logger_name
;
122 /* use system logger ? */
125 this->target
= fopen(file
, "a");
126 if (this->target
== NULL
)
128 allocator_free(this);
135 openlog("charon", 0, LOG_DAEMON
);
138 return (logger_t
*)this;