Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: set_log_tz #815

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 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,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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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.
*
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 */
Loading