From 3ff5d5d690724921c1fc880890b2a49c1d5c979d Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 9 Nov 2014 19:39:11 -0800 Subject: [PATCH] GUAC-911: Separate parsing of level name into own function. --- src/guacd/conf-file.c | 13 +++---------- src/guacd/conf-parse.c | 15 +++++++++++++++ src/guacd/conf-parse.h | 6 ++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/guacd/conf-file.c b/src/guacd/conf-file.c index 15118ef8..102db9cd 100644 --- a/src/guacd/conf-file.c +++ b/src/guacd/conf-file.c @@ -77,23 +77,16 @@ static int guacd_conf_callback(const char* section, const char* param, const cha /* Max log level */ else if (strcmp(param, "log_level") == 0) { - /* Translate level name */ - if (strcmp(value, "info") == 0) - config->max_log_level = GUAC_LOG_INFO; - else if (strcmp(value, "error") == 0) - config->max_log_level = GUAC_LOG_ERROR; - else if (strcmp(value, "warning") == 0) - config->max_log_level = GUAC_LOG_WARNING; - else if (strcmp(value, "debug") == 0) - config->max_log_level = GUAC_LOG_DEBUG; + int level = guacd_parse_log_level(value); /* Invalid log level */ - else { + if (level < 0) { guacd_conf_parse_error = "Invalid log level. Valid levels are: \"debug\", \"info\", \"warning\", and \"error\"."; return 1; } /* Valid log level */ + config->max_log_level = level; return 0; } diff --git a/src/guacd/conf-parse.c b/src/guacd/conf-parse.c index 7576602a..551b342f 100644 --- a/src/guacd/conf-parse.c +++ b/src/guacd/conf-parse.c @@ -24,6 +24,8 @@ #include "conf-parse.h" +#include + #include #include @@ -521,3 +523,16 @@ int guacd_parse_conf(guacd_param_callback* callback, char* buffer, int length, v } +int guacd_parse_log_level(const char* name) { + + /* Translate log level name */ + if (strcmp(name, "info") == 0) return GUAC_LOG_INFO; + if (strcmp(name, "error") == 0) return GUAC_LOG_ERROR; + if (strcmp(name, "warning") == 0) return GUAC_LOG_WARNING; + if (strcmp(name, "debug") == 0) return GUAC_LOG_DEBUG; + + /* No such log level */ + return -1; + +} + diff --git a/src/guacd/conf-parse.h b/src/guacd/conf-parse.h index a81ba1cf..3d9c1df5 100644 --- a/src/guacd/conf-parse.h +++ b/src/guacd/conf-parse.h @@ -50,6 +50,12 @@ typedef int guacd_param_callback(const char* section, const char* param, const c */ int guacd_parse_conf(guacd_param_callback* callback, char* buffer, int length, void* data); +/** + * Parses the given level name, returning the corresponding log level, or -1 if + * no such log level exists. + */ +int guacd_parse_log_level(const char* name); + /** * Human-readable description of the current error, if any. */