Browse Source

common: add routines for log level names.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
travis-experimental
Rusty Russell 4 years ago
parent
commit
becd4fe576
  1. 1
      common/Makefile
  2. 49
      common/status_levels.c
  3. 5
      common/status_levels.h
  4. 1
      lightningd/Makefile
  5. 59
      lightningd/log.c
  6. 15
      lightningd/plugin.c
  7. 7
      lightningd/test/run-log-pruning.c

1
common/Makefile

@ -95,6 +95,7 @@ COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) \
common/overflows.h \ common/overflows.h \
common/status_levels.h \ common/status_levels.h \
common/tx_roles.h common/tx_roles.h
COMMON_HEADERS_GEN := common/htlc_state_names_gen.h common/status_wiregen.h common/peer_status_wiregen.h COMMON_HEADERS_GEN := common/htlc_state_names_gen.h common/status_wiregen.h common/peer_status_wiregen.h
COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN) COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN)

49
common/status_levels.c

@ -0,0 +1,49 @@
#include <ccan/array_size/array_size.h>
#include <ccan/build_assert/build_assert.h>
#include <common/status_levels.h>
static const char *ll_names[] = {
"io",
"io",
"debug",
"info",
"unusual",
"broken",
};
const char *log_level_name(enum log_level level)
{
BUILD_ASSERT(ARRAY_SIZE(ll_names) == LOG_LEVEL_MAX+1);
if ((int)level <= LOG_LEVEL_MAX)
return ll_names[level];
return "***unknown***";
}
static bool streq_case(const char *str, const char *s, size_t len)
{
if (len != strlen(str))
return false;
return strncasecmp(str, s, len) == 0;
}
bool log_level_parse(const char *levelstr, size_t len,
enum log_level *level)
{
for (size_t i = 0; i < ARRAY_SIZE(ll_names); i++) {
if (streq_case(ll_names[i], levelstr, len)) {
*level = i;
return true;
}
}
/* We also allow "error" and "warn" */
if (streq_case("error", levelstr, len)) {
*level = LOG_BROKEN;
return true;
}
if (streq_case("warn", levelstr, len)) {
*level = LOG_UNUSUAL;
return true;
}
return false;
}

5
common/status_levels.h

