From 62ed812209caac7abbce7edf1ec36716fffbfadd Mon Sep 17 00:00:00 2001 From: Nishant Bansal Date: Mon, 16 Dec 2024 21:12:34 +0530 Subject: [PATCH] lightningd/log: Improve logging to handle multi-line messages Changelog-Added: Improved logging format in `lightningd/log` to handle multi-line messages by unescaping '\n' and logging each line separately. Signed-off-by: Nishant Bansal --- lightningd/log.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lightningd/log.c b/lightningd/log.c index eb03450f7047..1d64e77d48e8 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -1,6 +1,7 @@ #include "config.h" #include #include +#include #include #include #include @@ -564,27 +565,39 @@ void logv(struct logger *log, enum log_level level, { int save_errno = errno; struct log_entry *l = new_log_entry(log, level, node_id); + char *log_msg = NULL; /* This is WARN_UNUSED_RESULT, because everyone should somehow deal * with OOM, even though nobody does. */ - if (vasprintf(&l->log, fmt, ap) == -1) + if (vasprintf(&log_msg, fmt, ap) == -1) abort(); - size_t log_len = strlen(l->log); + char **lines = tal_strsplit( + log, json_escape_unescape(log, (struct json_escape *)log_msg), "\n", + STR_NO_EMPTY); - /* Sanitize any non-printable characters, and replace with '?' */ - for (size_t i=0; ilog[i] < ' ' || l->log[i] >= 0x7f) - l->log[i] = '?'; + /* Split to lines and log them separately. */ + for (size_t j = 0; lines[j]; j++) { + l->log = tal_strdup(log, lines[j]); - maybe_print(log, l); - maybe_notify_log(log, l); + /* Sanitize any non-printable characters, and replace with '?' + */ + size_t line_len = strlen(l->log); + for (size_t i = 0; i < line_len; i++) + if (l->log[i] < ' ' || l->log[i] >= 0x7f) + l->log[i] = '?'; - add_entry(log, &l); + maybe_print(log, l); + maybe_notify_log(log, l); + + add_entry(log, &l); + } if (call_notifier) notify_warning(log->log_book->ld, l); + tal_free(lines); + free(log_msg); errno = save_errno; }