file-logger: Optionally log the level of each message

Fixes #3509.
This commit is contained in:
Tobias Brunner 2020-07-21 15:56:50 +02:00
parent e635d3dcbd
commit a3f5e38b7f
5 changed files with 44 additions and 16 deletions

View File

@ -29,6 +29,9 @@ charon.filelog.<name>.ike_name = no
Prefix each log entry with the connection name and a unique numerical
identifier for each IKE_SA.
charon.filelog.<name>.log_level = no
Add the log level of each message after the subsystem (e.g. [IKE2]).
charon.filelog.<name>.time_format
Prefix each log entry with a timestamp. The option accepts a format string
as passed to **strftime**(3).

View File

@ -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);

View File

@ -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);
}

View File

@ -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

View File

@ -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,