From 4856ca5db0112337a2ce09232c0f7c4d09daf055 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 5 Feb 2018 14:39:28 +1030 Subject: [PATCH] json_log: move command to log.c, share code with listpeers log. Signed-off-by: Rusty Russell --- lightningd/jsonrpc.c | 117 ---------------------------------- lightningd/log.c | 130 ++++++++++++++++++++++++++++++++++++++ lightningd/log.h | 6 +- lightningd/peer_control.c | 30 +-------- 4 files changed, 137 insertions(+), 146 deletions(-) diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 6cd62c33b..0ff2f8623 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -81,123 +81,6 @@ static const struct json_command stop_command = { }; AUTODATA(json_command, &stop_command); -struct log_info { - enum log_level level; - struct json_result *response; - unsigned int num_skipped; -}; - -static void add_skipped(struct log_info *info) -{ - if (info->num_skipped) { - json_object_start(info->response, NULL); - json_add_string(info->response, "type", "SKIPPED"); - json_add_num(info->response, "num_skipped", info->num_skipped); - json_object_end(info->response); - info->num_skipped = 0; - } -} - -static void json_add_time(struct json_result *result, const char *fieldname, - struct timespec ts) -{ - char timebuf[100]; - - sprintf(timebuf, "%lu.%09u", - (unsigned long)ts.tv_sec, - (unsigned)ts.tv_nsec); - json_add_string(result, fieldname, timebuf); -} - -static void log_to_json(unsigned int skipped, - struct timerel diff, - enum log_level level, - const char *prefix, - const char *log, - struct log_info *info) -{ - info->num_skipped += skipped; - - if (level < info->level) { - info->num_skipped++; - return; - } - - add_skipped(info); - - json_object_start(info->response, NULL); - json_add_string(info->response, "type", - level == LOG_BROKEN ? "BROKEN" - : level == LOG_UNUSUAL ? "UNUSUAL" - : level == LOG_INFORM ? "INFO" - : level == LOG_DBG ? "DEBUG" - : level == LOG_IO ? "IO" - : "UNKNOWN"); - json_add_time(info->response, "time", diff.ts); - json_add_string(info->response, "source", prefix); - if (level == LOG_IO) { - assert(tal_count(log) > 0); - if (log[0]) - json_add_string(info->response, "direction", "IN"); - else - json_add_string(info->response, "direction", "OUT"); - - json_add_hex(info->response, "data", log+1, tal_count(log)-1); - } else - json_add_string(info->response, "log", log); - - json_object_end(info->response); -} - -static void json_getlog(struct command *cmd, - const char *buffer, const jsmntok_t *params) -{ - struct log_info info; - struct log_book *lr = cmd->ld->log_book; - jsmntok_t *level; - - if (!json_get_params(cmd, buffer, params, "?level", &level, NULL)) { - return; - } - - info.num_skipped = 0; - - if (!level) - info.level = LOG_INFORM; - else if (json_tok_streq(buffer, level, "io")) - info.level = LOG_IO; - else if (json_tok_streq(buffer, level, "debug")) - info.level = LOG_DBG; - else if (json_tok_streq(buffer, level, "info")) - info.level = LOG_INFORM; - else if (json_tok_streq(buffer, level, "unusual")) - info.level = LOG_UNUSUAL; - else { - command_fail(cmd, "Invalid level param"); - return; - } - - info.response = new_json_result(cmd); - json_object_start(info.response, NULL); - if (deprecated_apis) - json_add_time(info.response, "creation_time", log_init_time(lr)->ts); - json_add_time(info.response, "created_at", log_init_time(lr)->ts); - json_add_num(info.response, "bytes_used", (unsigned int)log_used(lr)); - json_add_num(info.response, "bytes_max", (unsigned int)log_max_mem(lr)); - json_array_start(info.response, "log"); - log_each_line(lr, log_to_json, &info); - json_array_end(info.response); - json_object_end(info.response); - command_success(cmd, info.response); -} - -static const struct json_command getlog_command = { - "getlog", - json_getlog, - "Show logs, with optional log {level} (info|unusual|debug|io)" -}; -AUTODATA(json_command, &getlog_command); - #if DEVELOPER static void json_rhash(struct command *cmd, const char *buffer, const jsmntok_t *params) diff --git a/lightningd/log.c b/lightningd/log.c index b15a163d5..17942609d 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -13,7 +13,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -563,3 +565,131 @@ void fatal(const char *fmt, ...) } abort(); } + +struct log_info { + enum log_level level; + struct json_result *response; + unsigned int num_skipped; +}; + +static void add_skipped(struct log_info *info) +{ + if (info->num_skipped) { + json_object_start(info->response, NULL); + json_add_string(info->response, "type", "SKIPPED"); + json_add_num(info->response, "num_skipped", info->num_skipped); + json_object_end(info->response); + info->num_skipped = 0; + } +} + +static void json_add_time(struct json_result *result, const char *fieldname, + struct timespec ts) +{ + char timebuf[100]; + + sprintf(timebuf, "%lu.%09u", + (unsigned long)ts.tv_sec, + (unsigned)ts.tv_nsec); + json_add_string(result, fieldname, timebuf); +} + +static void log_to_json(unsigned int skipped, + struct timerel diff, + enum log_level level, + const char *prefix, + const char *log, + struct log_info *info) +{ + info->num_skipped += skipped; + + if (level < info->level) { + info->num_skipped++; + return; + } + + add_skipped(info); + + json_object_start(info->response, NULL); + json_add_string(info->response, "type", + level == LOG_BROKEN ? "BROKEN" + : level == LOG_UNUSUAL ? "UNUSUAL" + : level == LOG_INFORM ? "INFO" + : level == LOG_DBG ? "DEBUG" + : level == LOG_IO ? "IO" + : "UNKNOWN"); + json_add_time(info->response, "time", diff.ts); + json_add_string(info->response, "source", prefix); + if (level == LOG_IO) { + assert(tal_count(log) > 0); + if (log[0]) + json_add_string(info->response, "direction", "IN"); + else + json_add_string(info->response, "direction", "OUT"); + + json_add_hex(info->response, "data", log+1, tal_count(log)-1); + } else + json_add_string(info->response, "log", log); + + json_object_end(info->response); +} + +void json_add_log(struct json_result *response, const char *fieldname, + const struct log_book *lr, enum log_level minlevel) +{ + struct log_info info; + + info.level = minlevel; + info.response = response; + info.num_skipped = 0; + + json_array_start(info.response, "log"); + log_each_line(lr, log_to_json, &info); + add_skipped(&info); + json_array_end(info.response); +} + +static void json_getlog(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct json_result *response = new_json_result(cmd); + enum log_level minlevel; + struct log_book *lr = cmd->ld->log_book; + jsmntok_t *level; + + if (!json_get_params(cmd, buffer, params, "?level", &level, NULL)) { + return; + } + + if (!level) + minlevel = LOG_INFORM; + else if (json_tok_streq(buffer, level, "io")) + minlevel = LOG_IO; + 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 { + command_fail(cmd, "Invalid level param"); + return; + } + + json_object_start(response, NULL); + if (deprecated_apis) + json_add_time(response, "creation_time", log_init_time(lr)->ts); + 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, "log", lr, minlevel); + json_object_end(response); + command_success(cmd, response); +} + +static const struct json_command getlog_command = { + "getlog", + json_getlog, + "Show logs, with optional log {level} (info|unusual|debug|io)" +}; +AUTODATA(json_command, &getlog_command); diff --git a/lightningd/log.h b/lightningd/log.h index 9fe3de278..39cc10bc4 100644 --- a/lightningd/log.h +++ b/lightningd/log.h @@ -8,9 +8,9 @@ #include #include +struct json_result; struct lightningd; struct timerel; -struct lightningd; /* We can have a single log book, with multiple logs in it. */ struct log_book *new_log_book(const tal_t *ctx, @@ -90,4 +90,8 @@ const tal_t *ltmp; /* Before the crashlog is activated, just prints to stderr. */ void NORETURN PRINTF_FMT(1,2) fatal(const char *fmt, ...); +/* Adds an array showing log entries */ +void json_add_log(struct json_result *result, const char *fieldname, + const struct log_book *lr, enum log_level minlevel); + #endif /* LIGHTNING_LIGHTNINGD_LOG_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index a84d34dc6..6ca037ba1 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -805,26 +805,6 @@ static const struct json_command connect_command = { }; AUTODATA(json_command, &connect_command); -struct log_info { - enum log_level level; - struct json_result *response; -}; - -/* FIXME: Share this with jsonrpc.c's code! */ -static void log_to_json(unsigned int skipped, - struct timerel diff, - enum log_level level, - const char *prefix, - const char *log, - struct log_info *info) -{ - if (level < info->level) - return; - - if (level != LOG_IO) - json_add_string(info->response, NULL, log); -} - struct getpeers_args { struct command *cmd; /* If non-NULL, they want logs too */ @@ -903,14 +883,8 @@ static void gossipd_getpeers_complete(struct subd *gossip, const u8 *msg, json_object_end(response); json_array_end(response); - if (gpa->ll) { - struct log_info info; - info.level = *gpa->ll; - info.response = response; - json_array_start(response, "log"); - log_each_line(p->log_book, log_to_json, &info); - json_array_end(response); - } + if (gpa->ll) + json_add_log(response, "log", p->log_book, *gpa->ll); json_object_end(response); }