mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
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>
This commit is contained in:
committed by
Rusty Russell
parent
dc25c43945
commit
a304db9be2
@@ -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):
|
def json_init(request, options, configuration):
|
||||||
"""The main daemon is telling us the relevant cli options
|
"""The main daemon is telling us the relevant cli options
|
||||||
"""
|
"""
|
||||||
global greeting
|
global greeting
|
||||||
|
|
||||||
greeting = request['params']['options']['greeting']
|
greeting = request['params']['options']['greeting']
|
||||||
|
plugin_log("Plugin helloworld.py initialized with greeting \"{}\"".format(greeting), "debug")
|
||||||
|
|
||||||
return "ok"
|
return "ok"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -234,6 +234,40 @@ static void plugin_request_queue(struct plugin *plugin,
|
|||||||
io_wake(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,
|
static void plugin_notification_handle(struct plugin *plugin,
|
||||||
const jsmntok_t *toks)
|
const jsmntok_t *toks)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ def test_rpc_passthrough(node_factory):
|
|||||||
cmd = [hlp for hlp in n.rpc.help()['help'] if 'hello' in hlp['command']]
|
cmd = [hlp for hlp in n.rpc.help()['help'] if 'hello' in hlp['command']]
|
||||||
assert(len(cmd) == 1)
|
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:
|
# Now try to call it and see what it returns:
|
||||||
greet = n.rpc.hello(name='Sun')
|
greet = n.rpc.hello(name='Sun')
|
||||||
assert(greet == "Hello Sun")
|
assert(greet == "Hello Sun")
|
||||||
|
|||||||
Reference in New Issue
Block a user