@ -1,6 +1,7 @@
#ifndef LIGHTNING_COMMON_STATUS_LEVELS_H #ifndef LIGHTNING_COMMON_STATUS_LEVELS_H
#define LIGHTNING_COMMON_STATUS_LEVELS_H #define LIGHTNING_COMMON_STATUS_LEVELS_H
#include "config.h" #include "config.h"
#include <ccan/tal/tal.h>
enum log_level { enum log_level {
/* Logging all IO. */ /* Logging all IO. */
@ -17,6 +18,10 @@ enum log_level {
}; };
#define LOG_LEVEL_MAX LOG_BROKEN #define LOG_LEVEL_MAX LOG_BROKEN
const char *log_level_name(enum log_level level);
bool log_level_parse(const char *levelstr, size_t len,
enum log_level *level);
/* /*
* These errors shouldn't happen: * These errors shouldn't happen:
*/ */

1
lightningd/Makefile

@ -83,6 +83,7 @@ LIGHTNINGD_COMMON_OBJS := \
common/features.o \ common/features.o \
common/fee_states.o \ common/fee_states.o \
common/peer_status_wiregen.o \ common/peer_status_wiregen.o \
common/status_levels.o \
common/status_wiregen.o \ common/status_wiregen.o \
common/gossip_rcvd_filter.o \ common/gossip_rcvd_filter.o \
common/hash_u5.o \ common/hash_u5.o \

59
lightningd/log.c

@ -543,57 +543,24 @@ static void log_one_line(unsigned int skipped,
data->prefix = "\n"; data->prefix = "\n";
} }
static const struct level {
const char *name;
enum log_level level;
} log_levels[] = {
{ "IO", LOG_IO_OUT },
{ "DEBUG", LOG_DBG },
{ "INFO", LOG_INFORM },
{ "UNUSUAL", LOG_UNUSUAL },
{ "BROKEN", LOG_BROKEN }
};
static const struct level *str_to_level(const char *str, size_t len)
{
for (size_t i = 0; i < ARRAY_SIZE(log_levels); i++) {
if (strlen(log_levels[i].name) != len)
continue;
if (strncasecmp(str, log_levels[i].name, len) != 0)
continue;
return &log_levels[i];
}
return NULL;
}
static const char *level_to_str(enum log_level level)
{
for (size_t i = 0; i < ARRAY_SIZE(log_levels); i++) {
if (level == log_levels[i].level)
return log_levels[i].name;
}
return NULL;
}
char *opt_log_level(const char *arg, struct log *log) char *opt_log_level(const char *arg, struct log *log)
{ {
const struct level *level; enum log_level level;
int len; int len;
len = strcspn(arg, ":"); len = strcspn(arg, ":");
level = str_to_level(arg, len); if (!log_level_parse(arg, len, &level))
if (!level)
return tal_fmt(NULL, "unknown log level %.*s", len, arg); return tal_fmt(NULL, "unknown log level %.*s", len, arg);
if (arg[len]) { if (arg[len]) {
struct print_filter *f = tal(log->lr, struct print_filter); struct print_filter *f = tal(log->lr, struct print_filter);
f->prefix = arg + len + 1; f->prefix = arg + len + 1;
f->level = level->level; f->level = level;
list_add_tail(&log->lr->print_filters, &f->list); list_add_tail(&log->lr->print_filters, &f->list);
} else { } else {
tal_free(log->lr->default_print_level); tal_free(log->lr->default_print_level);
log->lr->default_print_level = tal(log->lr, enum log_level); log->lr->default_print_level = tal(log->lr, enum log_level);
*log->lr->default_print_level = level->level; *log->lr->default_print_level = level;
} }
return NULL; return NULL;
} }
@ -604,7 +571,7 @@ void json_add_opt_log_levels(struct json_stream *response, struct log *log)
list_for_each(&log->lr->print_filters, i, list) { list_for_each(&log->lr->print_filters, i, list) {
json_add_member(response, "log-level", true, "%s:%s", json_add_member(response, "log-level", true, "%s:%s",
level_to_str(i->level), i->prefix); log_level_name(i->level), i->prefix);
} }
} }
@ -616,7 +583,7 @@ static void show_log_level(char buf[OPT_SHOW_LEN], const struct log *log)
l = *log->lr->default_print_level; l = *log->lr->default_print_level;
else else
l = DEFAULT_LOGLEVEL; l = DEFAULT_LOGLEVEL;
strncpy(buf, level_to_str(l), OPT_SHOW_LEN-1); strncpy(buf, log_level_name(l), OPT_SHOW_LEN-1);
} }
static char *arg_log_prefix(const char *arg, struct log *log) static char *arg_log_prefix(const char *arg, struct log *log)
@ -907,21 +874,13 @@ struct command_result *param_loglevel(struct command *cmd,
enum log_level **level) enum log_level **level)
{ {
*level = tal(cmd, enum log_level); *level = tal(cmd, enum log_level);
if (json_tok_streq(buffer, tok, "io")) if (log_level_parse(buffer + tok->start, tok->end - tok->start, *level))
**level = LOG_IO_OUT; return NULL;
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 command_fail_badparam(cmd, name, buffer, tok, return command_fail_badparam(cmd, name, buffer, tok,
"should be 'io', 'debug', 'info', or " "should be 'io', 'debug', 'info', or "
"'unusual'"); "'unusual'");
} }
return NULL;
}
static struct command_result *json_getlog(struct command *cmd, static struct command_result *json_getlog(struct command *cmd,
const char *buffer, const char *buffer,

15
lightningd/plugin.c

@ -277,15 +277,14 @@ static const char *plugin_log_handle(struct plugin *plugin,
"a string \"message\" field"); "a string \"message\" field");
} }
if (!leveltok || json_tok_streq(plugin->buffer, leveltok, "info")) if (!leveltok)
level = LOG_INFORM; level = LOG_INFORM;
else if (json_tok_streq(plugin->buffer, leveltok, "debug")) else if (!log_level_parse(plugin->buffer + leveltok->start,
level = LOG_DBG; leveltok->end - leveltok->start,
else if (json_tok_streq(plugin->buffer, leveltok, "warn")) &level)
level = LOG_UNUSUAL; /* FIXME: Allow io logging? */
else if (json_tok_streq(plugin->buffer, leveltok, "error")) || level == LOG_IO_IN
level = LOG_BROKEN; || level == LOG_IO_OUT) {
else {
return tal_fmt(plugin, return tal_fmt(plugin,
"Unknown log-level %.*s, valid values are " "Unknown log-level %.*s, valid values are "
"\"debug\", \"info\", \"warn\", or \"error\".", "\"debug\", \"info\", \"warn\", or \"error\".",

7
lightningd/test/run-log-pruning.c

@ -60,6 +60,13 @@ void json_stream_log_suppress_for_cmd(struct json_stream *js UNNEEDED,
/* Generated stub for json_stream_success */ /* Generated stub for json_stream_success */
struct json_stream *json_stream_success(struct command *cmd UNNEEDED) struct json_stream *json_stream_success(struct command *cmd UNNEEDED)
{ fprintf(stderr, "json_stream_success called!\n"); abort(); } { fprintf(stderr, "json_stream_success called!\n"); abort(); }
/* Generated stub for log_level_name */
const char *log_level_name(enum log_level level UNNEEDED)
{ fprintf(stderr, "log_level_name called!\n"); abort(); }
/* Generated stub for log_level_parse */
bool log_level_parse(const char *levelstr UNNEEDED, size_t len UNNEEDED,
enum log_level *level UNNEEDED)
{ fprintf(stderr, "log_level_parse called!\n"); abort(); }
/* Generated stub for node_id_to_hexstr */ /* Generated stub for node_id_to_hexstr */
char *node_id_to_hexstr(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED) char *node_id_to_hexstr(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "node_id_to_hexstr called!\n"); abort(); } { fprintf(stderr, "node_id_to_hexstr called!\n"); abort(); }

Loading…
Cancel
Save