The file logger supports a time prefix using a strftime() format specifier
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "file_logger.h"
|
#include "file_logger.h"
|
||||||
|
|
||||||
@@ -40,6 +41,11 @@ struct private_file_logger_t {
|
|||||||
* Maximum level to log, for each group
|
* Maximum level to log, for each group
|
||||||
*/
|
*/
|
||||||
level_t levels[DBG_MAX];
|
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])
|
if (level <= this->levels[group])
|
||||||
{
|
{
|
||||||
char buffer[8192];
|
char buffer[8192], timestr[128];
|
||||||
char *current = buffer, *next;
|
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 */
|
/* write in memory buffer first */
|
||||||
vsnprintf(buffer, sizeof(buffer), format, args);
|
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';
|
*(next++) = '\0';
|
||||||
}
|
}
|
||||||
fprintf(this->out, "%.2d[%N] %s\n",
|
if (this->time_format)
|
||||||
thread, debug_names, group, current);
|
{
|
||||||
|
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;
|
current = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,7 +129,7 @@ static void destroy(private_file_logger_t *this)
|
|||||||
/*
|
/*
|
||||||
* Described in header.
|
* 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);
|
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 */
|
/* private variables */
|
||||||
this->out = out;
|
this->out = out;
|
||||||
|
this->time_format = time_format;
|
||||||
set_level(this, DBG_ANY, LEVEL_SILENT);
|
set_level(this, DBG_ANY, LEVEL_SILENT);
|
||||||
|
|
||||||
return &this->public;
|
return &this->public;
|
||||||
|
|||||||
@@ -52,9 +52,10 @@ struct file_logger_t {
|
|||||||
/**
|
/**
|
||||||
* Constructor to create a file_logger_t object.
|
* Constructor to create a file_logger_t object.
|
||||||
*
|
*
|
||||||
* @param out FILE to write to
|
* @param out FILE to write to
|
||||||
* @return file_logger_t object
|
* @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_ @}*/
|
#endif /** FILE_LOGGER_H_ @}*/
|
||||||
|
|||||||
@@ -281,7 +281,9 @@ static void initialize_loggers(private_daemon_t *this, bool use_stderr,
|
|||||||
setlinebuf(file);
|
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,
|
def = lib->settings->get_int(lib->settings,
|
||||||
"charon.filelog.%s.default", 1, filename);
|
"charon.filelog.%s.default", 1, filename);
|
||||||
for (group = 0; group < DBG_MAX; group++)
|
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)
|
if (!loggers_defined)
|
||||||
{
|
{
|
||||||
/* set up default stdout file_logger */
|
/* 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.bus->add_listener(this->public.bus, &file_logger->listener);
|
||||||
this->public.file_loggers->insert_last(this->public.file_loggers,
|
this->public.file_loggers->insert_last(this->public.file_loggers,
|
||||||
file_logger);
|
file_logger);
|
||||||
|
|||||||
Reference in New Issue
Block a user