Skip to content

Commit 545970f

Browse files
committed
lightningd: use static buffer for common log path.
This speeds logging slightly, but most of the time is actually spent in fflush() (which we need). Result: FAILED tests/test_connection.py::test_bench - assert 51.54966354370117 == 0 Signed-off-by: Rusty Russell <[email protected]>
1 parent d6261ad commit 545970f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lightningd/log.c

+16-6
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ static void log_to_files(const char *log_prefix,
208208
{
209209
char tstamp[sizeof("YYYY-mm-ddTHH:MM:SS.nnnZ ")];
210210
char *entry, nodestr[hex_str_size(PUBKEY_CMPR_LEN)];
211+
char buf[sizeof("%s%s%s %s-%s: %s\n")
212+
+ strlen(log_prefix)
213+
+ sizeof(tstamp)
214+
+ strlen(level_prefix(level))
215+
+ sizeof(nodestr)
216+
+ strlen(entry_prefix)
217+
+ strlen(str)];
211218
bool filtered;
212219

213220
if (print_timestamps) {
@@ -235,14 +242,17 @@ static void log_to_files(const char *log_prefix,
235242
entry_prefix, str, dir, hex);
236243
tal_free(hex);
237244
} else {
245+
size_t len;
246+
entry = buf;
238247
if (!node_id)
239-
entry = tal_fmt(tmpctx, "%s%s%s %s: %s\n",
240-
log_prefix, tstamp, level_prefix(level), entry_prefix, str);
248+
len = snprintf(buf, "%s%s%s %s: %s\n",
249+
log_prefix, tstamp, level_prefix(level), entry_prefix, str);
241250
else
242-
entry = tal_fmt(tmpctx, "%s%s%s %s-%s: %s\n",
243-
log_prefix, tstamp, level_prefix(level),
244-
nodestr,
245-
entry_prefix, str);
251+
len = snprintf(buf, "%s%s%s %s-%s: %s\n",
252+
log_prefix, tstamp, level_prefix(level),
253+
nodestr,
254+
entry_prefix, str);
255+
assert(len < sizeof(buf));
246256
}
247257

248258
/* In complex configurations, we tell loggers to overshare: then we

0 commit comments

Comments
 (0)