Skip to content

Commit bf3ffce

Browse files
authored
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.
1 parent 1c71065 commit bf3ffce

File tree

3 files changed

+9
-34
lines changed

3 files changed

+9
-34
lines changed

src/out.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ gboolean write_output_channel(GString *buffer) {
9292
GIOStatus write_status;
9393
GError *error = NULL;
9494
gsize written;
95-
gsize written_timestamp = 0;
95+
96+
if ( _timestamp_prefix != NULL ) {
97+
GDateTime *dt = g_date_time_new_now_local();
98+
gchar *prefix = g_date_time_format(dt, _timestamp_prefix);
99+
g_string_prepend(buffer, prefix);
100+
g_free(prefix);
101+
g_date_time_unref(dt);
102+
}
103+
96104
while (TRUE) {
97105
if (_use_locks) {
98106
int res = flock(g_io_channel_unix_get_fd(_out_channel), LOCK_EX);
@@ -101,18 +109,6 @@ gboolean write_output_channel(GString *buffer) {
101109
}
102110
}
103111

104-
if ( _timestamp_prefix != NULL && written_timestamp == 0 ) {
105-
gchar *timestamp = compute_timestamp_prefix(_timestamp_prefix);
106-
if ( timestamp != NULL ) {
107-
write_status = g_io_channel_write_chars(_out_channel, timestamp,
108-
strlen(timestamp), &written_timestamp, &error);
109-
g_free(timestamp);
110-
if (write_status == G_IO_STATUS_AGAIN) {
111-
continue;
112-
}
113-
}
114-
}
115-
116112
write_status = g_io_channel_write_chars(_out_channel, buffer->str,
117113
buffer->len, &written, &error);
118114
if (_use_locks) {

src/util.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,6 @@ gchar *compute_strftime_suffix(const gchar *str, const gchar *strftime_suffix) {
116116
return g_strdup_printf("%s%s", str, outstr);
117117
}
118118

119-
/**
120-
* Format current timestamp prefix to prepend to a log line.
121-
*
122-
* @param strftime_prefix format with strftime placeholders to expand with current time.
123-
* @return newly allocated string (free it with g_free) with the current timestamp prefix.
124-
*/
125-
gchar *compute_timestamp_prefix(const gchar *strftime_prefix) {
126-
time_t t;
127-
struct tm *tmp;
128-
t = time(NULL);
129-
tmp = localtime(&t);
130-
g_assert(tmp != NULL);
131-
char outstr[100];
132-
if (strftime(outstr, sizeof(outstr), strftime_prefix, tmp) == 0) {
133-
g_critical("problem with strftime on %s", strftime_prefix);
134-
return NULL;
135-
}
136-
return g_strdup(outstr);
137-
}
138-
139119
/**
140120
* Compute absolute file path from directory path and file name
141121
*

src/util.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
glong get_file_size(const gchar *file_path);
88
glong get_current_timestamp();
99
gchar *compute_strftime_suffix(const gchar *str, const gchar *strftime_suffix);
10-
gchar *compute_timestamp_prefix(const gchar *strftime_prefix);
1110
gchar *get_unique_hexa_identifier();
1211
glong get_file_inode(const gchar *file_path);
1312
glong get_fd_inode(int fd);

0 commit comments

Comments
 (0)