From 58ba5f61771636a39077478e290a130457f1a4df Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 19 Oct 2021 21:44:14 +0200 Subject: [PATCH] daemon: Kill get_loglevel_by_name(). Open-code the logic in daemon_set_log_color_or_die() and get the values from the new SEVERTIES macro rather than duplicating the severity list in get_loglevel_by_name(). The SEVERTIES macro will turn out to be handy for the ll subcommands of para_server and para_audiod which are introduced in subsequent commits. --- Makefile.real | 2 ++ daemon.c | 22 ++++++++++++++-------- error.h | 1 - string.c | 31 ------------------------------- string.h | 1 - 5 files changed, 16 insertions(+), 41 deletions(-) diff --git a/Makefile.real b/Makefile.real index 6e8084d5..92ef9952 100644 --- a/Makefile.real +++ b/Makefile.real @@ -10,6 +10,7 @@ endif .SHELLFLAGS := -ec LOGLEVELS := LL_DEBUG,LL_INFO,LL_NOTICE,LL_WARNING,LL_ERROR,LL_CRIT,LL_EMERG +SEVERITIES := \"debug\",\"info\",\"notice\",\"warning\",\"error\",\"crit\",\"emerg\" vardir := /var/paraslash mandir := $(datarootdir)/man/man1 MKDIR_P := mkdir -p @@ -112,6 +113,7 @@ CPPFLAGS += -DBINDIR='"$(bindir)"' CPPFLAGS += -DCOPYRIGHT_YEAR='"$(COPYRIGHT_YEAR)"' CPPFLAGS += -DBUILD_DATE='"$(build_date)"' CPPFLAGS += -DLOGLEVELS='$(LOGLEVELS)' +CPPFLAGS += -DSEVERITIES=$(SEVERITIES) CPPFLAGS += -DUNAME_RS='"$(uname_rs)"' CPPFLAGS += -DCC_VERSION='"$(cc_version)"' CPPFLAGS += -I$(lls_suite_dir) diff --git a/daemon.c b/daemon.c index 04dbc122..f44848ac 100644 --- a/daemon.c +++ b/daemon.c @@ -64,18 +64,24 @@ static void daemon_set_default_log_colors(void) */ void daemon_set_log_color_or_die(const char *arg) { + unsigned ll; + const char * const sev[] = {SEVERITIES}; char *p = strchr(arg, ':'); - int ret, ll; if (!p) goto err; - ret = get_loglevel_by_name(arg); - if (ret < 0) - goto err; - ll = ret; - p++; - color_parse_or_die(p, me->log_colors[ll]); - return; + for (ll = 0; ll < NUM_LOGLEVELS; ll++) { + const char *name = sev[ll]; + /* + * Parse only the first part of the string so that, for + * example, the argument "info:something_else" is recognized. + * Note that the string comparison is performed + * case-insensitively. + */ + if (strncasecmp(arg, name, strlen(name))) + continue; + return color_parse_or_die(p + 1, me->log_colors[ll]); + } err: PARA_EMERG_LOG("%s: invalid color argument\n", arg); exit(EXIT_FAILURE); diff --git a/error.h b/error.h index 60c23294..468910d0 100644 --- a/error.h +++ b/error.h @@ -51,7 +51,6 @@ PARA_ERROR(BAD_CT, "invalid chunk table or bad FEC configuration"), \ PARA_ERROR(BAD_FEATURE, "invalid feature request"), \ PARA_ERROR(BAD_FEC_HEADER, "invalid fec header"), \ - PARA_ERROR(BAD_LL, "invalid loglevel"), \ PARA_ERROR(BAD_PATH, "invalid path"), \ PARA_ERROR(BAD_PRIVATE_KEY, "invalid private key"), \ PARA_ERROR(BAD_SAMPLE_FORMAT, "sample format not supported"), \ diff --git a/string.c b/string.c index 198e9f1d..2c69c40a 100644 --- a/string.c +++ b/string.c @@ -602,37 +602,6 @@ int para_atoi32(const char *str, int32_t *value) return 1; } -static inline int loglevel_equal(const char *arg, const char * const ll) -{ - return !strncasecmp(arg, ll, strlen(ll)); -} - -/** - * Compute the loglevel number from its name. - * - * \param txt The name of the loglevel (debug, info, ...). - * - * \return The numeric representation of the loglevel name. - */ -int get_loglevel_by_name(const char *txt) -{ - if (loglevel_equal(txt, "debug")) - return LL_DEBUG; - if (loglevel_equal(txt, "info")) - return LL_INFO; - if (loglevel_equal(txt, "notice")) - return LL_NOTICE; - if (loglevel_equal(txt, "warning")) - return LL_WARNING; - if (loglevel_equal(txt, "error")) - return LL_ERROR; - if (loglevel_equal(txt, "crit")) - return LL_CRIT; - if (loglevel_equal(txt, "emerg")) - return LL_EMERG; - return -E_BAD_LL; -} - static int get_next_word(const char *buf, const char *delim, char **word) { enum line_state_flags {LSF_HAVE_WORD = 1, LSF_BACKSLASH = 2, diff --git a/string.h b/string.h index 10379a0e..79ccb67e 100644 --- a/string.h +++ b/string.h @@ -84,7 +84,6 @@ __malloc char *para_hostname(void); __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...); int para_atoi64(const char *str, int64_t *result); int para_atoi32(const char *str, int32_t *value); -int get_loglevel_by_name(const char *txt); int read_size_header(const char *buf); int create_argv(const char *buf, const char *delim, char ***result); int create_shifted_argv(const char *buf, const char *delim, char ***result); -- 2.39.2