Skip to content

Commit 23997b2

Browse files
stariusrustyrussell
authored andcommitted
plugin_log_handle: accommodate multi-line messages
Previously, the code utilized JSON-encoded text to pass log messages to the log_() function, resulting in new lines being printed to the log as '\n'. This commit addresses this issue by unescaping and splitting the log message into lines, with each non-empty line being logged separately. It is deemed acceptable to pass non-printable characters to log_() since they are replaced with "?" in the logv() function, invoked by log_(). Therefore, leaving lines JSON unescaped is permissible. It's important to note that only the last log_() call may have call_notifier set to true. This adjustment is crucial to prevent Python tracebacks complaining about a broken pipe after lightningd exits. Fix #4912 For reference, the log of lightningd failing without a backend: ./lightningd/lightningd 2024-02-08T20:33:17.642Z INFO lightningd: v23.11-257-g968d6d6-modded Could not connect to bitcoind using bitcoin-cli. Is bitcoind running? Make sure you have bitcoind running and that bitcoin-cli is able to connect to bitcoind. You can verify that your Bitcoin Core installation is ready for use by running: $ bitcoin-cli echo 'hello world' 2024-02-08T20:33:18.227Z **BROKEN** plugin-bcli: Could not connect to bitcoind using bitcoin-cli. Is bitcoind running? 2024-02-08T20:33:18.227Z **BROKEN** plugin-bcli: Make sure you have bitcoind running and that bitcoin-cli is able to connect to bitcoind. 2024-02-08T20:33:18.227Z **BROKEN** plugin-bcli: You can verify that your Bitcoin Core installation is ready for use by running: 2024-02-08T20:33:18.227Z **BROKEN** plugin-bcli: $ bitcoin-cli echo 'hello world' 2024-02-08T20:33:18.227Z INFO plugin-bcli: Killing plugin: exited before we sent init The Bitcoin backend died. Changelog-Changed: Plugins: log messages containing \n are now split into multiple log lines, for neatness, and " is no longer turned into \".
1 parent 833b8d1 commit 23997b2

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

lightningd/plugin.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <ccan/ccan/tal/grab_file/grab_file.h>
44
#include <ccan/crc32c/crc32c.h>
55
#include <ccan/io/io.h>
6+
#include <ccan/json_escape/json_escape.h>
67
#include <ccan/mem/mem.h>
78
#include <ccan/opt/opt.h>
89
#include <ccan/pipecmd/pipecmd.h>
@@ -512,9 +513,50 @@ static const char *plugin_log_handle(struct plugin *plugin,
512513
}
513514

514515
call_notifier = (level == LOG_BROKEN || level == LOG_UNUSUAL)? true : false;
515-
/* FIXME: Let plugin specify node_id? */
516-
log_(plugin->log, level, NULL, call_notifier, "%.*s", msgtok->end - msgtok->start,
517-
plugin->buffer + msgtok->start);
516+
517+
/* Unescape log message. */
518+
tal_t *ctx = tal(NULL, char);
519+
const char *log_escaped = plugin->buffer + msgtok->start;
520+
const size_t log_escaped_len = msgtok->end - msgtok->start;
521+
struct json_escape *esc = json_escape_string_(ctx, log_escaped, log_escaped_len);
522+
const char *log_msg = json_escape_unescape(ctx, esc);
523+
524+
/* Find last line. */
525+
const char *log_msg2 = log_msg;
526+
const char *last_msg;
527+
while (true) {
528+
const char *msg_end = strchr(log_msg2, '\n');
529+
if (!msg_end) {
530+
break;
531+
}
532+
int msg_len = msg_end - log_msg2;
533+
if (msg_len > 0) {
534+
last_msg = log_msg2;
535+
}
536+
log_msg2 = msg_end + 1;
537+
}
538+
539+
/* Split to lines and log them separately. */
540+
while (true) {
541+
const char *msg_end = strchr(log_msg, '\n');
542+
if (!msg_end) {
543+
break;
544+
}
545+
int msg_len = msg_end - log_msg;
546+
if (msg_len > 0) {
547+
/* Call notifier only for the last message to avoid Python errors. */
548+
bool call_notifier2 = call_notifier;
549+
if (log_msg != last_msg) {
550+
call_notifier2 = false;
551+
}
552+
/* FIXME: Let plugin specify node_id? */
553+
log_(plugin->log, level, NULL, call_notifier2, "%.*s", msg_len, log_msg);
554+
}
555+
log_msg = msg_end + 1;
556+
}
557+
558+
tal_free(ctx);
559+
518560
return NULL;
519561
}
520562

0 commit comments

Comments
 (0)