core: Initial implementation for run-time selection of log level

Log level can be controlled using NFD_LOG environment variable. The
following options are available

Set up log level using mnemonic

    NFD_LOG=none|error|warn|info|debug|trace

Set up log level using log-level number (0 none, >=5 all)

    NFD_LOG=0|1|2|3|4|5|6|...

Change-Id: Id38c2775f1137a4e38e4896963ab9a01fc863d2c
Refs: #1267
diff --git a/daemon/core/logger.cpp b/daemon/core/logger.cpp
index 14dd380..928b112 100644
--- a/daemon/core/logger.cpp
+++ b/daemon/core/logger.cpp
@@ -7,14 +7,69 @@
  */
 
 #include "logger.hpp"
+#include <boost/algorithm/string.hpp>
 
-namespace nfd
+namespace nfd {
+
+inline static bool
+isNumber(const std::string& s)
 {
+  if (s.empty())
+    return false;
+
+  for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
+    {
+      if (!std::isdigit(*i))
+        return false;
+    }
+
+  return true;
+}
 
 Logger::Logger(const std::string& name)
-  : m_moduleName(name),
-    m_isEnabled(true)
+  : m_moduleName(name)
 {
+  char* nfdLog = getenv("NFD_LOG");
+  if (nfdLog)
+    {
+      std::string level = nfdLog;
+      if (isNumber(nfdLog))
+        {
+          setLogLevel(boost::lexical_cast<uint32_t>(nfdLog));
+        }
+      else if (boost::iequals(level, "none"))
+        {
+          m_enabledLogLevel = LOG_NONE;
+        }
+      else if (boost::iequals(level, "error"))
+        {
+          m_enabledLogLevel = LOG_ERROR;
+        }
+      else if (boost::iequals(level, "warn"))
+        {
+          m_enabledLogLevel = LOG_WARN;
+        }
+      else if (boost::iequals(level, "info"))
+        {
+          m_enabledLogLevel = LOG_INFO;
+        }
+      else if (boost::iequals(level, "debug"))
+        {
+          m_enabledLogLevel = LOG_DEBUG;
+        }
+      else if (boost::iequals(level, "trace"))
+        {
+          m_enabledLogLevel = LOG_TRACE;
+        }
+      else
+        {
+          m_enabledLogLevel = LOG_ALL;
+        }
+    }
+  else
+    {
+      m_enabledLogLevel = LOG_WARN;
+    }
 }
 
 std::ostream&
diff --git a/daemon/core/logger.hpp b/daemon/core/logger.hpp
index f8c6e8e..aad2394 100644
--- a/daemon/core/logger.hpp
+++ b/daemon/core/logger.hpp
@@ -18,22 +18,29 @@
   LOG_NONE           = 0, // no messages
   LOG_ERROR          = 1, // serious error messages
   LOG_WARN           = 2, // warning messages
-  LOG_TRACE          = 3, // trace messages
+  LOG_INFO           = 3, // informational messages
   LOG_DEBUG          = 4, // debug messages
-  LOG_INFO           = 5, // informational messages
-  LOG_FATAL          = 6, // fatal error messages
-  LOG_ALL            = 0x0fffffff, // all messages
+  LOG_TRACE          = 5, // trace messages (most verbose)
+  // LOG_FATAL is not a level and is logged unconditionally
+  LOG_ALL            = 255, // all messages
 };
 
 class Logger
 {
 public:
+  explicit
   Logger(const std::string& name);
   
   bool
   isEnabled(LogLevel level)
   {
-    return m_isEnabled;
+    return (m_enabledLogLevel >= level);
+  }
+
+  void
+  setLogLevel(uint32_t level)
+  {
+    m_enabledLogLevel = static_cast<LogLevel>(level);
   }
   
   const std::string&
@@ -44,7 +51,7 @@
   
 private:
   std::string m_moduleName;
-  bool m_isEnabled;
+  uint32_t    m_enabledLogLevel;
 };
 
 std::ostream&
@@ -93,8 +100,7 @@
        std::cerr<<"ERROR: "<<"["<<g_logger<<"] " << expression <<"\n"
   
 #define NFD_LOG_FATAL(expression)\
