From 09706bdf8bd61f740a053aa1ea709fc4f65845c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Enrico=20Tr=C3=B6ger?= Date: Sun, 15 Nov 2020 14:15:42 +0100 Subject: [PATCH] Add microseconds to timestamp in debug log messages --- src/log.c | 2 +- src/msgwindow.c | 2 +- src/utils.c | 19 ++++++++++--------- src/utils.h | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/log.c b/src/log.c index 578ec5ff9..34c1f7ec6 100644 --- a/src/log.c +++ b/src/log.c @@ -137,7 +137,7 @@ static void handler_log(const gchar *domain, GLogLevelFlags level, const gchar * #endif } - time_str = utils_get_current_time_string(); + time_str = utils_get_current_time_string(TRUE); g_string_append_printf(log_buffer, "%s: %s %s: %s\n", time_str, domain, get_log_prefix(level), msg); diff --git a/src/msgwindow.c b/src/msgwindow.c index a82fd41ff..52bb03183 100644 --- a/src/msgwindow.c +++ b/src/msgwindow.c @@ -484,7 +484,7 @@ void msgwin_status_add_string(const gchar *string) gchar *statusmsg, *time_str; /* add a timestamp to status messages */ - time_str = utils_get_current_time_string(); + time_str = utils_get_current_time_string(FALSE); statusmsg = g_strconcat(time_str, ": ", string, NULL); g_free(time_str); diff --git a/src/utils.c b/src/utils.c index 9045ae9c6..5659bc736 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1012,16 +1012,17 @@ gint utils_parse_color_to_bgr(const gchar *spec) } -/* Returns: newly allocated string with the current time formatted HH:MM:SS. */ -gchar *utils_get_current_time_string(void) +/* Returns: newly allocated string with the current time formatted HH:MM:SS. + * If "include_microseconds" is TRUE, microseconds are appended. + * + * The returned string should be freed with g_free(). */ +gchar *utils_get_current_time_string(gboolean include_microseconds) { - const time_t tp = time(NULL); - const struct tm *tmval = localtime(&tp); - gchar *result = g_malloc0(9); - - strftime(result, 9, "%H:%M:%S", tmval); - result[8] = '\0'; - return result; + GDateTime *now = g_date_time_new_now_local(); + const gchar *format = include_microseconds ? "%H:%M:%S.%f" : "%H:%M:%S"; + gchar *time_string = g_date_time_format(now, format); + g_date_time_unref(now); + return time_string; } diff --git a/src/utils.h b/src/utils.h index 7de38d7ca..63904a433 100644 --- a/src/utils.h +++ b/src/utils.h @@ -287,7 +287,7 @@ gint utils_color_to_bgr(const GdkColor *color); gint utils_parse_color_to_bgr(const gchar *spec); -gchar *utils_get_current_time_string(void); +gchar *utils_get_current_time_string(gboolean include_microseconds); GIOChannel *utils_set_up_io_channel(gint fd, GIOCondition cond, gboolean nblock, GIOFunc func, gpointer data); -- 2.11.4.GIT