Allowed values are:
 - DEBUG, INFO, WARNING, ERROR (case insensitive)
 - integer numbers

Signed-off-by: Peter Schiffer <pschi...@redhat.com>
---
 include/libcgroup/log.h |    7 +++++++
 src/libcgroup.map       |    1 +
 src/log.c               |   30 +++++++++++++++++++++++++-----
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/include/libcgroup/log.h b/include/libcgroup/log.h
index 356eefb..0dd03b4 100644
--- a/include/libcgroup/log.h
+++ b/include/libcgroup/log.h
@@ -131,6 +131,13 @@ extern void cgroup_set_loglevel(int loglevel);
  * This logging automatically benefits from CGROUP_LOGLEVEL env. variable.
  */
 extern void cgroup_log(int loglevel, const char *fmt, ...);
+
+/**
+ * Parse levelstr string for information about desired loglevel. The levelstr
+ * is usually a value of the CGROUP_LOGLEVEL environment variable.
+ * @param levelstr String containing desired loglevel.
+ */
+extern int cgroup_parse_log_level_str(const char *levelstr);
 /**
  * @}
  * @}
diff --git a/src/libcgroup.map b/src/libcgroup.map
index df651b5..b0c162c 100644
--- a/src/libcgroup.map
+++ b/src/libcgroup.map
@@ -115,4 +115,5 @@ CGROUP_0.39 {
        cgroup_set_default_logger;
        cgroup_set_loglevel;
        cgroup_log;
+       cgroup_parse_log_level_str;
 } CGROUP_0.38;
diff --git a/src/log.c b/src/log.c
index d0a6854..f6a386d 100644
--- a/src/log.c
+++ b/src/log.c
@@ -18,6 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <strings.h>
 
 static cgroup_logger_callback cgroup_logger;
 static void *cgroup_logger_userdata;
@@ -58,6 +59,29 @@ void cgroup_set_default_logger(int level)
                cgroup_set_logger(cgroup_default_logger, level, NULL);
 }
 
+int cgroup_parse_log_level_str(const char *levelstr)
+{
+       char *end;
+       long level;
+       errno = 0;
+
+       /* try to parse integer first */
+       level = strtol(levelstr, &end, 10);
+       if (end != levelstr && *end == '\0')
+               return level;
+
+       if (strcasecmp(levelstr, "ERROR") == 0)
+               return CGROUP_LOG_ERROR;
+       if (strcasecmp(levelstr, "WARNING") == 0)
+               return CGROUP_LOG_WARNING;
+       if (strcasecmp(levelstr, "INFO") == 0)
+               return CGROUP_LOG_INFO;
+       if (strcasecmp(levelstr, "DEBUG") == 0)
+               return CGROUP_LOG_DEBUG;
+
+       return CGROUP_DEFAULT_LOGLEVEL;
+}
+
 void cgroup_set_loglevel(int loglevel)
 {
        if (loglevel != -1)
@@ -65,11 +89,7 @@ void cgroup_set_loglevel(int loglevel)
        else {
                char *level_str = getenv("CGROUP_LOGLEVEL");
                if (level_str != NULL)
-                       /*
-                        * TODO: add better loglevel detection, e.g. strings
-                        * instead of plain numbers.
-                        */
-                       cgroup_loglevel = atoi(level_str);
+                       cgroup_loglevel = cgroup_parse_log_level_str(level_str);
                else
                        cgroup_loglevel = CGROUP_DEFAULT_LOGLEVEL;
        }


------------------------------------------------------------------------------
Symantec Endpoint Protection 12 positioned as A LEADER in The Forrester  
Wave(TM): Endpoint Security, Q1 2013 and "remains a good choice" in the  
endpoint security space. For insight on selecting the right partner to 
tackle endpoint security challenges, access the full report. 
http://p.sf.net/sfu/symantec-dev2dev
_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to