Prefix each log entry with a timestamp. The option accepts a format string
as passed to **strftime**(3).
-charon.filelog.<name>.time_add_ms = no
- Adds the milliseconds within the current second after the timestamp
- (separated by a dot, so _time_format_ should end with %S or %T).
-
-charon.filelog.<name>.time_add_us = no
- Adds the microseconds within the current second after the timestamp
- (separated by a dot, so _time_format_ should end with %S or %T).
+charon.filelog.<name>.time_precision =
+ Add the milliseconds (_ms_) or microseconds (_us_) within the current second
+ after the timestamp (separated by a dot, so _time_format_ should end
+ with %S or %T). By default, nothing is added.
charon.syslog {}
Section to define syslog loggers, see LOGGER CONFIGURATION in
*/
static void load_logger_options(file_logger_t *logger, char *section)
{
- char *time_format;
- bool add_ms, add_us, ike_name, log_level, json;
+ file_logger_options_t options;
- time_format = conftest->test->get_str(conftest->test,
+ options.time_format = conftest->test->get_str(conftest->test,
"log.%s.time_format", NULL, section);
- add_ms = conftest->test->get_bool(conftest->test,
- "log.%s.time_add_ms", FALSE, section);
- add_us = conftest->test->get_bool(conftest->test,
- "log.%s.time_add_us", FALSE, section);
- ike_name = conftest->test->get_bool(conftest->test,
+ options.time_precision = file_logger_time_precision_parse(
+ conftest->test->get_str(conftest->test,
+ "log.%s.time_precision", NULL, section));
+ /* handle legacy option */
+ if (!options.time_precision &&
+ conftest->test->get_bool(conftest->test,
+ "log.%s.time_add_ms", FALSE, section))
+ {
+ options.time_precision = FILE_LOGGER_TIME_PRECISION_MS;
+ }
+ options.ike_name = conftest->test->get_bool(conftest->test,
"log.%s.ike_name", FALSE, section);
- log_level = conftest->test->get_bool(conftest->test,
+ options.log_level = conftest->test->get_bool(conftest->test,
"log.%s.log_level", FALSE, section);
- json = conftest->test->get_bool(conftest->test,
+ options.json = conftest->test->get_bool(conftest->test,
"log.%s.json", FALSE, section);
- logger->set_options(logger, time_format, add_ms, add_us, ike_name, log_level, json);
+ logger->set_options(logger, &options);
}
/**
int sig;
char *suite_file = "suite.conf", *test_file = NULL, *preload, *plugins;
file_logger_t *logger;
+ file_logger_options_t options = {};
if (!library_init(NULL, "conftest"))
{
lib->credmgr->add_set(lib->credmgr, &conftest->creds->set);
logger = file_logger_create("stdout");
- logger->set_options(logger, NULL, FALSE, FALSE, FALSE, FALSE, FALSE);
+ logger->set_options(logger, &options);
logger->open(logger, FALSE, FALSE);
logger->set_level(logger, DBG_ANY, LEVEL_CTRL);
charon->bus->add_logger(charon->bus, &logger->logger);
char *time_format;
/**
- * Add milliseconds after the time string
+ * Add milliseconds/microseconds after the time string
*/
- bool add_ms;
-
- /**
- * Add microseconds after the time string
- */
- bool add_us;
+ file_logger_time_precision_t time_precision;
/**
* Print the name/# of the IKE_SA?
timeval_t tv;
time_t s;
size_t time_len;
- u_int ms = 0;
- long us = 0;
this->lock->read_lock(this->lock);
if (!this->out)
{
gettimeofday(&tv, NULL);
s = tv.tv_sec;
- ms = tv.tv_usec / 1000;
- us = tv.tv_usec;
localtime_r(&s, &tm);
time_len = strftime(timestr, sizeof(timestr), this->time_format, &tm);
- if (this->add_us && sizeof(timestr) - time_len > 7)
+ if (this->time_precision == FILE_LOGGER_TIME_PRECISION_US &&
+ sizeof(timestr) - time_len > 7)
{
- snprintf(×tr[time_len], sizeof(timestr)-time_len, ".%06d", us);
+ snprintf(×tr[time_len], sizeof(timestr)-time_len, ".%06d",
+ tv.tv_usec);
}
- else if (this->add_ms && sizeof(timestr) - time_len > 4)
+ else if (this->time_precision == FILE_LOGGER_TIME_PRECISION_MS &&
+ sizeof(timestr) - time_len > 4)
{
- snprintf(×tr[time_len], sizeof(timestr)-time_len, ".%03u", ms);
+ snprintf(×tr[time_len], sizeof(timestr)-time_len, ".%03u",
+ tv.tv_usec / 1000);
}
}
}
METHOD(file_logger_t, set_options, void,
- private_file_logger_t *this, char *time_format, bool add_ms, bool add_us,
- bool ike_name, bool log_level, bool json)
+ private_file_logger_t *this, file_logger_options_t *options)
{
this->lock->write_lock(this->lock);
free(this->time_format);
- this->time_format = strdupnull(time_format);
- this->add_ms = add_ms;
- this->add_us = add_us;
- this->ike_name = ike_name;
- this->log_level = log_level;
- this->json = json;
+ this->time_format = strdupnull(options->time_format);
+ this->time_precision = options->time_precision;
+ this->ike_name = options->ike_name;
+ this->log_level = options->log_level;
+ this->json = options->json;
this->lock->unlock(this->lock);
}
free(this);
}
+/*
+ * Described in header
+ */
+file_logger_time_precision_t file_logger_time_precision_parse(const char *str)
+{
+ if (streq(str, "ms"))
+ {
+ return FILE_LOGGER_TIME_PRECISION_MS;
+ }
+ else if (streq(str, "us"))
+ {
+ return FILE_LOGGER_TIME_PRECISION_US;
+ }
+ return FILE_LOGGER_TIME_PRECISION_NONE;
+}
+
/*
* Described in header.
*/
/*
- * Copyright (C) 2012-2020 Tobias Brunner
+ * Copyright (C) 2012-2024 Tobias Brunner
* Copyright (C) 2006 Martin Willi
*
* Copyright (C) secunet Security Networks AG
#include <bus/listeners/logger.h>
typedef struct file_logger_t file_logger_t;
+typedef enum file_logger_time_precision_t file_logger_time_precision_t;
+typedef struct file_logger_options_t file_logger_options_t;
/**
* Logger to files which implements listener_t.
/**
* Set options used by this logger
*
- * @param time_format format of timestamp prefix, as in strftime(), cloned
- * @param add_ms TRUE to add the number of milliseconds within the
- * current second after the timestamp
- * @param add_us TRUE to add the number of microseconds within the
- * current second after the timestamp
- * @param ike_name TRUE to prefix the name of the IKE_SA
- * @param log_level TRUE to include the log level in the message
- * @param json TRUE to log as JSON objects
+ * @param options options for this file logger
*/
- void (*set_options) (file_logger_t *this, char *time_format, bool add_ms,
- bool add_us, bool ike_name, bool log_level, bool json);
+ void (*set_options) (file_logger_t *this, file_logger_options_t *options);
/**
* Open (or reopen) the log file according to the given parameters
void (*destroy) (file_logger_t *this);
};
+/**
+ * Precision for timestamps printed by file loggers.
+ */
+enum file_logger_time_precision_t {
+ /** Don't add anything after the timestamp */
+ FILE_LOGGER_TIME_PRECISION_NONE,
+ /** Add the number of milliseconds within the current second after the
+ * timestamp */
+ FILE_LOGGER_TIME_PRECISION_MS,
+ /** Add the number of microseconds within the current second after the
+ * timestamp */
+ FILE_LOGGER_TIME_PRECISION_US,
+};
+
+/**
+ * Parse the given time precision string.
+ *
+ * @param str time precision string value
+ * @return time precision
+ */
+file_logger_time_precision_t file_logger_time_precision_parse(const char *str);
+
+/**
+ * Options for file loggers.
+ */
+struct file_logger_options_t {
+ /** Format of timestamp prefix, as in strftime(), cloned */
+ char *time_format;
+ /** Optinoal precision suffix for timestamp */
+ file_logger_time_precision_t time_precision;
+ /** Prefix the name/unique ID of the IKE_SA */
+ bool ike_name;
+ /** Include the log level in the message */
+ bool log_level;
+ /** Log as JSON objects */
+ bool json;
+};
+
/**
* Constructor to create a file_logger_t object.
*
linked_list_t *current_loggers)
{
file_logger_t *file_logger;
+ file_logger_options_t options;
debug_t group;
level_t def;
- bool add_ms, add_us, ike_name, log_level, json, flush_line, append;
- char *time_format, *filename;
-
- time_format = lib->settings->get_str(lib->settings,
- "%s.filelog.%s.time_format", NULL, lib->ns, section);
- add_ms = lib->settings->get_bool(lib->settings,
- "%s.filelog.%s.time_add_ms", FALSE, lib->ns, section);
- add_us = lib->settings->get_bool(lib->settings,
- "%s.filelog.%s.time_add_us", FALSE, lib->ns, section);
- ike_name = lib->settings->get_bool(lib->settings,
- "%s.filelog.%s.ike_name", FALSE, lib->ns, section);
- log_level = lib->settings->get_bool(lib->settings,
- "%s.filelog.%s.log_level", FALSE, lib->ns, section);
- json = lib->settings->get_bool(lib->settings,
- "%s.filelog.%s.json", FALSE, lib->ns, section);
+ bool flush_line, append;
+ char *filename;
+
+ options.time_format = lib->settings->get_str(lib->settings,
+ "%s.filelog.%s.time_format", NULL, lib->ns, section);
+ options.time_precision = file_logger_time_precision_parse(
+ lib->settings->get_str(lib->settings,
+ "%s.filelog.%s.time_precision", NULL, lib->ns, section));
+ /* handle legacy option */
+ if (!options.time_precision &&
+ lib->settings->get_bool(lib->settings,
+ "%s.filelog.%s.time_add_ms", FALSE, lib->ns, section))
+ {
+ options.time_precision = FILE_LOGGER_TIME_PRECISION_MS;
+ }
+ options.ike_name = lib->settings->get_bool(lib->settings,
+ "%s.filelog.%s.ike_name", FALSE, lib->ns, section);
+ options.log_level = lib->settings->get_bool(lib->settings,
+ "%s.filelog.%s.log_level", FALSE, lib->ns, section);
+ options.json = lib->settings->get_bool(lib->settings,
+ "%s.filelog.%s.json", FALSE, lib->ns, section);
+
flush_line = lib->settings->get_bool(lib->settings,
- "%s.filelog.%s.flush_line", FALSE, lib->ns, section);
+ "%s.filelog.%s.flush_line", FALSE, lib->ns, section);
append = lib->settings->get_bool(lib->settings,
- "%s.filelog.%s.append", TRUE, lib->ns, section);
+ "%s.filelog.%s.append", TRUE, lib->ns, section);
filename = lib->settings->get_str(lib->settings,
- "%s.filelog.%s.path", section, lib->ns, section);
+ "%s.filelog.%s.path", section, lib->ns, section);
file_logger = add_file_logger(this, filename, current_loggers);
if (!file_logger)
return;
}
- file_logger->set_options(file_logger, time_format, add_ms, add_us,
- ike_name, log_level, json);
+ file_logger->set_options(file_logger, &options);
file_logger->open(file_logger, flush_line, append);
def = lib->settings->get_int(lib->settings, "%s.filelog.%s.default", 1,