mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
libplugin: support for sending notifications.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Changelog-Added: libplugin: routines to send notification updates and progress.
This commit is contained in:
@@ -92,6 +92,7 @@ PLUGIN_COMMON_OBJS := \
|
|||||||
common/pseudorand.o \
|
common/pseudorand.o \
|
||||||
common/random_select.o \
|
common/random_select.o \
|
||||||
common/setup.o \
|
common/setup.o \
|
||||||
|
common/status_levels.o \
|
||||||
common/type_to_string.o \
|
common/type_to_string.o \
|
||||||
common/utils.o \
|
common/utils.o \
|
||||||
common/version.o \
|
common/version.o \
|
||||||
|
|||||||
@@ -985,6 +985,65 @@ static void plugin_logv(struct plugin *p, enum log_level l,
|
|||||||
jsonrpc_finish_and_send(p, js);
|
jsonrpc_finish_and_send(p, js);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct json_stream *plugin_notify_start(struct command *cmd, const char *method)
|
||||||
|
{
|
||||||
|
struct json_stream *js = new_json_stream(cmd, NULL, NULL);
|
||||||
|
|
||||||
|
json_object_start(js, NULL);
|
||||||
|
json_add_string(js, "jsonrpc", "2.0");
|
||||||
|
json_add_string(js, "method", method);
|
||||||
|
|
||||||
|
json_object_start(js, "params");
|
||||||
|
json_add_u64(js, "id", *cmd->id);
|
||||||
|
|
||||||
|
return js;
|
||||||
|
}
|
||||||
|
|
||||||
|
void plugin_notify_end(struct command *cmd, struct json_stream *js)
|
||||||
|
{
|
||||||
|
json_object_end(js);
|
||||||
|
|
||||||
|
jsonrpc_finish_and_send(cmd->plugin, js);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Convenience wrapper for notify with "message" */
|
||||||
|
void plugin_notify_message(struct command *cmd,
|
||||||
|
enum log_level level,
|
||||||
|
const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
struct json_stream *js = plugin_notify_start(cmd, "message");
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
json_add_string(js, "level", log_level_name(level));
|
||||||
|
|
||||||
|
/* In case we're OOM */
|
||||||
|
if (js->jout)
|
||||||
|
json_out_addv(js->jout, "message", true, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
plugin_notify_end(cmd, js);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plugin_notify_progress(struct command *cmd,
|
||||||
|
u32 num_stages, u32 stage,
|
||||||
|
u32 num_progress, u32 progress)
|
||||||
|
{
|
||||||
|
struct json_stream *js = plugin_notify_start(cmd, "progress");
|
||||||
|
|
||||||
|
assert(progress < num_progress);
|
||||||
|
json_add_u32(js, "num", progress);
|
||||||
|
json_add_u32(js, "total", num_progress);
|
||||||
|
if (num_stages > 0) {
|
||||||
|
assert(stage < num_stages);
|
||||||
|
json_object_start(js, "stage");
|
||||||
|
json_add_u32(js, "num", stage);
|
||||||
|
json_add_u32(js, "total", num_stages);
|
||||||
|
json_object_end(js);
|
||||||
|
}
|
||||||
|
plugin_notify_end(cmd, js);
|
||||||
|
}
|
||||||
|
|
||||||
void NORETURN plugin_err(struct plugin *p, const char *fmt, ...)
|
void NORETURN plugin_err(struct plugin *p, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|||||||
@@ -232,6 +232,21 @@ struct plugin_timer *plugin_timer_(struct plugin *p,
|
|||||||
/* Log something */
|
/* Log something */
|
||||||
void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...) PRINTF_FMT(3, 4);
|
void plugin_log(struct plugin *p, enum log_level l, const char *fmt, ...) PRINTF_FMT(3, 4);
|
||||||
|
|
||||||
|
/* Notify the caller of something. */
|
||||||
|
struct json_stream *plugin_notify_start(struct command *cmd, const char *method);
|
||||||
|
void plugin_notify_end(struct command *cmd, struct json_stream *js);
|
||||||
|
|
||||||
|
/* Convenience wrapper for notify "message" */
|
||||||
|
void plugin_notify_message(struct command *cmd,
|
||||||
|
enum log_level level,
|
||||||
|
const char *fmt, ...)
|
||||||
|
PRINTF_FMT(3, 4);
|
||||||
|
|
||||||
|
/* Convenience wrapper for progress: num_stages is normally 0. */
|
||||||
|
void plugin_notify_progress(struct command *cmd,
|
||||||
|
u32 num_stages, u32 stage,
|
||||||
|
u32 num_progress, u32 progress);
|
||||||
|
|
||||||
/* Macro to define arguments */
|
/* Macro to define arguments */
|
||||||
#define plugin_option_(name, type, description, set, arg, deprecated) \
|
#define plugin_option_(name, type, description, set, arg, deprecated) \
|
||||||
(name), \
|
(name), \
|
||||||
|
|||||||
Reference in New Issue
Block a user