The file logger supports a time prefix using a strftime() format specifier

This commit is contained in:
Martin Willi
2010-07-08 17:44:19 +02:00
parent 4cc9afe35f
commit 6f07f5e3d4
3 changed files with 36 additions and 9 deletions
+28 -4
View File
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "file_logger.h"
@@ -40,6 +41,11 @@ struct private_file_logger_t {
* Maximum level to log, for each group
*/
level_t levels[DBG_MAX];
/**
* strftime() format of time prefix, if any
*/
char *time_format;
};
/**
@@ -50,8 +56,17 @@ static bool log_(private_file_logger_t *this, debug_t group, level_t level,
{
if (level <= this->levels[group])
{
char buffer[8192];
char buffer[8192], timestr[128];
char *current = buffer, *next;
struct tm tm;
time_t t;
if (this->time_format)
{
t = time(NULL);
localtime_r(&t, &tm);
strftime(timestr, sizeof(timestr), this->time_format, &tm);
}
/* write in memory buffer first */
vsnprintf(buffer, sizeof(buffer), format, args);
@@ -64,8 +79,16 @@ static bool log_(private_file_logger_t *this, debug_t group, level_t level,
{
*(next++) = '\0';
}
fprintf(this->out, "%.2d[%N] %s\n",
thread, debug_names, group, current);
if (this->time_format)
{
fprintf(this->out, "%s %.2d[%N] %s\n",
timestr, thread, debug_names, group, current);
}
else
{
fprintf(this->out, "%.2d[%N] %s\n",
thread, debug_names, group, current);
}
current = next;
}
}
@@ -106,7 +129,7 @@ static void destroy(private_file_logger_t *this)
/*
* Described in header.
*/
file_logger_t *file_logger_create(FILE *out)
file_logger_t *file_logger_create(FILE *out, char *time_format)
{
private_file_logger_t *this = malloc_thing(private_file_logger_t);
@@ -118,6 +141,7 @@ file_logger_t *file_logger_create(FILE *out)
/* private variables */
this->out = out;
this->time_format = time_format;
set_level(this, DBG_ANY, LEVEL_SILENT);
return &this->public;
+4 -3
View File
@@ -52,9 +52,10 @@ struct file_logger_t {
/**
* Constructor to create a file_logger_t object.
*
* @param out FILE to write to
* @return file_logger_t object
* @param out FILE to write to
* @param time_format format of timestamp prefix, as in strftime()
* @return file_logger_t object
*/
file_logger_t *file_logger_create(FILE *out);
file_logger_t *file_logger_create(FILE *out, char *time_format);
#endif /** FILE_LOGGER_H_ @}*/
+4 -2
View File
@@ -281,7 +281,9 @@ static void initialize_loggers(private_daemon_t *this, bool use_stderr,
setlinebuf(file);
}
}
file_logger = file_logger_create(file);
file_logger = file_logger_create(file,
lib->settings->get_str(lib->settings,
"charon.filelog.%s.time_format", NULL, filename));
def = lib->settings->get_int(lib->settings,
"charon.filelog.%s.default", 1, filename);
for (group = 0; group < DBG_MAX; group++)
@@ -302,7 +304,7 @@ static void initialize_loggers(private_daemon_t *this, bool use_stderr,
if (!loggers_defined)
{
/* set up default stdout file_logger */
file_logger = file_logger_create(stdout);
file_logger = file_logger_create(stdout, NULL);
this->public.bus->add_listener(this->public.bus, &file_logger->listener);
this->public.file_loggers->insert_last(this->public.file_loggers,
file_logger);