Skip to content

Commit

Permalink
Merge branch 'main' into get-credentials-segf
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmundell authored Jun 3, 2024
2 parents 5f22516 + 7bcf632 commit dbb3f59
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
44 changes: 43 additions & 1 deletion base/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions base/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ get_log_reference (void);
void
free_log_reference (void);

void
set_log_tz (const gchar *);

#endif /* not _GVM_LOGGING_H */

0 comments on commit dbb3f59

Please sign in to comment.