Skip to content

Commit f3096e2

Browse files
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 <[email protected]>
1 parent 86b160a commit f3096e2

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

lightningd/log.c

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#include "config.h"
22
#include <ccan/err/err.h>
33
#include <ccan/io/io.h>
4+
#include <ccan/json_escape/json_escape.h>
45
#include <ccan/read_write_all/read_write_all.h>
56
#include <ccan/str/hex/hex.h>
67
#include <ccan/tal/link/link.h>
78
#include <ccan/tal/str/str.h>
89
#include <common/configvar.h>
910
#include <common/json_command.h>
10-
#include <ccan/json_escape/json_escape.h>
1111
#include <common/json_param.h>
1212
#include <common/memleak.h>
1313
#include <errno.h>
@@ -565,41 +565,58 @@ void logv(struct logger *log, enum log_level level,
565565
{
566566
int save_errno = errno;
567567
struct log_entry *l = new_log_entry(log, level, node_id);
568+
char *log_msg = NULL;
568569

569570
/* This is WARN_UNUSED_RESULT, because everyone should somehow deal
570571
* with OOM, even though nobody does. */
571-
if (vasprintf(&l->log, fmt, ap) == -1)
572+
if (vasprintf(&log_msg, fmt, ap) == -1)
572573
abort();
573574

574-
tal_t *ctx = tal(NULL, char);
575-
const char *log_line = tal_strdup(
576-
ctx, json_escape_unescape(ctx, (struct json_escape *)l->log));
577-
char **lines = tal_strsplit(ctx, log_line, "\n", STR_NO_EMPTY);
578-
579-
/* Split to lines and log them separately. */
580-
for (size_t i = 0; i < tal_count(lines) - 1; i++) {
581-
struct log_entry *line_entry =
582-
new_log_entry(log, level, node_id);
583-
line_entry->log = tal_strdup(ctx, lines[i]);
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);
584580

585581
/* Sanitize any non-printable characters, and replace with '?'
586582
*/
587-
size_t line_len = strlen(line_entry->log);
588-
for (size_t i = 0; i < line_len; i++)
589-
if (line_entry->log[i] < ' ' ||
590-
line_entry->log[i] >= 0x7f)
591-
line_entry->log[i] = '?';
583+
for (size_t i = 0; i < log_len; i++)
584+
if (l->log[i] < ' ' || l->log[i] >= 0x7f)
585+
l->log[i] = '?';
592586

593-
maybe_print(log, line_entry);
594-
maybe_notify_log(log, line_entry);
587+
maybe_print(log, l);
588+
maybe_notify_log(log, l);
595589

596-
add_entry(log, &line_entry);
590+
add_entry(log, &l);
591+
} else {
592+
char **lines =
593+
tal_strsplit(log, 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+
free(l->log);
612+
}
613+
tal_free(lines);
597614
}
598615

599616
if (call_notifier)
600617
notify_warning(log->log_book->ld, l);
601618

602-
tal_free(ctx);
619+
tal_free(unescaped_log);
603620
errno = save_errno;
604621
}
605622

0 commit comments

Comments
 (0)