diff --git a/lightningd/log.c b/lightningd/log.c index 8a382fbdd..a9353c153 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -635,39 +635,47 @@ void json_add_log(struct json_result *response, json_array_end(info.response); } -bool json_tok_loglevel(const char *buffer, const jsmntok_t *tok, - enum log_level *level) +bool json_tok_loglevel(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + enum log_level **level) { + *level = tal(cmd, enum log_level); if (json_tok_streq(buffer, tok, "io")) - *level = LOG_IO_OUT; + **level = LOG_IO_OUT; else if (json_tok_streq(buffer, tok, "debug")) - *level = LOG_DBG; + **level = LOG_DBG; else if (json_tok_streq(buffer, tok, "info")) - *level = LOG_INFORM; + **level = LOG_INFORM; else if (json_tok_streq(buffer, tok, "unusual")) - *level = LOG_UNUSUAL; - else + **level = LOG_UNUSUAL; + else { + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be 'io', 'debug', 'info', or " + "'unusual', not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); return false; + } return true; } static void json_getlog(struct command *cmd, - const char *buffer, const jsmntok_t *params) + const char *buffer, const jsmntok_t * params) { struct json_result *response = new_json_result(cmd); - enum log_level minlevel; + enum log_level *minlevel; struct log_book *lr = cmd->ld->log_book; if (!param(cmd, buffer, params, - p_opt_def("level", json_tok_loglevel, &minlevel, LOG_INFORM), + p_opt_def_tal("level", json_tok_loglevel, &minlevel, + LOG_INFORM), NULL)) return; json_object_start(response, NULL); json_add_time(response, "created_at", log_init_time(lr)->ts); - json_add_num(response, "bytes_used", (unsigned int)log_used(lr)); - json_add_num(response, "bytes_max", (unsigned int)log_max_mem(lr)); - json_add_log(response, lr, minlevel); + json_add_num(response, "bytes_used", (unsigned int) log_used(lr)); + json_add_num(response, "bytes_max", (unsigned int) log_max_mem(lr)); + json_add_log(response, lr, *minlevel); json_object_end(response); command_success(cmd, response); } diff --git a/lightningd/log.h b/lightningd/log.h index 1d49fac72..6f77c08d9 100644 --- a/lightningd/log.h +++ b/lightningd/log.h @@ -9,6 +9,7 @@ #include #include +struct command; struct json_result; struct lightningd; struct timerel; @@ -102,7 +103,8 @@ void log_backtrace_exit(void); void json_add_log(struct json_result *result, const struct log_book *lr, enum log_level minlevel); -bool json_tok_loglevel(const char *buffer, const jsmntok_t *tok, - enum log_level *level); +bool json_tok_loglevel(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + enum log_level **level); #endif /* LIGHTNING_LIGHTNINGD_LOG_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index add0e06d3..3aae66713 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -750,7 +750,7 @@ static void json_listpeers(struct command *cmd, if (!param(cmd, buffer, params, p_opt_tal("id", json_tok_pubkey, &specific_id), - p_opt("level", json_tok_loglevel, &ll), + p_opt_tal("level", json_tok_loglevel, &ll), NULL)) return; diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index aef8add53..21da2de62 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -227,8 +227,9 @@ bool json_tok_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEE struct channel_id *cid UNNEEDED) { fprintf(stderr, "json_tok_channel_id called!\n"); abort(); } /* Generated stub for json_tok_loglevel */ -bool json_tok_loglevel(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - enum log_level *level UNNEEDED) +bool json_tok_loglevel(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + enum log_level **level UNNEEDED) { fprintf(stderr, "json_tok_loglevel called!\n"); abort(); } /* Generated stub for json_tok_number */ bool json_tok_number(struct command *cmd UNNEEDED, const char *name UNNEEDED,