util: Advanced filtering of the logging modules

refs: #3918

Change-Id: I766deb5ccd0d7f4472679d9955c8e55bad41f375
diff --git a/src/util/logger.cpp b/src/util/logger.cpp
index 8cb7d80..64cb5a2 100644
--- a/src/util/logger.cpp
+++ b/src/util/logger.cpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -25,7 +25,7 @@
 #include "time.hpp"
 
 #include <cinttypes>
-#include <stdio.h>
+#include <cstring>
 
 namespace ndn {
 namespace util {
@@ -78,9 +78,33 @@
   BOOST_THROW_EXCEPTION(std::invalid_argument("unrecognized log level '" + s + "'"));
 }
 
+/**
+ * \brief checks if incoming logger name meets criteria
+ * \param name name of logger
+ */
+static bool
+isValidLoggerName(const std::string& name)
+{
+  // acceptable characters for Logger name
+  const char* okChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~#%_<>.-";
+  if (std::strspn(name.c_str(), okChars) != name.size()) {
+    return false;
+  }
+  if (name.empty() || name.front() == '.' || name.back() == '.') {
+    return false;
+  }
+  if (name.find("..") != std::string::npos) {
+    return false;
+  }
+  return true;
+}
+
 Logger::Logger(const std::string& name)
   : m_moduleName(name)
 {
+  if (!isValidLoggerName(name)) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name " + name + " is invalid"));
+  }
   this->setLevel(LogLevel::NONE);
   Logging::addLogger(*this);
 }
diff --git a/src/util/logger.hpp b/src/util/logger.hpp
index e9efba9..548390b 100644
--- a/src/util/logger.hpp
+++ b/src/util/logger.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -93,6 +93,10 @@
 };
 
 /** \brief declare a log module
+ *
+ *  \note Logger name is restricted to alphanumeric characters and a select set of
+ *  symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`
+ *  A logger name must not start or end with `.` or contain consecutive `.`
  */
 #define NDN_LOG_INIT(name) \
   namespace { \
diff --git a/src/util/logging.cpp b/src/util/logging.cpp
index 06948c1..21a573c 100644
--- a/src/util/logging.cpp
+++ b/src/util/logging.cpp
@@ -25,6 +25,7 @@
 #include <boost/log/expressions.hpp>
 #include <boost/range/adaptor/map.hpp>
 #include <boost/range/algorithm/copy.hpp>
+#include <boost/range/iterator_range.hpp>
 
 #include <cstdlib>
 #include <iostream>
@@ -65,18 +66,14 @@
   std::lock_guard<std::mutex> lock(m_mutex);
 
   const std::string& moduleName = logger.getModuleName();
-  m_loggers.insert({moduleName, &logger});
+  m_loggers.emplace(moduleName, &logger);
 
-  auto levelIt = m_enabledLevel.find(moduleName);
-  if (levelIt == m_enabledLevel.end()) {
-    levelIt = m_enabledLevel.find("*");
-  }
-  LogLevel level = levelIt == m_enabledLevel.end() ? INITIAL_DEFAULT_LEVEL : levelIt->second;
+  LogLevel level = findLevel(moduleName);
   logger.setLevel(level);
 }
 
 std::set<std::string>
-Logging::getLoggerNamesImpl()
+Logging::getLoggerNamesImpl() const
 {
   std::lock_guard<std::mutex> lock(m_mutex);
 
@@ -85,6 +82,42 @@
   return loggerNames;
 }
 
+LogLevel
+Logging::findLevel(const std::string& moduleName) const
+{
+  std::string mn = moduleName;
+  while (!mn.empty()) {
+    auto it = m_enabledLevel.find(mn);
+    if (it != m_enabledLevel.end()) {
+      return it->second;
+    }
+    size_t pos = mn.find_last_of('.');
+    if (pos < mn.size() - 1) {
+      mn = mn.substr(0, pos + 1);
+    }
+    else if (pos == mn.size() - 1) {
+      mn.pop_back();
+      pos = mn.find_last_of('.');
+      if (pos != std::string::npos) {
+        mn = mn.substr(0, pos + 1);
+      }
+      else {
+        mn = "";
+      }
+    }
+    else {
+      mn = "";
+    }
+  }
+  auto it = m_enabledLevel.find(mn);
+  if (it != m_enabledLevel.end()) {
+    return it->second;
+  }
+  else {
+    return INITIAL_DEFAULT_LEVEL;
+  }
+}
+
 #ifdef NDN_CXX_HAVE_TESTS
 bool
 Logging::removeLogger(Logger& logger)
