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

perf: write out log line with time-prefix as one string to avoid holding locks #40

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
22 changes: 9 additions & 13 deletions src/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,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 @@ -102,18 +110,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 @@ -115,26 +115,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