core-lightning/common/status_levels.c
Rusty Russell 4ffda340d3 check: make sure all files outside contrib/ include "config.h" first.
And turn "" includes into full-path (which makes it easier to put
config.h first, and finds some cases check-includes.sh missed
previously).

config.h sets _GNU_SOURCE which really needs to be done before any
'#includes': we mainly got away with it with glibc, but other platforms
like Alpine may have stricter requirements.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2021-12-06 10:05:39 +10:30

50 lines
988 B
C

#include "config.h"
#include <ccan/array_size/array_size.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;
}