From bf3ffced7f3661b06582721c90e3bd38f015f91c Mon Sep 17 00:00:00 2001 From: Mike Kazantsev Date: Fri, 9 Feb 2024 17:13:27 +0500 Subject: [PATCH] perf: write out log line with time-prefix as one string to avoid holding locks (#40) Earlier implementation in #35 can loop while holding the lock, which is not a good idea, and composing prefix+line into one GString is simpler anyway. Using glib datetime and string formatting also allows to simplify implementation. --- src/out.c | 22 +++++++++------------- src/util.c | 20 -------------------- src/util.h | 1 - 3 files changed, 9 insertions(+), 34 deletions(-) diff --git a/src/out.c b/src/out.c index 941335e..1ecc76d 100644 --- a/src/out.c +++ b/src/out.c @@ -92,7 +92,15 @@ gboolean write_output_channel(GString *buffer) { GIOStatus write_status; GError *error = NULL; gsize written; - gsize written_timestamp = 0; + + if ( _timestamp_prefix != NULL ) { + GDateTime *dt = g_date_time_new_now_local(); + gchar *prefix = g_date_time_format(dt, _timestamp_prefix); + g_string_prepend(buffer, prefix); + g_free(prefix); + g_date_time_unref(dt); + } + while (TRUE) { if (_use_locks) { int res = flock(g_io_channel_unix_get_fd(_out_channel), LOCK_EX); @@ -101,18 +109,6 @@ gboolean write_output_channel(GString *buffer) { } } - if ( _timestamp_prefix != NULL && written_timestamp == 0 ) { - gchar *timestamp = compute_timestamp_prefix(_timestamp_prefix); - if ( timestamp != NULL ) { - write_status = g_io_channel_write_chars(_out_channel, timestamp, - strlen(timestamp), &written_timestamp, &error); - g_free(timestamp); - if (write_status == G_IO_STATUS_AGAIN) { - continue; - } - } - } - write_status = g_io_channel_write_chars(_out_channel, buffer->str, buffer->len, &written, &error); if (_use_locks) { diff --git a/src/util.c b/src/util.c index 0d36c42..8120d5c 100644 --- a/src/util.c +++ b/src/util.c @@ -116,26 +116,6 @@ gchar *compute_strftime_suffix(const gchar *str, const gchar *strftime_suffix) { return g_strdup_printf("%s%s", str, outstr); } -/** - * Format current timestamp prefix to prepend to a log line. - * - * @param strftime_prefix format with strftime placeholders to expand with current time. - * @return newly allocated string (free it with g_free) with the current timestamp prefix. - */ -gchar *compute_timestamp_prefix(const gchar *strftime_prefix) { - time_t t; - struct tm *tmp; - t = time(NULL); - tmp = localtime(&t); - g_assert(tmp != NULL); - char outstr[100]; - if (strftime(outstr, sizeof(outstr), strftime_prefix, tmp) == 0) { - g_critical("problem with strftime on %s", strftime_prefix); - return NULL; - } - return g_strdup(outstr); -} - /** * Compute absolute file path from directory path and file name * diff --git a/src/util.h b/src/util.h index f961052..0310eb4 100644 --- a/src/util.h +++ b/src/util.h @@ -7,7 +7,6 @@ glong get_file_size(const gchar *file_path); glong get_current_timestamp(); gchar *compute_strftime_suffix(const gchar *str, const gchar *strftime_suffix); -gchar *compute_timestamp_prefix(const gchar *strftime_prefix); gchar *get_unique_hexa_identifier(); glong get_file_inode(const gchar *file_path); glong get_fd_inode(int fd);