""
[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 static status_t logg(private_logger_t *this, logger_level_t loglevel, char *format, ...)
60 {
61 if ((this->level & loglevel) == loglevel)
62 {
63 va_list args;
64 va_start(args, format);
65
66 if (this->target)
67 {
68 fprintf(this->target, format, args);
69 fprintf(this->target, "\n");
70 }
71 else
72 {
73 syslog(LOG_INFO, format, args);
74 }
75 va_end(args);
76 }
77
78
79 return SUCCESS;
80 }
81
82 static status_t enable_level(private_logger_t *this, logger_level_t log_level)
83 {
84 this->level |= log_level;
85 return SUCCESS;
86 }
87
88 static status_t disable_level(private_logger_t *this, logger_level_t log_level)
89 {
90 this->level &= (~log_level);
91 return SUCCESS;
92 }
93
94 static status_t destroy(private_logger_t *this)
95 {
96 if (this->target)
97 {
98 fclose(this->target);
99 }
100 allocator_free(this);
101 return SUCCESS;
102 }
103
104
105 logger_t *logger_create(char *logger_name, char *file, logger_level_t log_level)
106 {
107 private_logger_t *this = allocator_alloc_thing(private_logger_t);
108
109 if (this == NULL)
110 {
111 return NULL;
112 }
113
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;
118
119 this->level = log_level;
120 this->name = logger_name;
121
122 /* use system logger ? */
123 if (file)
124 {
125 this->target = fopen(file, "a");
126 if (this->target == NULL)
127 {
128 allocator_free(this);
129 return NULL;
130 }
131 }
132 else
133 {
134 this->target = NULL;
135 openlog("charon", 0, LOG_DAEMON);
136 }
137
138 return (logger_t*)this;
139 }
140
141