|
1 | 1 | #include "config.h" |
2 | 2 | #include <ccan/err/err.h> |
3 | 3 | #include <ccan/io/io.h> |
| 4 | +#include <ccan/json_escape/json_escape.h> |
4 | 5 | #include <ccan/read_write_all/read_write_all.h> |
5 | 6 | #include <ccan/str/hex/hex.h> |
6 | 7 | #include <ccan/tal/link/link.h> |
7 | 8 | #include <ccan/tal/str/str.h> |
8 | 9 | #include <common/configvar.h> |
9 | 10 | #include <common/json_command.h> |
10 | | -#include <ccan/json_escape/json_escape.h> |
11 | 11 | #include <common/json_param.h> |
12 | 12 | #include <common/memleak.h> |
13 | 13 | #include <errno.h> |
@@ -565,41 +565,57 @@ void logv(struct logger *log, enum log_level level, |
565 | 565 | { |
566 | 566 | int save_errno = errno; |
567 | 567 | struct log_entry *l = new_log_entry(log, level, node_id); |
| 568 | + char *log_msg = NULL; |
568 | 569 |
|
569 | 570 | /* This is WARN_UNUSED_RESULT, because everyone should somehow deal |
570 | 571 | * with OOM, even though nobody does. */ |
571 | | - if (vasprintf(&l->log, fmt, ap) == -1) |
| 572 | + if (vasprintf(&log_msg, fmt, ap) == -1) |
572 | 573 | abort(); |
573 | 574 |
|
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); |
584 | 580 |
|
585 | 581 | /* Sanitize any non-printable characters, and replace with '?' |
586 | 582 | */ |
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] = '?'; |
592 | 586 |
|
593 | | - maybe_print(log, line_entry); |
594 | | - maybe_notify_log(log, line_entry); |
| 587 | + maybe_print(log, l); |
| 588 | + maybe_notify_log(log, l); |
| 589 | + |
| 590 | + add_entry(log, &l); |
| 591 | + } else { |
| 592 | + char **lines = |
| 593 | + tal_strsplit(log, unescaped_log, "\n", STR_NO_EMPTY); |
595 | 594 |
|
596 | | - add_entry(log, &line_entry); |
| 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 | + } |
| 612 | + tal_free(lines); |
597 | 613 | } |
598 | 614 |
|
599 | 615 | if (call_notifier) |
600 | 616 | notify_warning(log->log_book->ld, l); |
601 | 617 |
|
602 | | - tal_free(ctx); |
| 618 | + tal_free(unescaped_log); |
603 | 619 | errno = save_errno; |
604 | 620 | } |
605 | 621 |
|
|
0 commit comments