diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 3a4380cf0..d52ba97a8 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -83,16 +83,6 @@ struct command_result *command_param_failed(void) return &complete; } -void NORETURN plugin_err(const char *fmt, ...) -{ - va_list ap; - - /* FIXME: log */ - va_start(ap, fmt); - errx(1, "%s", tal_vfmt(NULL, fmt, ap)); - va_end(ap); -} - /* Realloc helper for tal membufs */ static void *membuf_tal_realloc(struct membuf *mb, void *rawelems, size_t newsize) @@ -625,6 +615,46 @@ struct plugin_timer *plugin_timer(struct plugin_conn *rpc, struct timerel t, return timer; } +static void plugin_logv(enum log_level l, const char *fmt, va_list ap) +{ + char *message; + + printf_json(STDOUT_FILENO, + "{ 'jsonrpc': '2.0', " + "'method': 'log', " + "'params': { 'level': '%s', 'message': \"", + l == LOG_DBG ? "debug" + : l == LOG_INFORM ? "info" + : l == LOG_UNUSUAL ? "warn" + : "error"); + + message = tal_vfmt(NULL, fmt, ap); + write_all(STDOUT_FILENO, message, strlen(message)); + printf_json(STDOUT_FILENO, "\" } }\n\n"); + tal_free(message); +} + +void NORETURN plugin_err(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + plugin_logv(LOG_BROKEN, fmt, ap); + va_end(ap); + va_start(ap, fmt); + errx(1, "%s", tal_vfmt(NULL, fmt, ap)); + va_end(ap); +} + +void plugin_log(enum log_level l, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + plugin_logv(l, fmt, ap); + va_end(ap); +} + void plugin_main(char *argv[], void (*init)(struct plugin_conn *rpc), const struct plugin_command *commands, diff --git a/plugins/libplugin.h b/plugins/libplugin.h index 951307fa0..8364f1244 100644 --- a/plugins/libplugin.h +++ b/plugins/libplugin.h @@ -8,6 +8,7 @@ #include #include #include +#include struct command; struct plugin_conn; @@ -113,6 +114,9 @@ struct plugin_timer *plugin_timer(struct plugin_conn *rpc, struct timerel t, struct command_result *(*cb)(void)); +/* Log something */ +void PRINTF_FMT(2, 3) plugin_log(enum log_level l, const char *fmt, ...); + /* Macro to define arguments */ #define plugin_option(name, description, set, arg) \ (name), \