From 5341f4055b25f1bbc1629018ed83fca3e0253b48 Mon Sep 17 00:00:00 2001 From: Matt Mundell Date: Thu, 9 May 2024 16:22:22 +0200 Subject: [PATCH] Add: set_log_tz --- base/logging.c | 44 +++++++++++++++++++++++++++++++++++++++++++- base/logging.h | 3 +++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/base/logging.c b/base/logging.c index 290d4a26..36223c05 100644 --- a/base/logging.c +++ b/base/logging.c @@ -34,6 +34,13 @@ */ #define G_LOG_DOMAIN "libgvm base" +/** + * @brief Timezone to use for logs. + * + * NULL means to use the current timezone. + */ +static gchar *log_tz = NULL; + /** * @struct gvm_logging_t * @brief Logging stores the parameters loaded from a log configuration @@ -69,7 +76,14 @@ get_time (gchar *time_fmt) { time_t now; struct tm ts; - gchar buf[80]; + gchar buf[80], *original_tz; + + if (log_tz) + { + original_tz = getenv ("TZ") ? g_strdup (getenv ("TZ")) : NULL; + setenv ("TZ", log_tz, 1); + tzset (); + } /* Get the current time. */ now = time (NULL); @@ -78,6 +92,19 @@ get_time (gchar *time_fmt) localtime_r (&now, &ts); strftime (buf, sizeof (buf), time_fmt, &ts); + if (log_tz) + { + /* Revert to stored TZ. */ + if (original_tz) + { + setenv ("TZ", original_tz, 1); + g_free (original_tz); + tzset (); + } + else + unsetenv ("TZ"); + } + return g_strdup_printf ("%s", buf); } @@ -873,6 +900,21 @@ check_log_file (gvm_logging_t *log_domain_entry) return 0; } +/** + * @brief Set the log timezone. + * + * This is the timezone used for dates in log messages. If NULL then + * the current timezone is used. + * + * @param tz Timezone. + */ +void +set_log_tz (const gchar *tz) +{ + g_free (log_tz); + log_tz = tz ? g_strdup (tz) : NULL; +} + /** * @brief Sets up routing of logdomains to log handlers. * diff --git a/base/logging.h b/base/logging.h index 5919e798..729a30ed 100644 --- a/base/logging.h +++ b/base/logging.h @@ -54,4 +54,7 @@ get_log_reference (void); void free_log_reference (void); +void +set_log_tz (const gchar *); + #endif /* not _GVM_LOGGING_H */