f1943bb466d79556df12b28fb4fbb09094f6a9e8
[strongswan.git] / Source / charon / logger.c
1 /**
2 * @file logger.c
3 *
4 * @brief Logger object, allows fine-controlled logging
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23
24
25 #include "logger.h"
26 #include "types.h"
27 #include "allocator.h"
28
29 #include <syslog.h>
30 #include <stdarg.h>
31
32
33
34 /**
35 * @brief The logger object
36 */
37 typedef struct private_logger_s private_logger_t;
38 struct private_logger_s {
39 /**
40 * Public data
41 */
42 logger_t public;
43 /**
44 * fd to log, NULL for syslog
45 */
46 FILE *target;
47 /**
48 * detail-level of logger
49 */
50 logger_level_t level;
51 /**
52 * name of logger
53 */
54 char *name;
55 };
56
57
58 /**
59 * implements logger_t-function log
60 *
61 */
62 static status_t logg(private_logger_t *this, logger_level_t loglevel, char *format, ...)
63 {
64 if ((this->level & loglevel) == loglevel)
65 {
66 va_list args;
67 va_start(args, format);
68
69 if (this->target)
70 {
71 fprintf(this->target, format, args);
72 fprintf(this->target, "\n");
73 }
74 else
75 {
76 syslog(LOG_INFO, format, args);
77 }
78 va_end(args);
79 }
80
81
82 return SUCCESS;
83 }
84
85 /**
86 * implements logger_t-function enable_level
87 *
88 */
89 static status_t enable_level(private_logger_t *this, logger_level_t log_level)
90 {
91 this->level |= log_level;
92 return SUCCESS;
93 }
94
95 /**
96 * implements logger_t-function disable_level
97 *
98 */
99 static status_t disable_level(private_logger_t *this, logger_level_t log_level)
100 {
101 this->level &= (~log_level);
102 return SUCCESS;
103 }
104
105 /**
106 * implements logger_t-function destroy
107 *
108 */
109 static status_t destroy(private_logger_t *this)
110 {
111 if (this->target)
112 {
113 fclose(this->target);
114 }
115 allocator_free(this);
116 return SUCCESS;
117 }
118
119 /*
120 * Described in Header
121 *
122 */
123 logger_t *logger_create(char *logger_name, char *file, logger_level_t log_level)
124 {
125 private_logger_t *this = allocator_alloc_thing(private_logger_t);
126
127 if (this == NULL)
128 {
129 return NULL;
130 }
131
132 this->public.log = (status_t(*)(logger_t*,logger_level_t,char*,...))logg;
133 this->public.enable_level = (status_t(*)(logger_t*,logger_level_t))enable_level;
134 this->public.disable_level = (status_t(*)(logger_t*,logger_level_t))disable_level;
135 this->public.destroy = (status_t(*)(logger_t*))destroy;
136
137 this->level = log_level;
138 this->name = logger_name;
139
140 /* use system logger ? */
141 if (file)
142 {
143 this->target = fopen(file, "a");
144 if (this->target == NULL)
145 {
146 allocator_free(this);
147 return NULL;
148 }
149 }
150 else
151 {
152 this->target = NULL;
153 openlog("charon", 0, LOG_DAEMON);
154 }
155
156 return (logger_t*)this;
157 }