Skip to content

Commit c83501c

Browse files
lightningd/log: Improve log handling
Changelog-Added: Improved log handling to save memory. Signed-off-by: Nishant Bansal <[email protected]>
1 parent 62ed812 commit c83501c

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

lightningd/log.c

+27-11
Original file line numberDiff line numberDiff line change
@@ -572,32 +572,48 @@ void logv(struct logger *log, enum log_level level,
572572
if (vasprintf(&log_msg, fmt, ap) == -1)
573573
abort();
574574

575-
char **lines = tal_strsplit(
576-
log, json_escape_unescape(log, (struct json_escape *)log_msg), "\n",
577-
STR_NO_EMPTY);
578-
579-
/* Split to lines and log them separately. */
580-
for (size_t j = 0; lines[j]; j++) {
581-
l->log = tal_strdup(log, lines[j]);
575+
const char *unescaped_log =
576+
json_escape_unescape(log, (struct json_escape *)log_msg);
577+
if (!strchr(log_msg, '\\') || !unescaped_log) {
578+
l->log = log_msg;
579+
size_t log_len = strlen(l->log);
582580

583581
/* Sanitize any non-printable characters, and replace with '?'
584582
*/
585-
size_t line_len = strlen(l->log);
586-
for (size_t i = 0; i < line_len; i++)
583+
for (size_t i = 0; i < log_len; i++)
587584
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
588585
l->log[i] = '?';
589586

590587
maybe_print(log, l);
591588
maybe_notify_log(log, l);
592589

593590
add_entry(log, &l);
591+
} else {
592+
char **lines =
593+
tal_strsplit(log, take(unescaped_log), "\n", STR_NO_EMPTY);
594+
595+
/* Split to lines and log them separately. */
596+
for (size_t j = 0; lines[j]; j++) {
597+
l->log = strdup(lines[j]);
598+
599+
/* Sanitize any non-printable characters, and replace
600+
* with '?'
601+
*/
602+
size_t line_len = strlen(l->log);
603+
for (size_t i = 0; i < line_len; i++)
604+
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
605+
l->log[i] = '?';
606+
607+
maybe_print(log, l);
608+
maybe_notify_log(log, l);
609+
610+
add_entry(log, &l);
611+
}
594612
}
595613

596614
if (call_notifier)
597615
notify_warning(log->log_book->ld, l);
598616

599-
tal_free(lines);
600-
free(log_msg);
601617
errno = save_errno;
602618
}
603619

0 commit comments

Comments
 (0)