Browse Source

plugin: Handle log notifications from plugins

Logs are parsed and injected into the main daemon's logs.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
plugin-6
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
a304db9be2
  1. 17
      contrib/plugins/helloworld.py
  2. 34
      lightningd/plugin.c
  3. 4
      tests/test_plugin.py

17
contrib/plugins/helloworld.py

@ -45,12 +45,27 @@ def json_getmanifest(request):
}
def plugin_log(message, level="info"):
payload = {
"jsonrpc": "2.0",
"method": "log",
"params": {
"level": level,
"message": message,
}
}
json.dump(payload, fp=sys.stdout)
sys.stdout.write('\n\n')
sys.stdout.flush()
def json_init(request, options, configuration):
"""The main daemon is telling us the relevant cli options
"""
global greeting
greeting = request['params']['options']['greeting']
plugin_log("Plugin helloworld.py initialized with greeting \"{}\"".format(greeting), "debug")
return "ok"

34
lightningd/plugin.c

@ -234,6 +234,40 @@ static void plugin_request_queue(struct plugin *plugin,
io_wake(plugin);
}
static void plugin_log_handle(struct plugin *plugin, const jsmntok_t *paramstok)
{
const jsmntok_t *msgtok, *leveltok;
enum log_level level;
msgtok = json_get_member(plugin->buffer, paramstok, "message");
leveltok = json_get_member(plugin->buffer, paramstok, "level");
if (!msgtok || msgtok->type != JSMN_STRING) {
plugin_kill(plugin, "Log notification from plugin doesn't have "
"a string \"message\" field");
return;
}
if (!leveltok || json_tok_streq(plugin->buffer, leveltok, "info"))
level = LOG_INFORM;
else if (json_tok_streq(plugin->buffer, leveltok, "debug"))
level = LOG_DBG;
else if (json_tok_streq(plugin->buffer, leveltok, "warn"))
level = LOG_UNUSUAL;
else if (json_tok_streq(plugin->buffer, leveltok, "error"))
level = LOG_BROKEN;
else {
plugin_kill(plugin,
"Unknown log-level %.*s, valid values are "
"\"debug\", \"info\", \"warn\", or \"error\".",
json_tok_len(leveltok),
json_tok_contents(plugin->buffer, leveltok));
return;
}
log_(plugin->log, level, "%.*s", msgtok->end - msgtok->start,
plugin->buffer + msgtok->start);
}
static void plugin_notification_handle(struct plugin *plugin,
const jsmntok_t *toks)
{

4
tests/test_plugin.py

@ -47,6 +47,10 @@ def test_rpc_passthrough(node_factory):
cmd = [hlp for hlp in n.rpc.help()['help'] if 'hello' in hlp['command']]
assert(len(cmd) == 1)
# While we're at it, let's check that helloworld.py is logging
# correctly via the notifications plugin->lightningd
assert n.daemon.is_in_log('Plugin helloworld.py initialized')
# Now try to call it and see what it returns:
greet = n.rpc.hello(name='Sun')
assert(greet == "Hello Sun")

Loading…
Cancel
Save