From: Tobias Brunner Date: Tue, 21 Jul 2020 13:56:50 +0000 (+0200) Subject: file-logger: Optionally log the level of each message X-Git-Tag: 5.9.1rc1~23 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a3f5e38b7fa7873764ff9192984b44d65a36cebb;p=thirdparty%2Fstrongswan.git file-logger: Optionally log the level of each message Fixes #3509. --- diff --git a/conf/options/charon-logging.opt b/conf/options/charon-logging.opt index e850c4487b..71f3a460bd 100644 --- a/conf/options/charon-logging.opt +++ b/conf/options/charon-logging.opt @@ -29,6 +29,9 @@ charon.filelog..ike_name = no Prefix each log entry with the connection name and a unique numerical identifier for each IKE_SA. +charon.filelog..log_level = no + Add the log level of each message after the subsystem (e.g. [IKE2]). + charon.filelog..time_format Prefix each log entry with a timestamp. The option accepts a format string as passed to **strftime**(3). diff --git a/src/conftest/conftest.c b/src/conftest/conftest.c index cbbe0e9def..0d36ba3ae9 100644 --- a/src/conftest/conftest.c +++ b/src/conftest/conftest.c @@ -382,7 +382,7 @@ 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, ike_name; + bool add_ms, ike_name, log_level; time_format = conftest->test->get_str(conftest->test, "log.%s.time_format", NULL, section); @@ -390,8 +390,10 @@ static void load_logger_options(file_logger_t *logger, char *section) "log.%s.time_add_ms", FALSE, section); ike_name = conftest->test->get_bool(conftest->test, "log.%s.ike_name", FALSE, section); + log_level = conftest->test->get_bool(conftest->test, + "log.%s.log_level", FALSE, section); - logger->set_options(logger, time_format, add_ms, ike_name); + logger->set_options(logger, time_format, add_ms, ike_name, log_level); } /** @@ -457,7 +459,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); + logger->set_options(logger, NULL, FALSE, FALSE, FALSE); 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 704c4a510e..cc93956d50 100644 --- a/src/libcharon/bus/listeners/file_logger.c +++ b/src/libcharon/bus/listeners/file_logger.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2015 Tobias Brunner + * Copyright (C) 2012-2020 Tobias Brunner * Copyright (C) 2006 Martin Willi * HSR Hochschule fuer Technik Rapperswil * @@ -74,6 +74,11 @@ struct private_file_logger_t { */ bool ike_name; + /** + * Print the log level + */ + bool log_level; + /** * Mutex to ensure multi-line log messages are not torn apart */ @@ -89,7 +94,7 @@ METHOD(logger_t, log_, void, private_file_logger_t *this, debug_t group, level_t level, int thread, ike_sa_t* ike_sa, const char *message) { - char timestr[128], namestr[128] = ""; + char groupstr[5], timestr[128], namestr[128] = ""; const char *current = message, *next; struct tm tm; timeval_t tv; @@ -102,6 +107,7 @@ METHOD(logger_t, log_, void, this->lock->unlock(this->lock); return; } + if (this->time_format) { gettimeofday(&tv, NULL); @@ -110,6 +116,17 @@ METHOD(logger_t, log_, void, localtime_r(&s, &tm); strftime(timestr, sizeof(timestr), this->time_format, &tm); } + + if (this->log_level) + { + snprintf(groupstr, sizeof(groupstr), "%N%d", debug_names, group, + level); + } + else + { + snprintf(groupstr, sizeof(groupstr), "%N", debug_names, group); + } + if (this->ike_name && ike_sa) { if (ike_sa->get_peer_cfg(ike_sa)) @@ -137,19 +154,19 @@ METHOD(logger_t, log_, void, { if (this->add_ms) { - fprintf(this->out, "%s.%03u %.2d[%N]%s ", - timestr, ms, thread, debug_names, group, namestr); + fprintf(this->out, "%s.%03u %.2d[%s]%s ", + timestr, ms, thread, groupstr, namestr); } else { - fprintf(this->out, "%s %.2d[%N]%s ", - timestr, thread, debug_names, group, namestr); + fprintf(this->out, "%s %.2d[%s]%s ", + timestr, thread, groupstr, namestr); } } else { - fprintf(this->out, "%.2d[%N]%s ", - thread, debug_names, group, namestr); + fprintf(this->out, "%.2d[%s]%s ", + thread, groupstr, namestr); } if (next == NULL) { @@ -199,13 +216,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 ike_name) + private_file_logger_t *this, char *time_format, bool add_ms, bool ike_name, + bool log_level) { this->lock->write_lock(this->lock); free(this->time_format); this->time_format = strdupnull(time_format); this->add_ms = add_ms; this->ike_name = ike_name; + this->log_level = log_level; this->lock->unlock(this->lock); } diff --git a/src/libcharon/bus/listeners/file_logger.h b/src/libcharon/bus/listeners/file_logger.h index 85260b132d..abbe90cdf1 100644 --- a/src/libcharon/bus/listeners/file_logger.h +++ b/src/libcharon/bus/listeners/file_logger.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012-2015 Tobias Brunner + * Copyright (C) 2012-2020 Tobias Brunner * Copyright (C) 2006 Martin Willi * HSR Hochschule fuer Technik Rapperswil * @@ -51,9 +51,10 @@ struct file_logger_t { * @param add_ms TRUE to add the number of milliseconds 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 */ void (*set_options) (file_logger_t *this, char *time_format, bool add_ms, - bool ike_name); + bool ike_name, bool log_level); /** * Open (or reopen) the log file according to the given parameters diff --git a/src/libcharon/daemon.c b/src/libcharon/daemon.c index 801e42c72e..0b5bc3ec7a 100644 --- a/src/libcharon/daemon.c +++ b/src/libcharon/daemon.c @@ -486,7 +486,7 @@ static void load_file_logger(private_daemon_t *this, char *section, file_logger_t *file_logger; debug_t group; level_t def; - bool add_ms, ike_name, flush_line, append; + bool add_ms, ike_name, log_level, flush_line, append; char *time_format, *filename; time_format = lib->settings->get_str(lib->settings, @@ -495,6 +495,8 @@ static void load_file_logger(private_daemon_t *this, char *section, "%s.filelog.%s.time_add_ms", 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); flush_line = lib->settings->get_bool(lib->settings, "%s.filelog.%s.flush_line", FALSE, lib->ns, section); append = lib->settings->get_bool(lib->settings, @@ -508,7 +510,8 @@ static void load_file_logger(private_daemon_t *this, char *section, return; } - file_logger->set_options(file_logger, time_format, add_ms, ike_name); + file_logger->set_options(file_logger, time_format, add_ms, ike_name, + log_level); file_logger->open(file_logger, flush_line, append); def = lib->settings->get_int(lib->settings, "%s.filelog.%s.default", 1,