From 56ac19d8afd49f200fb2bd954d2f5bcd56c920cf Mon Sep 17 00:00:00 2001 From: thiagoftsm Date: Fri, 3 Apr 2020 10:26:51 +0000 Subject: [PATCH] cove355076: Config per section (#8588) This commit brings the possibility to search an option directly when we already have a section --- libnetdata/config/appconfig.c | 38 ++++++++++++++++++++++++++--------- libnetdata/config/appconfig.h | 2 ++ streaming/rrdpush.c | 2 +- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/libnetdata/config/appconfig.c b/libnetdata/config/appconfig.c index 2fb21f1837..6c008234ad 100644 --- a/libnetdata/config/appconfig.c +++ b/libnetdata/config/appconfig.c @@ -285,16 +285,10 @@ cleanup: return ret; } - -char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value) +char *appconfig_get_by_section(struct section *co, const char *name, const char *default_value) { struct config_option *cv; - debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value); - - struct section *co = appconfig_section_find(root, section); - if(!co) co = appconfig_section_create(root, section); - cv = appconfig_option_index_find(co, name, 0); if(!cv) { cv = appconfig_value_create(co, name, default_value); @@ -314,6 +308,16 @@ char *appconfig_get(struct config *root, const char *section, const char *name, return(cv->value); } +char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value) +{ + debug(D_CONFIG, "request to get config in section '%s', name '%s', default_value '%s'", section, name, default_value); + + struct section *co = appconfig_section_find(root, section); + if(!co) co = appconfig_section_create(root, section); + + return appconfig_get_by_section(co, name, default_value); +} + long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value) { char buffer[100], *s; @@ -336,6 +340,23 @@ LONG_DOUBLE appconfig_get_float(struct config *root, const char *section, const return str2ld(s, NULL); } +static inline int appconfig_test_boolean_value(char *s) { + if(!strcasecmp(s, "yes") || !strcasecmp(s, "true") || !strcasecmp(s, "on") + || !strcasecmp(s, "auto") || !strcasecmp(s, "on demand")) + return 1; + + return 0; +} + +int appconfig_get_boolean_by_section(struct section *co, const char *name, int value) { + char *s; + + s = appconfig_get_by_section(co, name, (!value)?"no":"yes"); + if(!s) return value; + + return appconfig_test_boolean_value(s); +} + int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value) { char *s; @@ -345,8 +366,7 @@ int appconfig_get_boolean(struct config *root, const char *section, const char * s = appconfig_get(root, section, name, s); if(!s) return value; - if(!strcasecmp(s, "yes") || !strcasecmp(s, "true") || !strcasecmp(s, "on") || !strcasecmp(s, "auto") || !strcasecmp(s, "on demand")) return 1; - return 0; + return appconfig_test_boolean_value(s); } int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value) diff --git a/libnetdata/config/appconfig.h b/libnetdata/config/appconfig.h index 0dea5ddf17..a0a3bd6329 100644 --- a/libnetdata/config/appconfig.h +++ b/libnetdata/config/appconfig.h @@ -159,9 +159,11 @@ extern int appconfig_load(struct config *root, char *filename, int overwrite_use extern void config_section_wrlock(struct section *co); extern void config_section_unlock(struct section *co); +extern char *appconfig_get_by_section(struct section *co, const char *name, const char *default_value); extern char *appconfig_get(struct config *root, const char *section, const char *name, const char *default_value); extern long long appconfig_get_number(struct config *root, const char *section, const char *name, long long value); extern LONG_DOUBLE appconfig_get_float(struct config *root, const char *section, const char *name, LONG_DOUBLE value); +extern int appconfig_get_boolean_by_section(struct section *co, const char *name, int value); extern int appconfig_get_boolean(struct config *root, const char *section, const char *name, int value); extern int appconfig_get_boolean_ondemand(struct config *root, const char *section, const char *name, int value); extern int appconfig_get_duration(struct config *root, const char *section, const char *name, const char *value); diff --git a/streaming/rrdpush.c b/streaming/rrdpush.c index d99a2be19d..cd7b3b93e2 100644 --- a/streaming/rrdpush.c +++ b/streaming/rrdpush.c @@ -174,7 +174,7 @@ int configured_as_master() { uuid_t uuid; if (uuid_parse(section->name, uuid) != -1 && - appconfig_get_boolean(&stream_config, section->name, "enabled", 0)) { + appconfig_get_boolean_by_section(section, "enabled", 0)) { is_master = 1; break; }