Skip to content

Commit 37d0e51

Browse files
committed
lightningd: simplify and optimize plugin escape handling.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 23997b2 commit 37d0e51

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed

lightningd/plugin.c

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -514,49 +514,30 @@ static const char *plugin_log_handle(struct plugin *plugin,
514514

515515
call_notifier = (level == LOG_BROKEN || level == LOG_UNUSUAL)? true : false;
516516

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-
}
517+
/* Only bother unescaping and splitting if it has \ */
518+
if (memchr(plugin->buffer + msgtok->start, '\\', msgtok->end - msgtok->start)) {
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_(tmpctx, log_escaped, log_escaped_len);
522+
const char *log_msg = json_escape_unescape(tmpctx, esc);
523+
char **lines;
538524

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. */
525+
lines = tal_strsplit(tmpctx, log_msg, "\n", STR_EMPTY_OK);
526+
527+
for (size_t i = 0; lines[i]; i++) {
548528
bool call_notifier2 = call_notifier;
549-
if (log_msg != last_msg) {
529+
/* Call notifier only for the last message to avoid Python errors. */
530+
if (lines[i+1] != NULL)
550531
call_notifier2 = false;
551-
}
552532
/* FIXME: Let plugin specify node_id? */
553-
log_(plugin->log, level, NULL, call_notifier2, "%.*s", msg_len, log_msg);
533+
log_(plugin->log, level, NULL, call_notifier2, "%s", lines[i]);
554534
}
555-
log_msg = msg_end + 1;
535+
} else {
536+
log_(plugin->log, level, NULL, call_notifier, "%.*s",
537+
msgtok->end - msgtok->start,
538+
plugin->buffer + msgtok->start);
556539
}
557540

558-
tal_free(ctx);
559-
560541
return NULL;
561542
}
562543

0 commit comments

Comments
 (0)