file-logger: Take options as a struct and combine ms/us suffix options
References strongswan/strongswan#2475
This commit is contained in:
@@ -46,13 +46,10 @@ charon.filelog.<name>.time_format
|
|||||||
Prefix each log entry with a timestamp. The option accepts a format string
|
Prefix each log entry with a timestamp. The option accepts a format string
|
||||||
as passed to **strftime**(3).
|
as passed to **strftime**(3).
|
||||||
|
|
||||||
charon.filelog.<name>.time_add_ms = no
|
charon.filelog.<name>.time_precision =
|
||||||
Adds the milliseconds within the current second after the timestamp
|
Add the milliseconds (_ms_) or microseconds (_us_) within the current second
|
||||||
(separated by a dot, so _time_format_ should end with %S or %T).
|
after the timestamp (separated by a dot, so _time_format_ should end
|
||||||
|
with %S or %T). By default, nothing is added.
|
||||||
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.syslog {}
|
charon.syslog {}
|
||||||
Section to define syslog loggers, see LOGGER CONFIGURATION in
|
Section to define syslog loggers, see LOGGER CONFIGURATION in
|
||||||
|
|||||||
+18
-12
@@ -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)
|
static void load_logger_options(file_logger_t *logger, char *section)
|
||||||
{
|
{
|
||||||
char *time_format;
|
file_logger_options_t options;
|
||||||
bool add_ms, add_us, ike_name, log_level, json;
|
|
||||||
|
|
||||||
time_format = conftest->test->get_str(conftest->test,
|
options.time_format = conftest->test->get_str(conftest->test,
|
||||||
"log.%s.time_format", NULL, section);
|
"log.%s.time_format", NULL, section);
|
||||||
add_ms = conftest->test->get_bool(conftest->test,
|
options.time_precision = file_logger_time_precision_parse(
|
||||||
"log.%s.time_add_ms", FALSE, section);
|
conftest->test->get_str(conftest->test,
|
||||||
add_us = conftest->test->get_bool(conftest->test,
|
"log.%s.time_precision", NULL, section));
|
||||||
"log.%s.time_add_us", FALSE, section);
|
/* handle legacy option */
|
||||||
ike_name = conftest->test->get_bool(conftest->test,
|
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.%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);
|
"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);
|
"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;
|
int sig;
|
||||||
char *suite_file = "suite.conf", *test_file = NULL, *preload, *plugins;
|
char *suite_file = "suite.conf", *test_file = NULL, *preload, *plugins;
|
||||||
file_logger_t *logger;
|
file_logger_t *logger;
|
||||||
|
file_logger_options_t options = {};
|
||||||
|
|
||||||
if (!library_init(NULL, "conftest"))
|
if (!library_init(NULL, "conftest"))
|
||||||
{
|
{
|
||||||
@@ -464,7 +470,7 @@ int main(int argc, char *argv[])
|
|||||||
lib->credmgr->add_set(lib->credmgr, &conftest->creds->set);
|
lib->credmgr->add_set(lib->credmgr, &conftest->creds->set);
|
||||||
|
|
||||||
logger = file_logger_create("stdout");
|
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->open(logger, FALSE, FALSE);
|
||||||
logger->set_level(logger, DBG_ANY, LEVEL_CTRL);
|
logger->set_level(logger, DBG_ANY, LEVEL_CTRL);
|
||||||
charon->bus->add_logger(charon->bus, &logger->logger);
|
charon->bus->add_logger(charon->bus, &logger->logger);
|
||||||
|
|||||||
@@ -66,14 +66,9 @@ struct private_file_logger_t {
|
|||||||
char *time_format;
|
char *time_format;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add milliseconds after the time string
|
* Add milliseconds/microseconds after the time string
|
||||||
*/
|
*/
|
||||||
bool add_ms;
|
file_logger_time_precision_t time_precision;
|
||||||
|
|
||||||
/**
|
|
||||||
* Add microseconds after the time string
|
|
||||||
*/
|
|
||||||
bool add_us;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print the name/# of the IKE_SA?
|
* Print the name/# of the IKE_SA?
|
||||||
@@ -112,8 +107,6 @@ METHOD(logger_t, log_, void,
|
|||||||
timeval_t tv;
|
timeval_t tv;
|
||||||
time_t s;
|
time_t s;
|
||||||
size_t time_len;
|
size_t time_len;
|
||||||
u_int ms = 0;
|
|
||||||
long us = 0;
|
|
||||||
|
|
||||||
this->lock->read_lock(this->lock);
|
this->lock->read_lock(this->lock);
|
||||||
if (!this->out)
|
if (!this->out)
|
||||||
@@ -126,18 +119,20 @@ METHOD(logger_t, log_, void,
|
|||||||
{
|
{
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
s = tv.tv_sec;
|
s = tv.tv_sec;
|
||||||
ms = tv.tv_usec / 1000;
|
|
||||||
us = tv.tv_usec;
|
|
||||||
localtime_r(&s, &tm);
|
localtime_r(&s, &tm);
|
||||||
time_len = strftime(timestr, sizeof(timestr), this->time_format, &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,
|
METHOD(file_logger_t, set_options, void,
|
||||||
private_file_logger_t *this, char *time_format, bool add_ms, bool add_us,
|
private_file_logger_t *this, file_logger_options_t *options)
|
||||||
bool ike_name, bool log_level, bool json)
|
|
||||||
{
|
{
|
||||||
this->lock->write_lock(this->lock);
|
this->lock->write_lock(this->lock);
|
||||||
free(this->time_format);
|
free(this->time_format);
|
||||||
this->time_format = strdupnull(time_format);
|
this->time_format = strdupnull(options->time_format);
|
||||||
this->add_ms = add_ms;
|
this->time_precision = options->time_precision;
|
||||||
this->add_us = add_us;
|
this->ike_name = options->ike_name;
|
||||||
this->ike_name = ike_name;
|
this->log_level = options->log_level;
|
||||||
this->log_level = log_level;
|
this->json = options->json;
|
||||||
this->json = json;
|
|
||||||
this->lock->unlock(this->lock);
|
this->lock->unlock(this->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,6 +356,22 @@ METHOD(file_logger_t, destroy, void,
|
|||||||
free(this);
|
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.
|
* Described in header.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2012-2020 Tobias Brunner
|
* Copyright (C) 2012-2024 Tobias Brunner
|
||||||
* Copyright (C) 2006 Martin Willi
|
* Copyright (C) 2006 Martin Willi
|
||||||
*
|
*
|
||||||
* Copyright (C) secunet Security Networks AG
|
* Copyright (C) secunet Security Networks AG
|
||||||
@@ -26,6 +26,8 @@
|
|||||||
#include <bus/listeners/logger.h>
|
#include <bus/listeners/logger.h>
|
||||||
|
|
||||||
typedef struct file_logger_t file_logger_t;
|
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.
|
* Logger to files which implements listener_t.
|
||||||
@@ -48,17 +50,9 @@ struct file_logger_t {
|
|||||||
/**
|
/**
|
||||||
* Set options used by this logger
|
* Set options used by this logger
|
||||||
*
|
*
|
||||||
* @param time_format format of timestamp prefix, as in strftime(), cloned
|
* @param options options for this file logger
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
void (*set_options) (file_logger_t *this, char *time_format, bool add_ms,
|
void (*set_options) (file_logger_t *this, file_logger_options_t *options);
|
||||||
bool add_us, bool ike_name, bool log_level, bool json);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open (or reopen) the log file according to the given parameters
|
* 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);
|
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.
|
* Constructor to create a file_logger_t object.
|
||||||
*
|
*
|
||||||
|
|||||||
+26
-19
@@ -491,29 +491,37 @@ static void load_file_logger(private_daemon_t *this, char *section,
|
|||||||
linked_list_t *current_loggers)
|
linked_list_t *current_loggers)
|
||||||
{
|
{
|
||||||
file_logger_t *file_logger;
|
file_logger_t *file_logger;
|
||||||
|
file_logger_options_t options;
|
||||||
debug_t group;
|
debug_t group;
|
||||||
level_t def;
|
level_t def;
|
||||||
bool add_ms, add_us, ike_name, log_level, json, flush_line, append;
|
bool flush_line, append;
|
||||||
char *time_format, *filename;
|
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);
|
||||||
|
|
||||||
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);
|
|
||||||
flush_line = lib->settings->get_bool(lib->settings,
|
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,
|
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,
|
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);
|
file_logger = add_file_logger(this, filename, current_loggers);
|
||||||
if (!file_logger)
|
if (!file_logger)
|
||||||
@@ -521,8 +529,7 @@ static void load_file_logger(private_daemon_t *this, char *section,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_logger->set_options(file_logger, time_format, add_ms, add_us,
|
file_logger->set_options(file_logger, &options);
|
||||||
ike_name, log_level, json);
|
|
||||||
file_logger->open(file_logger, flush_line, append);
|
file_logger->open(file_logger, flush_line, append);
|
||||||
|
|
||||||
def = lib->settings->get_int(lib->settings, "%s.filelog.%s.default", 1,
|
def = lib->settings->get_int(lib->settings, "%s.filelog.%s.default", 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user