diff --git a/base/logging.c b/base/logging.c index 290d4a26..cd825c82 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,13 @@ 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 +91,18 @@ 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 +898,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 */