file-logger: Take options as a struct and combine ms/us suffix options
References strongswan/strongswan#2475
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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 <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.
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user