From a0cd2ac59b891751067512dce77cf6223104932f Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 19 Oct 2021 21:28:30 +0200 Subject: [PATCH] server/audiod: Don't parse loglevel argument unnecessarily. Currently the severity string (debug, info, etc.) given to --loglevel is parsed twice: Once by lopsub, which returns the loglevel as the index into the array of severity strings. We turn this index into a string and pass the string to daemon_set_loglevel() which parses the string again to turn it back into a log level value (which happens to coincide with the index value). Clean this up by letting daemon_set_loglevel() receive a log level value rather than a severity string. This also allows us to remove the now unused ENUM_STRING_VAL() macro from audiod.c. --- audiod.c | 6 ++---- daemon.c | 15 +++++++++------ daemon.h | 2 +- server.c | 4 ++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/audiod.c b/audiod.c index 113f1132..85c14a59 100644 --- a/audiod.c +++ b/audiod.c @@ -44,8 +44,6 @@ static struct lls_parse_result *lpr; #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name))) #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name))) #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name))) -#define ENUM_STRING_VAL(_name) (lls_enum_string_val(OPT_UINT32_VAL(_name), \ - lls_opt(LSG_AUDIOD_PARA_AUDIOD_OPT_ ## _name, CMD_PTR))) __printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log; /** define the array containing all supported audio formats */ @@ -389,7 +387,7 @@ static void parse_config_or_die(void) para_strerror(-ret)); exit(EXIT_FAILURE); } - daemon_set_loglevel(ENUM_STRING_VAL(LOGLEVEL)); + daemon_set_loglevel(OPT_UINT32_VAL(LOGLEVEL)); n = OPT_GIVEN(USER_ALLOW); if (n == 0) return; @@ -1462,7 +1460,7 @@ int main(int argc, char *argv[]) ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx)); if (ret < 0) goto out; - daemon_set_loglevel(ENUM_STRING_VAL(LOGLEVEL)); + daemon_set_loglevel(OPT_UINT32_VAL(LOGLEVEL)); daemon_drop_privileges_or_die(OPT_STRING_VAL(USER), OPT_STRING_VAL(GROUP)); version_handle_flag("audiod", OPT_GIVEN(VERSION)); diff --git a/daemon.c b/daemon.c index dd5420a6..04dbc122 100644 --- a/daemon.c +++ b/daemon.c @@ -134,16 +134,19 @@ void daemon_set_logfile(const char *logfile_name) } /** - * Suppress log messages with severity lower than the given loglevel. + * Control the verbosity for logging. * - * \param loglevel The smallest level that should be logged. + * This instructs the daemon to not log subsequent messages whose severity is + * lower than the given value. + * + * \param loglevel The new log level. */ -void daemon_set_loglevel(const char *loglevel) +void daemon_set_loglevel(int loglevel) { - int ret = get_loglevel_by_name(loglevel); + assert(loglevel >= 0); + assert(loglevel < NUM_LOGLEVELS); + me->loglevel = loglevel; - assert(ret >= 0); - me->loglevel = ret; } /** diff --git a/daemon.h b/daemon.h index b530b0d7..92b6ceae 100644 --- a/daemon.h +++ b/daemon.h @@ -13,7 +13,7 @@ __malloc char *daemon_get_uptime_str(const struct timeval *current_time); void daemon_set_logfile(const char *logfile_name); void daemon_set_hooks(void (*pre_log_hook)(void), void (*post_log_hook)(void)); void daemon_set_flag(unsigned flag); -void daemon_set_loglevel(const char *loglevel); +void daemon_set_loglevel(int loglevel); bool daemon_init_colors_or_die(int color_arg, int color_arg_auto, int color_arg_no, bool logfile_given); void daemon_set_log_color_or_die(const char *arg); diff --git a/server.c b/server.c index e0df714b..b9be84ce 100644 --- a/server.c +++ b/server.c @@ -205,7 +205,7 @@ void parse_config_or_die(bool reload) para_strerror(-ret)); exit(EXIT_FAILURE); } - daemon_set_loglevel(ENUM_STRING_VAL(LOGLEVEL)); + daemon_set_loglevel(OPT_UINT32_VAL(LOGLEVEL)); if (OPT_GIVEN(LOGFILE)) { daemon_set_logfile(OPT_STRING_VAL(LOGFILE)); daemon_open_log_or_die(); @@ -543,7 +543,7 @@ static void server_init(int argc, char **argv, struct server_command_task *sct) if (ret < 0) goto fail; server_lpr = cmdline_lpr; - daemon_set_loglevel(ENUM_STRING_VAL(LOGLEVEL)); + daemon_set_loglevel(OPT_UINT32_VAL(LOGLEVEL)); daemon_drop_privileges_or_die(OPT_STRING_VAL(USER), OPT_STRING_VAL(GROUP)); version_handle_flag("server", OPT_GIVEN(VERSION)); -- 2.39.5