file-logger: Add support to log timestamp in microseconds
Closes strongswan/strongswan#2475
This commit is contained in:
@@ -50,6 +50,10 @@ charon.filelog.<name>.time_add_ms = no
|
||||
Adds the milliseconds within the current second after the timestamp
|
||||
(separated by a dot, so _time_format_ should end with %S or %T).
|
||||
|
||||
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 {}
|
||||
Section to define syslog loggers, see LOGGER CONFIGURATION in
|
||||
**strongswan.conf**(5).
|
||||
|
||||
@@ -383,12 +383,14 @@ 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, log_level, json;
|
||||
bool add_ms, add_us, ike_name, log_level, json;
|
||||
|
||||
time_format = conftest->test->get_str(conftest->test,
|
||||
"log.%s.time_format", NULL, section);
|
||||
add_ms = conftest->test->get_bool(conftest->test,
|
||||
"log.%s.time_add_ms", FALSE, section);
|
||||
add_us = conftest->test->get_bool(conftest->test,
|
||||
"log.%s.time_add_us", FALSE, section);
|
||||
ike_name = conftest->test->get_bool(conftest->test,
|
||||
"log.%s.ike_name", FALSE, section);
|
||||
log_level = conftest->test->get_bool(conftest->test,
|
||||
@@ -396,7 +398,7 @@ static void load_logger_options(file_logger_t *logger, char *section)
|
||||
json = conftest->test->get_bool(conftest->test,
|
||||
"log.%s.json", FALSE, section);
|
||||
|
||||
logger->set_options(logger, time_format, add_ms, ike_name, log_level, json);
|
||||
logger->set_options(logger, time_format, add_ms, add_us, ike_name, log_level, json);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -462,7 +464,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, FALSE, FALSE);
|
||||
logger->set_options(logger, NULL, FALSE, FALSE, FALSE, FALSE, FALSE);
|
||||
logger->open(logger, FALSE, FALSE);
|
||||
logger->set_level(logger, DBG_ANY, LEVEL_CTRL);
|
||||
charon->bus->add_logger(charon->bus, &logger->logger);
|
||||
|
||||
@@ -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(×tr[time_len], sizeof(timestr)-time_len, ".%06d", us);
|
||||
}
|
||||
else if (this->add_ms && sizeof(timestr) - time_len > 4)
|
||||
{
|
||||
snprintf(×tr[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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -493,13 +493,15 @@ 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, log_level, json, flush_line, append;
|
||||
bool add_ms, add_us, ike_name, log_level, json, flush_line, append;
|
||||
char *time_format, *filename;
|
||||
|
||||
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,
|
||||
@@ -519,8 +521,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,
|
||||
log_level, json);
|
||||
file_logger->set_options(file_logger, time_format, add_ms, add_us,
|
||||
ike_name, log_level, json);
|
||||
file_logger->open(file_logger, flush_line, append);
|
||||
|
||||
def = lib->settings->get_int(lib->settings, "%s.filelog.%s.default", 1,
|
||||
|
||||
Reference in New Issue
Block a user