@@ -102,30 +135,38 @@
 #endif // NDN_CXX_HAVE_TESTS
 
 void
-Logging::setLevelImpl(const std::string& moduleName, LogLevel level)
+Logging::setLevelImpl(const std::string& prefix, LogLevel level)
 {
   std::lock_guard<std::mutex> lock(m_mutex);
 
-  if (moduleName == "*") {
-    this->setDefaultLevel(level);
-    return;
+  if (prefix.empty() || prefix.back() == '*') {
+    std::string p = prefix;
+    if (!p.empty()) {
+      p.pop_back();
+    }
+
+    for (auto i = m_enabledLevel.begin(); i != m_enabledLevel.end();) {
+      if (i->first.compare(0, p.size(), p) == 0) {
+        i = m_enabledLevel.erase(i);
+      }
+      else {
+        ++i;
+      }
+    }
+    m_enabledLevel[p] = level;
+
+    for (auto&& it : m_loggers) {
+      if (it.first.compare(0, p.size(), p) == 0) {
+        it.second->setLevel(level);
+      }
+    }
   }
-
-  m_enabledLevel[moduleName] = level;
-  auto range = m_loggers.equal_range(moduleName);
-  for (auto i = range.first; i != range.second; ++i) {
-    i->second->setLevel(level);
-  }
-}
-
-void
-Logging::setDefaultLevel(LogLevel level)
-{
-  m_enabledLevel.clear();
-  m_enabledLevel["*"] = level;
-
-  for (auto i = m_loggers.begin(); i != m_loggers.end(); ++i) {
-    i->second->setLevel(level);
+  else {
+    m_enabledLevel[prefix] = level;
+    auto range = boost::make_iterator_range(m_loggers.equal_range(prefix));
+    for (auto&& it : range) {
+      it.second->setLevel(level);
+    }
   }
 }
 
@@ -141,43 +182,16 @@
     }
 
     std::string moduleName = configModule.substr(0, ind);
-    LogLevel level = parseLogLevel(configModule.substr(ind+1));
-
+    LogLevel level = parseLogLevel(configModule.substr(ind + 1));
     this->setLevelImpl(moduleName, level);
   }
 }
 
 #ifdef NDN_CXX_HAVE_TESTS
-std::string
-Logging::getLevels() const
-{
-  std::ostringstream os;
-
-  auto defaultLevelIt = m_enabledLevel.find("*");
-  if (defaultLevelIt != m_enabledLevel.end()) {
-    os << "*=" << defaultLevelIt->second << ':';
-  }
-
-  for (auto it = m_enabledLevel.begin(); it != m_enabledLevel.end(); ++it) {
-    if (it->first == "*") {
-      continue;
-    }
-    os << it->first << '=' << it->second << ':';
-  }
-
-  std::string s = os.str();
-  if (!s.empty()) {
-    s.pop_back(); // delete last ':'
-  }
-  return s;
-}
-#endif // NDN_CXX_HAVE_TESTS
-
-#ifdef NDN_CXX_HAVE_TESTS
 void
 Logging::resetLevels()
 {
-  this->setDefaultLevel(INITIAL_DEFAULT_LEVEL);
+  this->setLevelImpl("*", INITIAL_DEFAULT_LEVEL);
   m_enabledLevel.clear();
 }
 #endif // NDN_CXX_HAVE_TESTS
