[sheepdog] [PATCH] sheep: make passing log level uniform for sheep and dog

Liu Yuan namei.unix at gmail.com
Sat Jan 4 18:35:31 CET 2014


With this patch, we all set/get level with string instead of cryptical number

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/common.c       |  9 +--------
 include/compiler.h |  2 ++
 include/logger.h   | 23 +++++++++++++++++++++++
 include/util.h     |  1 -
 sheep/sheep.c      | 29 +++++++++++++++--------------
 5 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/dog/common.c b/dog/common.c
index 828ed1d..0535a44 100644
--- a/dog/common.c
+++ b/dog/common.c
@@ -385,18 +385,11 @@ static const char * const loglevel_table[] = {
 
 int do_loglevel_set(const struct node_id *nid, const char *loglevel_str)
 {
-	int32_t loglevel = -1;
+	int32_t loglevel = loglevel_str2num(loglevel_str);
 	int ret;
 	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 
-	for (int i = 0; i < ARRAY_SIZE(loglevel_table); i++) {
-		if (!strcmp(loglevel_table[i], loglevel_str)) {
-			loglevel = i;
-			break;
-		}
-	}
-
 	if (loglevel == -1)
 		return EXIT_USAGE;
 
diff --git a/include/compiler.h b/include/compiler.h
index 324dacf..4a59b92 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -21,6 +21,8 @@
 
 #include "config.h"
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
 #define __LOCAL(var, line) __ ## var ## line
 #define _LOCAL(var, line) __LOCAL(var, line)
 #define LOCAL(var) _LOCAL(var, __LINE__)
diff --git a/include/logger.h b/include/logger.h
index a86c369..bebe043 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -87,6 +87,29 @@ void sd_backtrace(void);
 	abort();				\
 })
 
+
+static inline int loglevel_str2num(const char *str)
+{
+	static const char * const loglevel_table[] = {
+		"emerg",
+		"alert",
+		"crit",
+		"err",
+		"warning",
+		"notice",
+		"info",
+		"debug",
+	};
+	int i, max = ARRAY_SIZE(loglevel_table);
+
+	for (i = 0; i < max; i++) {
+		if (!strcmp(loglevel_table[i], str))
+			break;
+	}
+
+	return i == max ? -1 : i;
+}
+
 void set_loglevel(int new_loglevel);
 int get_loglevel(void);
 
diff --git a/include/util.h b/include/util.h
index 3df2eec..b7e0e8b 100644
--- a/include/util.h
+++ b/include/util.h
@@ -20,7 +20,6 @@
 #define SECTOR_SIZE (1U << 9)
 #define BLOCK_SIZE (1U << 12)
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 #define round_up(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
 #define round_down(x, y) (((x) / (y)) * (y))
 
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 953739c..c1c3349 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -85,7 +85,7 @@ static const char cache_help[] =
 "cache in directio mode\n";
 
 static const char log_help[] =
-"Example:\n\t$ sheep -l dir=/var/log/,level=0,format=server ...\n"
+"Example:\n\t$ sheep -l dir=/var/log/,level=debug,format=server ...\n"
 "Available arguments:\n"
 "\tdir=: path to the location of sheep.log\n"
 "\tlevel=: log level of sheep.log\n"
@@ -93,15 +93,15 @@ static const char log_help[] =
 "if dir is not specified, use metastore directory\n\n"
 "Available log levels:\n"
 "  #    Level           Description\n"
-"  0    SDOG_EMERG      system has failed and is unusable\n"
-"  1    SDOG_ALERT      action must be taken immediately\n"
-"  2    SDOG_CRIT       critical conditions\n"
-"  3    SDOG_ERR        error conditions\n"
-"  4    SDOG_WARNING    warning conditions\n"
-"  5    SDOG_NOTICE     normal but significant conditions\n"
-"  6    SDOG_INFO       informational notices\n"
-"  7    SDOG_DEBUG      debugging messages\n"
-"default log level is SDOG_INFO\n\n"
+"  0    emerg      system has failed and is unusable\n"
+"  1    alert      action must be taken immediately\n"
+"  2    crit       critical conditions\n"
+"  3    err        error conditions\n"
+"  4    warning    warning conditions\n"
+"  5    notice     normal but significant conditions\n"
+"  6    info       informational notices\n"
+"  7    debug      debugging messages\n"
+"default log level is info\n\n"
 "Available log format:\n"
 "  FormatType      Description\n"
 "  default         raw format\n"
@@ -316,14 +316,15 @@ static int log_level = SDOG_INFO;
 
 static int log_level_parser(const char *s)
 {
-	char *p;
-	log_level = strtol(s, &p, 10);
-	if (s == p || log_level < SDOG_EMERG ||
-		SDOG_DEBUG < log_level || *p != '\0') {
+	int level = loglevel_str2num(s);
+
+	if (level < 0) {
 		sd_err("Invalid log level '%s'", s);
 		sdlog_help();
 		return -1;
 	}
+
+	log_level = level;
 	return 0;
 }
 
-- 
1.8.1.2




More information about the sheepdog mailing list