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
32 #include "allocator.h"
35 * Maximum length of al log entry (only used for logger_s.log)
40 * @brief The logger object.
42 typedef struct private_logger_s private_logger_t
;
43 struct private_logger_s
{
49 * Detail-level of logger.
57 * File to write log output to .
62 /* private functions */
64 * Logs a message to the associated log file.
66 void (*log_to_file
) (private_logger_t
*this, char *format
, ...);
70 * Implements logger_t-function log.
73 * Yes, logg is wrong written :-).
75 static status_t
logg(private_logger_t
*this, logger_level_t loglevel
, char *format
, ...)
77 if ((this->level
& loglevel
) == loglevel
)
82 if (this->output
== NULL
)
85 snprintf(buffer
, MAX_LOG
, "%s: %s", this->name
, format
);
86 va_start(args
, format
);
87 vsyslog(LOG_INFO
, buffer
, args
);
93 snprintf(buffer
, MAX_LOG
, "File %s: %s", this->name
, format
);
94 va_start(args
, format
);
95 this->log_to_file(this, buffer
, args
);
104 * Implements private_logger_t-function log_to_file.
105 * @see private_logger_s.log_to_file.
107 static void log_to_file(private_logger_t
*this,char *format
, ...)
109 char buffer
[MAX_LOG
];
112 current_time
= time(NULL
);
114 snprintf(buffer
, MAX_LOG
, "%s\n", format
);
115 va_start(args
, format
);
116 vfprintf(this->output
, buffer
, args
);
121 * Implements logger_t-function destroy.
122 * @see logger_s.log_bytes.
124 static status_t
log_bytes(private_logger_t
*this, logger_level_t loglevel
, char *label
, char *bytes
, size_t len
)
126 if ((this->level
& loglevel
) == loglevel
)
130 char *bytes_pos
, *bytes_roof
;
133 if (this->output
== NULL
)
135 syslog(LOG_INFO
, "%s: %s (%d bytes)", this->name
, label
, len
);
138 this->log_to_file(this,"%s: %s (%d bytes)", this->name
, label
, len
);
142 bytes_roof
= bytes
+ len
;
145 for (i
= 1; bytes_pos
< bytes_roof
; i
++)
147 static const char hexdig
[] = "0123456789ABCDEF";
148 *buffer_pos
++ = hexdig
[(*bytes_pos
>> 4) & 0xF];
149 *buffer_pos
++ = hexdig
[ *bytes_pos
& 0xF];
152 *buffer_pos
++ = '\0';
154 if (this->output
== NULL
)
156 syslog(LOG_INFO
, "| %s", buffer
);
160 this->log_to_file(this, "| %s", buffer
);
163 else if ((i
% 8) == 0)
169 else if ((i
% 4) == 0)
182 *buffer_pos
++ = '\0';
184 if (this->output
== NULL
)
186 syslog(LOG_INFO
, "| %s", buffer
);
190 this->log_to_file(this, "| %s", buffer
);
199 * Implements logger_t-function log_chunk.
200 * @see logger_s.log_chunk.
202 static status_t
log_chunk(logger_t
*this, logger_level_t loglevel
, char *label
, chunk_t
*chunk
)
204 this->log_bytes(this, loglevel
, label
, chunk
->ptr
, chunk
->len
);
210 * Implements logger_t-function enable_level.
211 * @see logger_s.enable_level.
213 static status_t
enable_level(private_logger_t
*this, logger_level_t log_level
)
215 this->level
|= log_level
;
220 * Implements logger_t-function disable_level.
221 * @see logger_s.disable_level.
223 static status_t
disable_level(private_logger_t
*this, logger_level_t log_level
)
225 this->level
&= ~log_level
;
230 * Implements logger_t-function destroy.
231 * @see logger_s.destroy.
233 static status_t
destroy(private_logger_t
*this)
235 allocator_free(this->name
);
236 allocator_free(this);
241 * Described in Header
243 logger_t
*logger_create(char *logger_name
, logger_level_t log_level
,FILE * output
)
245 private_logger_t
*this = allocator_alloc_thing(private_logger_t
);
252 if (logger_name
== NULL
)
257 this->public.log
= (status_t(*)(logger_t
*,logger_level_t
,char*,...))logg
;
258 this->public.log_bytes
= (status_t(*)(logger_t
*, logger_level_t
, char*,char*,size_t))log_bytes
;
259 this->public.log_chunk
= log_chunk
;
260 this->public.enable_level
= (status_t(*)(logger_t
*,logger_level_t
))enable_level
;
261 this->public.disable_level
= (status_t(*)(logger_t
*,logger_level_t
))disable_level
;
262 this->public.destroy
= (status_t(*)(logger_t
*))destroy
;
264 this->log_to_file
= log_to_file
;
266 /* private variables */
267 this->level
= log_level
;
268 this->name
= allocator_alloc(strlen(logger_name
) + 1);
269 if (this->name
== NULL
)
271 allocator_free(this);
274 strcpy(this->name
,logger_name
);
275 this->output
= output
;
280 openlog(DEAMON_NAME
, 0, LOG_DAEMON
);
283 return (logger_t
*)this;