diff --git a/lightningd/log.c b/lightningd/log.c index a759296a7..8334e6391 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -653,6 +653,22 @@ void json_add_log(struct json_result *response, const char *fieldname, json_array_end(info.response); } +bool json_tok_loglevel(const char *buffer, const jsmntok_t *tok, + enum log_level *level) +{ + if (json_tok_streq(buffer, tok, "io")) + *level = LOG_IO_OUT; + else if (json_tok_streq(buffer, tok, "debug")) + *level = LOG_DBG; + else if (json_tok_streq(buffer, tok, "info")) + *level = LOG_INFORM; + else if (json_tok_streq(buffer, tok, "unusual")) + *level = LOG_UNUSUAL; + else + return false; + return true; +} + static void json_getlog(struct command *cmd, const char *buffer, const jsmntok_t *params) { @@ -667,15 +683,7 @@ static void json_getlog(struct command *cmd, if (!level) minlevel = LOG_INFORM; - else if (json_tok_streq(buffer, level, "io")) - minlevel = LOG_IO_OUT; - else if (json_tok_streq(buffer, level, "debug")) - minlevel = LOG_DBG; - else if (json_tok_streq(buffer, level, "info")) - minlevel = LOG_INFORM; - else if (json_tok_streq(buffer, level, "unusual")) - minlevel = LOG_UNUSUAL; - else { + else if (!json_tok_loglevel(buffer, level, &minlevel)) { command_fail(cmd, "Invalid level param"); return; } diff --git a/lightningd/log.h b/lightningd/log.h index 22ab534ba..3082fc5eb 100644 --- a/lightningd/log.h +++ b/lightningd/log.h @@ -6,6 +6,7 @@ #include #include #include +#include #include struct json_result; @@ -101,4 +102,7 @@ void NORETURN PRINTF_FMT(1,2) fatal(const char *fmt, ...); void json_add_log(struct json_result *result, const char *fieldname, const struct log_book *lr, enum log_level minlevel); +bool json_tok_loglevel(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 068e245f5..bc00103e2 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -944,15 +944,7 @@ static void json_listpeers(struct command *cmd, } if (leveltok) { gpa->ll = tal(gpa, enum log_level); - if (json_tok_streq(buffer, leveltok, "debug")) - *gpa->ll = LOG_DBG; - else if (json_tok_streq(buffer, leveltok, "info")) - *gpa->ll = LOG_INFORM; - else if (json_tok_streq(buffer, leveltok, "unusual")) - *gpa->ll = LOG_UNUSUAL; - else if (json_tok_streq(buffer, leveltok, "broken")) - *gpa->ll = LOG_BROKEN; - else { + if (!json_tok_loglevel(buffer, leveltok, gpa->ll)) { command_fail(cmd, "Invalid level param"); return; }