Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance plugin_log_handle to accommodate multi-line messages #7013

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ccan/ccan/tal/grab_file/grab_file.h>
#include <ccan/crc32c/crc32c.h>
#include <ccan/io/io.h>
#include <ccan/json_escape/json_escape.h>
#include <ccan/mem/mem.h>
#include <ccan/opt/opt.h>
#include <ccan/pipecmd/pipecmd.h>
Expand Down Expand Up @@ -503,9 +504,27 @@ static const char *plugin_log_handle(struct plugin *plugin,
}

call_notifier = (level == LOG_BROKEN || level == LOG_UNUSUAL)? true : false;
/* FIXME: Let plugin specify node_id? */
log_(plugin->log, level, NULL, call_notifier, "%.*s", msgtok->end - msgtok->start,
plugin->buffer + msgtok->start);

/* 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;

lines = tal_strsplit(tmpctx, log_msg, "\n", STR_EMPTY_OK);

for (size_t i = 0; lines[i]; i++) {
/* FIXME: Let plugin specify node_id? */
log_(plugin->log, level, NULL, call_notifier, "%s", lines[i]);
}
} else {
log_(plugin->log, level, NULL, call_notifier, "%.*s",
msgtok->end - msgtok->start,
plugin->buffer + msgtok->start);
}

return NULL;
}

Expand Down
11 changes: 10 additions & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ def test_htlc_accepted_hook_forward_restart(node_factory, executor):
def test_warning_notification(node_factory):
""" test 'warning' notifications
"""
l1 = node_factory.get_node(options={'plugin': os.path.join(os.getcwd(), 'tests/plugins/pretend_badlog.py')}, broken_log=r'Test warning notification\(for broken event\)')
l1 = node_factory.get_node(options={'plugin': os.path.join(os.getcwd(), 'tests/plugins/pretend_badlog.py')}, broken_log=r'Test warning notification\(for broken event\)|LINE[12]')

# 1. test 'warn' level
event = "Test warning notification(for unusual event)"
Expand All @@ -1283,6 +1283,15 @@ def test_warning_notification(node_factory):
l1.daemon.wait_for_log('plugin-pretend_badlog.py: source: plugin-pretend_badlog.py')
l1.daemon.wait_for_log('plugin-pretend_badlog.py: log: Test warning notification\\(for broken event\\)')

# Test linesplitting while we're here
l1.rpc.call('pretendbad', {'event': 'LINE1\nLINE2', 'level': 'error'})
l1.daemon.wait_for_log(r'\*\*BROKEN\*\* plugin-pretend_badlog.py: LINE1')
l1.daemon.wait_for_log(r'\*\*BROKEN\*\* plugin-pretend_badlog.py: LINE2')
l1.daemon.wait_for_log('plugin-pretend_badlog.py: Received warning')
l1.daemon.wait_for_log('plugin-pretend_badlog.py: log: LINE1')
l1.daemon.wait_for_log('plugin-pretend_badlog.py: Received warning')
l1.daemon.wait_for_log('plugin-pretend_badlog.py: log: LINE2')


def test_invoice_payment_notification(node_factory):
"""
Expand Down
Loading