libplugin: logging support.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2019-05-21 17:02:37 +09:30 committed by Christian Decker
parent 6a8cd9a016
commit 44196e7d82
2 changed files with 44 additions and 10 deletions

View File

@ -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,

View File

@ -8,6 +8,7 @@
#include <common/json_helpers.h>
#include <common/jsonrpc_errors.h>
#include <common/param.h>
#include <common/status_levels.h>
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), \