lightningd: simplify and optimize plugin escape handling.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -514,49 +514,30 @@ static const char *plugin_log_handle(struct plugin *plugin,
|
||||
|
||||
call_notifier = (level == LOG_BROKEN || level == LOG_UNUSUAL)? true : false;
|
||||
|
||||
/* Unescape log message. */
|
||||
tal_t *ctx = tal(NULL, char);
|
||||
const char *log_escaped = plugin->buffer + msgtok->start;
|
||||
const size_t log_escaped_len = msgtok->end - msgtok->start;
|
||||
struct json_escape *esc = json_escape_string_(ctx, log_escaped, log_escaped_len);
|
||||
const char *log_msg = json_escape_unescape(ctx, esc);
|
||||
/* Only bother unescaping and splitting if it has \ */
|
||||
if (memchr(plugin->buffer + msgtok->start, '\\', msgtok->end - msgtok->start)) {
|
||||
const char *log_escaped = plugin->buffer + msgtok->start;
|
||||
const size_t log_escaped_len = msgtok->end - msgtok->start;
|
||||
struct json_escape *esc = json_escape_string_(tmpctx, log_escaped, log_escaped_len);
|
||||
const char *log_msg = json_escape_unescape(tmpctx, esc);
|
||||
char **lines;
|
||||
|
||||
/* Find last line. */
|
||||
const char *log_msg2 = log_msg;
|
||||
const char *last_msg;
|
||||
while (true) {
|
||||
const char *msg_end = strchr(log_msg2, '\n');
|
||||
if (!msg_end) {
|
||||
break;
|
||||
}
|
||||
int msg_len = msg_end - log_msg2;
|
||||
if (msg_len > 0) {
|
||||
last_msg = log_msg2;
|
||||
}
|
||||
log_msg2 = msg_end + 1;
|
||||
}
|
||||
lines = tal_strsplit(tmpctx, log_msg, "\n", STR_EMPTY_OK);
|
||||
|
||||
/* Split to lines and log them separately. */
|
||||
while (true) {
|
||||
const char *msg_end = strchr(log_msg, '\n');
|
||||
if (!msg_end) {
|
||||
break;
|
||||
}
|
||||
int msg_len = msg_end - log_msg;
|
||||
if (msg_len > 0) {
|
||||
/* Call notifier only for the last message to avoid Python errors. */
|
||||
for (size_t i = 0; lines[i]; i++) {
|
||||
bool call_notifier2 = call_notifier;
|
||||
if (log_msg != last_msg) {
|
||||
/* Call notifier only for the last message to avoid Python errors. */
|
||||
if (lines[i+1] != NULL)
|
||||
call_notifier2 = false;
|
||||
}
|
||||
/* FIXME: Let plugin specify node_id? */
|
||||
log_(plugin->log, level, NULL, call_notifier2, "%.*s", msg_len, log_msg);
|
||||
log_(plugin->log, level, NULL, call_notifier2, "%s", lines[i]);
|
||||
}
|
||||
log_msg = msg_end + 1;
|
||||
} else {
|
||||
log_(plugin->log, level, NULL, call_notifier, "%.*s",
|
||||
msgtok->end - msgtok->start,
|
||||
plugin->buffer + msgtok->start);
|
||||
}
|
||||
|
||||
tal_free(ctx);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user