-    if(g_logger.isEnabled(nfd::LOG_FATAL)) \
-       std::cerr<<"FATAL: "<<"["<<g_logger<<"] " << expression <<"\n"
+    std::cerr<<"FATAL: "<<"["<<g_logger<<"] " << expression <<"\n"
   
 } //namespace nfd
 
diff --git a/tests/core/logger.cpp b/tests/core/logger.cpp
index ca55c37..6694ece 100644
--- a/tests/core/logger.cpp
+++ b/tests/core/logger.cpp
@@ -27,7 +27,7 @@
   {
     std::cerr.rdbuf(m_savedBuf);
   }
-  
+
   std::stringstream m_buffer;
   std::streambuf* m_savedBuf;
 };
@@ -35,14 +35,15 @@
 BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
 {
   NFD_LOG_INIT("BasicTests");
-  
+  g_logger.setLogLevel(LOG_ALL);
+
   NFD_LOG_TRACE("trace message JHGFDSR^1");
   NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
   NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
-  NFD_LOG_INFO("info message Jjxjshj13"); 
+  NFD_LOG_INFO("info message Jjxjshj13");
   NFD_LOG_ERROR("error message !#$&^%$#@");
   NFD_LOG_FATAL("fatal message JJSjaamcng");
-  
+
   BOOST_CHECK_EQUAL(m_buffer.str(),
                     "TRACE: [BasicTests] trace message JHGFDSR^1\n"
                     "DEBUG: [BasicTests] debug message IGg2474fdksd fo 151617\n"
@@ -56,17 +57,22 @@
 class InClassLogger : public LoggerFixture
 {
 public:
+  InClassLogger()
+  {
+    g_logger.setLogLevel(LOG_ALL);
+  }
+
   void
   writeLogs()
   {
     NFD_LOG_TRACE("trace message JHGFDSR^1");
     NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
     NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
-    NFD_LOG_INFO("info message Jjxjshj13"); 
+    NFD_LOG_INFO("info message Jjxjshj13");
     NFD_LOG_ERROR("error message !#$&^%$#@");
     NFD_LOG_FATAL("fatal message JJSjaamcng");
   }
-    
+
 private:
   NFD_LOG_INCLASS_DECLARE();
 };
@@ -76,7 +82,7 @@
 BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
 {
   writeLogs();
-  
+
   BOOST_CHECK_EQUAL(m_buffer.str(),
                     "TRACE: [InClassLogger] trace message JHGFDSR^1\n"
                     "DEBUG: [InClassLogger] debug message IGg2474fdksd fo 151617\n"
@@ -92,17 +98,22 @@
 class InClassTemplateLogger : public LoggerFixture
 {
 public:
+  InClassTemplateLogger()
+  {
+    g_logger.setLogLevel(LOG_ALL);
+  }
+
   void
   writeLogs()
   {
     NFD_LOG_TRACE("trace message JHGFDSR^1");
     NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
     NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
-    NFD_LOG_INFO("info message Jjxjshj13"); 
+    NFD_LOG_INFO("info message Jjxjshj13");
     NFD_LOG_ERROR("error message !#$&^%$#@");
     NFD_LOG_FATAL("fatal message JJSjaamcng");
   }
-    
+
 private:
   NFD_LOG_INCLASS_DECLARE();
 };
@@ -113,7 +124,7 @@
 BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
 {
   writeLogs();
-  
+
   BOOST_CHECK_EQUAL(m_buffer.str(),
                     "TRACE: [GenericInClassTemplateLogger] trace message JHGFDSR^1\n"
                     "DEBUG: [GenericInClassTemplateLogger] debug message IGg2474fdksd fo 151617\n"
@@ -127,7 +138,7 @@
 BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
 {
   writeLogs();
-  
+
   BOOST_CHECK_EQUAL(m_buffer.str(),
                     "TRACE: [IntInClassLogger] trace message JHGFDSR^1\n"
                     "DEBUG: [IntInClassLogger] debug message IGg2474fdksd fo 151617\n"