diff --git a/contrib/plugins/helloworld.py b/contrib/plugins/helloworld.py index 7fcbd1514..372d369d7 100755 --- a/contrib/plugins/helloworld.py +++ b/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" diff --git a/lightningd/plugin.c b/lightningd/plugin.c index d7d3f5f70..e41b32af0 100644 --- a/lightningd/plugin.c +++ b/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) { diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 8b85269ad..c8760f15c 100644 --- a/tests/test_plugin.py +++ b/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")