file-logger: Add support to log timestamp in microseconds

Closes strongswan/strongswan#2475
This commit is contained in:
Tobias Brunner
2024-10-25 14:48:17 +02:00
parent a2fba6db4a
commit 768fec23bc
5 changed files with 32 additions and 10 deletions
+15 -3
View File
@@ -70,6 +70,11 @@ struct private_file_logger_t {
*/
bool add_ms;
/**
* Add microseconds after the time string
*/
bool add_us;
/**
* Print the name/# of the IKE_SA?
*/
@@ -108,6 +113,7 @@ METHOD(logger_t, log_, void,
time_t s;
size_t time_len;
u_int ms = 0;
long us = 0;
this->lock->read_lock(this->lock);
if (!this->out)
@@ -121,10 +127,15 @@ 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_ms && sizeof(timestr) - time_len > 4)
if (this->add_us && sizeof(timestr) - time_len > 7)
{
snprintf(&timestr[time_len], sizeof(timestr)-time_len, ".%06d", us);
}
else if (this->add_ms && sizeof(timestr) - time_len > 4)
{
snprintf(&timestr[time_len], sizeof(timestr)-time_len, ".%03u", ms);
}
@@ -258,13 +269,14 @@ 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,
bool log_level, bool json)
private_file_logger_t *this, char *time_format, bool add_ms, bool add_us,
bool ike_name, bool log_level, bool json)
{
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;
+3 -1
View File
@@ -51,12 +51,14 @@ struct file_logger_t {
* @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
*/
void (*set_options) (file_logger_t *this, char *time_format, bool add_ms,
bool ike_name, bool log_level, bool json);
bool add_us, bool ike_name, bool log_level, bool json);
/**
* Open (or reopen) the log file according to the given parameters