]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
file-logger: Add support to log timestamp in microseconds
authorTobias Brunner <tobias@strongswan.org>
Wed, 9 Oct 2024 15:40:55 +0000 (17:40 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 25 Oct 2024 12:48:17 +0000 (14:48 +0200)
Closes strongswan/strongswan#2475

conf/options/charon-logging.opt
src/conftest/conftest.c
src/libcharon/bus/listeners/file_logger.c
src/libcharon/bus/listeners/file_logger.h
src/libcharon/daemon.c

index 79958f3078b9c34ffd00e6eea25c91dedacf4bfd..6b67d3b6657def626a92b79ca04f25e201b2d169 100644 (file)
@@ -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).
index 7e46e61567085c562f4da8460a92dd4ea2b00af2..02713b23fca54add3b242726d2a77bc55e0029cf 100644 (file)
@@ -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);
index b2ac7b36ec65dfadc8248e53ae69e2ccb3ec3305..0078e20c3901ef4f802fdf868556385c9c5f8532 100644 (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;
index a26b6511072a28bc5b1ee626966cdcb1a5a36415..508c86b150b97914ea055f699dc7c12f9c9ce309 100644 (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
index 5722d16d8ffa60e12984b56cfc64b014e540286d..013f21f9a3bd25ae35216bb7c5bd5dc10943909c 100644 (file)
@@ -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,