Skip to content

Commit

Permalink
lightningd: infrastructure for internal notifications.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Oct 12, 2020
1 parent ee55c8c commit 3a3f015
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,33 @@ static void json_command_malformed(struct json_connection *jcon,
json_stream_close(js, NULL);
}

void json_notify_fmt(struct command *cmd,
enum log_level level,
const char *fmt, ...)
{
va_list ap;
struct json_stream *js;

if (!cmd->send_notifications)
return;

js = json_stream_raw_for_cmd(cmd);

va_start(ap, fmt);
json_object_start(js, NULL);
json_add_string(js, "jsonrpc", "2.0");
json_add_string(js, "method", "message");
json_object_start(js, "params");
json_add_string(js, "id", cmd->id);
json_add_string(js, "level", log_level_name(level));
json_add_string(js, "message", tal_vfmt(tmpctx, fmt, ap));
json_object_end(js);
json_object_end(js);

json_stream_double_cr(js);
json_stream_flush(js);
}

struct json_stream *json_stream_raw_for_cmd(struct command *cmd)
{
struct json_stream *js;
Expand Down
7 changes: 7 additions & 0 deletions lightningd/jsonrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <common/errcode.h>
#include <common/json.h>
#include <common/json_stream.h>
#include <common/status_levels.h>
#include <stdarg.h>

struct jsonrpc;
Expand Down Expand Up @@ -154,6 +155,12 @@ static inline void fixme_ignore(const struct command_result *res)
{
}

/* Notifier to the caller. */
void json_notify_fmt(struct command *cmd,
enum log_level level,
const char *fmt, ...)
PRINTF_FMT(3, 4);

/* FIXME: For the few cases where return value is indeterminate */
struct command_result *command_its_complicated(const char *why);

Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ void log_io(struct log *log UNNEEDED, enum log_level dir UNNEEDED,
const char *comment UNNEEDED,
const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "log_io called!\n"); abort(); }
/* Generated stub for log_level_name */
const char *log_level_name(enum log_level level UNNEEDED)
{ fprintf(stderr, "log_level_name called!\n"); abort(); }
/* Generated stub for memleak_remove_strmap_ */
void memleak_remove_strmap_(struct htable *memtable UNNEEDED, const struct strmap *m UNNEEDED)
{ fprintf(stderr, "memleak_remove_strmap_ called!\n"); abort(); }
Expand Down

0 comments on commit 3a3f015

Please sign in to comment.