Skip to content

Commit

Permalink
perf: write out log line with time-prefix as one string to avoid hold…
Browse files Browse the repository at this point in the history
…ing 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.
  • Loading branch information
mk-fg authored Feb 9, 2024
1 parent 1c71065 commit bf3ffce
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 34 deletions.
22 changes: 9 additions & 13 deletions src/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
20 changes: 0 additions & 20 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 0 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bf3ffce

Please sign in to comment.