From: Tobias Brunner Date: Wed, 9 Oct 2024 15:53:39 +0000 (+0200) Subject: file-logger: Take options as a struct and combine ms/us suffix options X-Git-Tag: 6.0.0rc1~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f09b8203d3f919b61570138d2c82d53597e0a9f7;p=thirdparty%2Fstrongswan.git file-logger: Take options as a struct and combine ms/us suffix options References strongswan/strongswan#2475 --- diff --git a/conf/options/charon-logging.opt b/conf/options/charon-logging.opt index 6b67d3b665..f7ff1dfe43 100644 --- a/conf/options/charon-logging.opt +++ b/conf/options/charon-logging.opt @@ -46,13 +46,10 @@ charon.filelog..time_format Prefix each log entry with a timestamp. The option accepts a format string as passed to **strftime**(3). -charon.filelog..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..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..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 diff --git a/src/conftest/conftest.c b/src/conftest/conftest.c index 02713b23fc..e223d9f941 100644 --- a/src/conftest/conftest.c +++ b/src/conftest/conftest.c @@ -382,23 +382,28 @@ static void load_log_levels(file_logger_t *logger, char *section) */ 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); } /** @@ -442,6 +447,7 @@ int main(int argc, char *argv[]) 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")) { @@ -464,7 +470,7 @@ int main(int argc, char *argv[]) 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); diff --git a/src/libcharon/bus/listeners/file_logger.c b/src/libcharon/bus/listeners/file_logger.c index 0078e20c39..3a05aa47ce 100644 --- a/src/libcharon/bus/listeners/file_logger.c +++ b/src/libcharon/bus/listeners/file_logger.c @@ -66,14 +66,9 @@ struct private_file_logger_t { 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? @@ -112,8 +107,6 @@ METHOD(logger_t, log_, void, 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) @@ -126,18 +119,20 @@ METHOD(logger_t, log_, void, { 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); } } @@ -269,17 +264,15 @@ METHOD(file_logger_t, set_level, void, } 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); } @@ -363,6 +356,22 @@ METHOD(file_logger_t, destroy, void, 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. */ diff --git a/src/libcharon/bus/listeners/file_logger.h b/src/libcharon/bus/listeners/file_logger.h index 508c86b150..5e4eda992c 100644 --- a/src/libcharon/bus/listeners/file_logger.h +++ b/src/libcharon/bus/listeners/file_logger.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2020 Tobias Brunner + * Copyright (C) 2012-2024 Tobias Brunner * Copyright (C) 2006 Martin Willi * * Copyright (C) secunet Security Networks AG @@ -26,6 +26,8 @@ #include 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. @@ -48,17 +50,9 @@ struct file_logger_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 @@ -74,6 +68,44 @@ struct file_logger_t { 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. * diff --git a/src/libcharon/daemon.c b/src/libcharon/daemon.c index 013f21f9a3..8af6056c9d 100644 --- a/src/libcharon/daemon.c +++ b/src/libcharon/daemon.c @@ -491,29 +491,37 @@ static void load_file_logger(private_daemon_t *this, char *section, 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) @@ -521,8 +529,7 @@ static void load_file_logger(private_daemon_t *this, char *section, 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,