@@ -193,11 +207,11 @@
 {
   std::lock_guard<std::mutex> lock(m_mutex);
 
-  m_destination = os;
+  m_destination = std::move(os);
 
   auto backend = boost::make_shared<boost::log::sinks::text_ostream_backend>();
   backend->auto_flush(true);
-  backend->add_stream(boost::shared_ptr<std::ostream>(os.get(), bind([]{})));
+  backend->add_stream(boost::shared_ptr<std::ostream>(m_destination.get(), bind([]{})));
 
   if (m_sink != nullptr) {
     boost::log::core::get()->remove_sink(m_sink);
@@ -212,10 +226,25 @@
 
 #ifdef NDN_CXX_HAVE_TESTS
 shared_ptr<std::ostream>
-Logging::getDestination()
+Logging::getDestination() const
 {
   return m_destination;
 }
+
+void
+Logging::setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules)
+{
+  resetLevels();
+  for (const auto& rule : prefixRules) {
+    setLevelImpl(rule.first, rule.second);
+  }
+}
+
+const std::unordered_map<std::string, LogLevel>&
+Logging::getLevels() const
+{
+  return m_enabledLevel;
+}
 #endif // NDN_CXX_HAVE_TESTS
 
 void
diff --git a/src/util/logging.hpp b/src/util/logging.hpp
index 459cf2b..d85b524 100644
--- a/src/util/logging.hpp
+++ b/src/util/logging.hpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -58,16 +58,15 @@
   getLoggerNames();
 
   /** \brief set severity level
-   *  \param moduleName logger name, or "*" for default level
+   *  \param prefix logger prefix; this can be a specific logger name, a general prefix like ndn.a.*
+   *   to apply a setting for all modules that contain this prefix, or "*" for all modules
    *  \param level minimum severity level
    *
    *  Log messages are output only if its severity is greater than the set minimum severity level.
    *  Initial default severity level is \p LogLevel::NONE which enables FATAL only.
-   *
-   *  Changing the default level overwrites individual settings.
    */
   static void
-  setLevel(const std::string& moduleName, LogLevel level);
+  setLevel(const std::string& prefix, LogLevel level);
 
   /** \brief set severity levels with a config string
    *  \param config colon-separate key=value pairs
@@ -117,13 +116,25 @@
   addLoggerImpl(Logger& logger);
 
   std::set<std::string>
-  getLoggerNamesImpl();
+  getLoggerNamesImpl() const;
+
+  /**
+   * \brief finds the appropriate LogLevel for a logger
+   * \param moduleName name of logger
+   *
+   * This searches m_enabledLevel map to determine which LogLevel is appropriate for
+   * the incoming logger. It looks for the most specific prefix and broadens its
+   * prefix scope if a setting is not found. For example, when an incoming logger
+   * name is "ndn.a.b", it will search for "ndn.a.b" first. If this prefix is not
+   * contained in m_enabledLevel, it will search for "ndn.a.*", then "ndn.*", and
+   * finally "*". It defaults to INITIAL_DEFAULT_LEVEL if a matching prefix is not
+   * found.
+   */
+  LogLevel
+  findLevel(const std::string& moduleName) const;
 
   void
-  setLevelImpl(const std::string& moduleName, LogLevel level);
-
-  void
-  setDefaultLevel(LogLevel level);
+  setLevelImpl(const std::string& prefix, LogLevel level);
 
   void
   setLevelImpl(const std::string& config);
@@ -142,19 +153,22 @@
   bool
   removeLogger(Logger& logger);
 
-  std::string
-  getLevels() const;
-
   void
   resetLevels();
 
   shared_ptr<std::ostream>
-  getDestination();
+  getDestination() const;
+
+  void
+  setLevelImpl(const std::unordered_map<std::string, LogLevel>& prefixRules);
+
+  const std::unordered_map<std::string, LogLevel>&
+  getLevels() const;
 #endif // NDN_CXX_HAVE_TESTS
 
 private:
-  std::mutex m_mutex;
-  std::unordered_map<std::string, LogLevel> m_enabledLevel; ///< moduleName => minimum level
+  mutable std::mutex m_mutex;
+  std::unordered_map<std::string, LogLevel> m_enabledLevel; ///< module prefix => minimum level
   std::unordered_multimap<std::string, Logger*> m_loggers; ///< moduleName => logger
 
   shared_ptr<std::ostream> m_destination;
@@ -175,9 +189,9 @@
 }
 
 inline void
-Logging::setLevel(const std::string& moduleName, LogLevel level)
+Logging::setLevel(const std::string& prefix, LogLevel level)
 {
-  get().setLevelImpl(moduleName, level);
+  get().setLevelImpl(prefix, level);
 }
 
 inline void
@@ -189,7 +203,7 @@
 inline void
 Logging::setDestination(shared_ptr<std::ostream> os)
 {
-  get().setDestinationImpl(os);
+  get().setDestinationImpl(std::move(os));
 }
 
 inline void
@@ -198,7 +212,6 @@
   get().flushImpl();
 }
 
-
 } // namespace util
 } // namespace ndn