aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libcharon/bus/listeners/file_logger.c32
-rw-r--r--src/libcharon/bus/listeners/file_logger.h7
-rw-r--r--src/libcharon/daemon.c6
3 files changed, 36 insertions, 9 deletions
diff --git a/src/libcharon/bus/listeners/file_logger.c b/src/libcharon/bus/listeners/file_logger.c
index 12587deaf..87db532f5 100644
--- a/src/libcharon/bus/listeners/file_logger.c
+++ b/src/libcharon/bus/listeners/file_logger.c
@@ -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;
diff --git a/src/libcharon/bus/listeners/file_logger.h b/src/libcharon/bus/listeners/file_logger.h
index bd443fdb8..e02a12c0c 100644
--- a/src/libcharon/bus/listeners/file_logger.h
+++ b/src/libcharon/bus/listeners/file_logger.h
@@ -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_ @}*/
diff --git a/src/libcharon/daemon.c b/src/libcharon/daemon.c
index 09eec7ef8..e1aa03e7f 100644
--- a/src/libcharon/daemon.c
+++ b/src/libcharon/daemon.c
@@ -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);