[ndnSIM] util: Redirect logger to use the logging facility of NS3
This commit also includes:
1) Disambiguation of logging component name of CertificateFetcherFromNetwork
2) Addition of the "ndn-cxx." prefix
Change-Id: Ib345978575104afa436296d2f5a824a39e3a9721
diff --git a/src/security/v2/certificate-fetcher-from-network.cpp b/src/security/v2/certificate-fetcher-from-network.cpp
index 5d7bcaa..8c4d362 100644
--- a/src/security/v2/certificate-fetcher-from-network.cpp
+++ b/src/security/v2/certificate-fetcher-from-network.cpp
@@ -27,7 +27,7 @@
namespace security {
namespace v2 {
-NDN_LOG_INIT(ndn.security.v2.CertificateFetcher);
+NDN_LOG_INIT(ndn.security.v2.CertificateFetcher.FromNetwork);
#define NDN_LOG_DEBUG_DEPTH(x) NDN_LOG_DEBUG(std::string(state->getDepth() + 1, '>') << " " << x)
#define NDN_LOG_TRACE_DEPTH(x) NDN_LOG_TRACE(std::string(state->getDepth() + 1, '>') << " " << x)
diff --git a/src/util/logger.cpp b/src/util/logger.cpp
deleted file mode 100644
index 8c752d3..0000000
--- a/src/util/logger.cpp
+++ /dev/null
@@ -1,152 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2013-2018 Regents of the University of California.
- *
- * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
- *
- * ndn-cxx library is free software: you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- *
- * You should have received copies of the GNU General Public License and GNU Lesser
- * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- */
-
-#include "logger.hpp"
-
-#include "logging.hpp"
-#include "time.hpp"
-
-#include <cinttypes> // for PRIdLEAST64
-#include <cstdlib> // for std::abs()
-#include <cstring> // for std::strspn()
-#include <stdio.h> // for snprintf()
-
-namespace ndn {
-namespace util {
-
-std::ostream&
-operator<<(std::ostream& os, LogLevel level)
-{
- switch (level) {
- case LogLevel::FATAL:
- return os << "FATAL";
- case LogLevel::NONE:
- return os << "NONE";
- case LogLevel::ERROR:
- return os << "ERROR";
- case LogLevel::WARN:
- return os << "WARN";
- case LogLevel::INFO:
- return os << "INFO";
- case LogLevel::DEBUG:
- return os << "DEBUG";
- case LogLevel::TRACE:
- return os << "TRACE";
- case LogLevel::ALL:
- return os << "ALL";
- }
-
- BOOST_THROW_EXCEPTION(std::invalid_argument("unknown log level " + to_string(static_cast<int>(level))));
-}
-
-LogLevel
-parseLogLevel(const std::string& s)
-{
- if (s == "FATAL")
- return LogLevel::FATAL;
- else if (s == "NONE")
- return LogLevel::NONE;
- else if (s == "ERROR")
- return LogLevel::ERROR;
- else if (s == "WARN")
- return LogLevel::WARN;
- else if (s == "INFO")
- return LogLevel::INFO;
- else if (s == "DEBUG")
- return LogLevel::DEBUG;
- else if (s == "TRACE")
- return LogLevel::TRACE;
- else if (s == "ALL")
- return LogLevel::ALL;
-
- 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 char* name)
- : m_moduleName(name)
-{
- if (!isValidLoggerName(m_moduleName)) {
- BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + m_moduleName + "' is invalid"));
- }
- this->setLevel(LogLevel::NONE);
- Logging::get().addLoggerImpl(*this);
-}
-
-void
-Logger::registerModuleName(const char* name)
-{
- std::string moduleName(name);
- if (!isValidLoggerName(moduleName)) {
- BOOST_THROW_EXCEPTION(std::invalid_argument("Logger name '" + moduleName + "' is invalid"));
- }
- Logging::get().registerLoggerNameImpl(std::move(moduleName));
-}
-
-namespace detail {
-
-std::ostream&
-operator<<(std::ostream& os, LoggerTimestamp)
-{
- using namespace ndn::time;
-
- const auto sinceEpoch = system_clock::now().time_since_epoch();
- BOOST_ASSERT(sinceEpoch.count() >= 0);
- // use abs() to silence truncation warning in snprintf(), see #4365
- const auto usecs = std::abs(duration_cast<microseconds>(sinceEpoch).count());
- const auto usecsPerSec = microseconds::period::den;
-
- // 10 (whole seconds) + '.' + 6 (fraction) + '\0'
- char buffer[10 + 1 + 6 + 1];
- BOOST_ASSERT_MSG(usecs / usecsPerSec <= 9999999999, "whole seconds cannot fit in 10 characters");
-
- static_assert(std::is_same<microseconds::rep, int_least64_t>::value,
- "PRIdLEAST64 is incompatible with microseconds::rep");
- // std::snprintf unavailable on some platforms, see #2299
- ::snprintf(buffer, sizeof(buffer), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
- usecs / usecsPerSec, usecs % usecsPerSec);
-
- return os << buffer;
-}
-
-} // namespace detail
-} // namespace util
-} // namespace ndn
diff --git a/src/util/logger.hpp b/src/util/logger.hpp
index 75d86b4..570b77e 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-2018 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).
*
@@ -22,268 +22,23 @@
#ifndef NDN_UTIL_LOGGER_HPP
#define NDN_UTIL_LOGGER_HPP
-#include "../common.hpp"
-
-#ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
-#include "ndn-cxx-custom-logger.hpp"
-#else
-
-#include <boost/log/common.hpp>
-#include <boost/log/sources/logger.hpp>
-#include <atomic>
+#include "ns3/log.h"
namespace ndn {
namespace util {
-/** \brief Indicates the severity level of a log message.
+/** \brief declare a log module
*/
-enum class LogLevel {
- FATAL = -1, ///< fatal (will be logged unconditionally)
- NONE = 0, ///< no messages
- ERROR = 1, ///< serious error messages
- WARN = 2, ///< warning messages
- INFO = 3, ///< informational messages
- DEBUG = 4, ///< debug messages
- TRACE = 5, ///< trace messages (most verbose)
- ALL = 255 ///< all messages
-};
+#define NDN_LOG_INIT(name) NS_LOG_COMPONENT_DEFINE("ndn-cxx." BOOST_STRINGIZE(name));
-/** \brief Output LogLevel as a string.
- * \throw std::invalid_argument unknown \p level
- */
-std::ostream&
-operator<<(std::ostream& os, LogLevel level);
-
-/** \brief Parse LogLevel from a string.
- * \throw std::invalid_argument unknown level name
- */
-LogLevel
-parseLogLevel(const std::string& s);
-
-/** \brief Represents a log module in the logging facility.
- *
- * \note Normally, loggers should be defined using #NDN_LOG_INIT, #NDN_LOG_MEMBER_INIT,
- * or #NDN_LOG_MEMBER_INIT_SPECIALIZED.
- */
-class Logger : public boost::log::sources::logger_mt
-{
-public:
- explicit
- Logger(const char* name);
-
- static void
- registerModuleName(const char* name);
-
- const std::string&
- getModuleName() const
- {
- return m_moduleName;
- }
-
- bool
- isLevelEnabled(LogLevel level) const
- {
- return m_currentLevel.load(std::memory_order_relaxed) >= level;
- }
-
- void
- setLevel(LogLevel level)
- {
- m_currentLevel.store(level, std::memory_order_relaxed);
- }
-
-private:
- const std::string m_moduleName;
- std::atomic<LogLevel> m_currentLevel;
-};
-
-namespace detail {
-
-/** \brief A tag type used to output a timestamp to a stream.
- * \code
- * std::clog << LoggerTimestamp();
- * \endcode
- */
-struct LoggerTimestamp
-{
-};
-
-/** \brief Write a timestamp to \p os.
- * \note This function is thread-safe.
- */
-std::ostream&
-operator<<(std::ostream& os, LoggerTimestamp);
-
-/** \cond */
-template<class T>
-struct ExtractArgument;
-
-template<class T, class U>
-struct ExtractArgument<T(U)>
-{
- using type = U;
-};
-
-template<class T>
-using ArgumentType = typename ExtractArgument<T>::type;
-/** \endcond */
-
-} // namespace detail
-
-/** \cond */
-// implementation detail
-#define NDN_LOG_REGISTER_NAME(name) \
- []() -> bool { \
- ::ndn::util::Logger::registerModuleName(BOOST_STRINGIZE(name)); \
- return true; \
- }()
-
-// implementation detail
-#define NDN_LOG_INIT_FUNCTION_BODY(name) \
- { \
- static ::ndn::util::Logger logger(BOOST_STRINGIZE(name)); \
- return logger; \
- }
-/** \endcond */
-
-/** \brief Define a non-member log module.
- *
- * This macro can be used in global scope to define a log module for an entire translation
- * unit, or in namespace scope to define a log module for the enclosing namespace.
- * Use #NDN_LOG_MEMBER_INIT to define a log module as a class or struct member.
- *
- * \warning Do not use this macro in header files unless you know what you're doing,
- * as it can easily trigger ODR violations if used incorrectly.
- *
- * \param name the logger name
- * \note The logger name is restricted to alphanumeric characters and a select set of
- * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
- * a dot (`.`), or contain multiple consecutive dots.
- */
-#define NDN_LOG_INIT(name) \
- namespace { \
- const bool ndn_cxx_loggerRegistration __attribute__((used)) = NDN_LOG_REGISTER_NAME(name); \
- ::ndn::util::Logger& ndn_cxx_getLogger() \
- NDN_LOG_INIT_FUNCTION_BODY(name) \
- } \
- struct ndn_cxx_allow_trailing_semicolon
-
-/** \brief Declare a member log module, without initializing it.
- *
- * This macro should only be used to declare a log module as a class or struct member.
- * It is recommended to place this macro in the private or protected section of the
- * class or struct definition. Use #NDN_LOG_INIT to declare a non-member log module.
- *
- * If the enclosing class is a template, this macro can be used in conjunction with
- * #NDN_LOG_MEMBER_DECL_SPECIALIZED and #NDN_LOG_MEMBER_INIT_SPECIALIZED to provide
- * different loggers for different template specializations.
- */
-#define NDN_LOG_MEMBER_DECL() \
- static ::ndn::util::Logger& ndn_cxx_getLogger(); \
- private: \
- static const bool ndn_cxx_loggerRegistration
-
-/** \brief Initialize a member log module.
- *
- * This macro should only be used to initialize a previously declared member log module.
- * It must be placed in a .cpp file (NOT in a header file), in the same namespace as
- * the class or struct that contains the log module.
- *
- * \param cls class name; wrap in parentheses if it contains commas
- * \param name the logger name
- * \note The logger name is restricted to alphanumeric characters and a select set of
- * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
- * a dot (`.`), or contain multiple consecutive dots.
- */
-#define NDN_LOG_MEMBER_INIT(cls, name) \
- const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration = \
- NDN_LOG_REGISTER_NAME(name); \
- ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() \
- NDN_LOG_INIT_FUNCTION_BODY(name) \
- struct ndn_cxx_allow_trailing_semicolon
-
-/** \brief Declare an explicit specialization of a member log module of a class template.
- *
- * \param cls fully specialized class name; wrap in parentheses if it contains commas
- */
-#define NDN_LOG_MEMBER_DECL_SPECIALIZED(cls) \
- template<> \
- const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration; \
- template<> \
- ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger()
-
-/** \brief Define an explicit specialization of a member log module of a class template.
- *
- * This macro must be placed in a .cpp file (NOT in a header file), in the same namespace
- * as the class template that contains the log module.
- *
- * \param cls fully specialized class name; wrap in parentheses if it contains commas
- * \param name the logger name
- * \note The logger name is restricted to alphanumeric characters and a select set of
- * symbols: `~`, `#`, `%`, `_`, `<`, `>`, `.`, `-`. It must not start or end with
- * a dot (`.`), or contain multiple consecutive dots.
- */
-#define NDN_LOG_MEMBER_INIT_SPECIALIZED(cls, name) \
- template<> \
- const bool ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_loggerRegistration = \
- NDN_LOG_REGISTER_NAME(name); \
- template<> \
- ::ndn::util::Logger& ::ndn::util::detail::ArgumentType<void(cls)>::ndn_cxx_getLogger() \
- NDN_LOG_INIT_FUNCTION_BODY(name) \
- struct ndn_cxx_allow_trailing_semicolon
-
-/** \cond */
-#if BOOST_VERSION == 105900
-// workaround Boost bug 11549
-#define NDN_BOOST_LOG(x) BOOST_LOG(x) << ""
-#else
-#define NDN_BOOST_LOG(x) BOOST_LOG(x)
-#endif
-
-// implementation detail
-#define NDN_LOG_INTERNAL(lvl, lvlstr, expression) \
- do { \
- if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
- NDN_BOOST_LOG(ndn_cxx_getLogger()) << ::ndn::util::detail::LoggerTimestamp{} \
- << " " BOOST_STRINGIZE(lvlstr) ": [" << ndn_cxx_getLogger().getModuleName() << "] " \
- << expression; \
- } \
- } while (false)
-/** \endcond */
-
-/** \brief Log at TRACE level.
- * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
- */
-#define NDN_LOG_TRACE(expression) NDN_LOG_INTERNAL(TRACE, TRACE, expression)
-
-/** \brief Log at DEBUG level.
- * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
- */
-#define NDN_LOG_DEBUG(expression) NDN_LOG_INTERNAL(DEBUG, DEBUG, expression)
-
-/** \brief Log at INFO level.
- * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
- */
-#define NDN_LOG_INFO(expression) NDN_LOG_INTERNAL(INFO, INFO, expression)
-
-/** \brief Log at WARN level.
- * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
- */
-#define NDN_LOG_WARN(expression) NDN_LOG_INTERNAL(WARN, WARNING, expression)
-
-/** \brief Log at ERROR level.
- * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
- */
-#define NDN_LOG_ERROR(expression) NDN_LOG_INTERNAL(ERROR, ERROR, expression)
-
-/** \brief Log at FATAL level.
- * \pre A log module must be declared in the same translation unit, class, struct, or namespace.
- */
-#define NDN_LOG_FATAL(expression) NDN_LOG_INTERNAL(FATAL, FATAL, expression)
+#define NDN_LOG_TRACE(expression) NS_LOG_LOGIC(expression)
+#define NDN_LOG_DEBUG(expression) NS_LOG_DEBUG(expression)
+#define NDN_LOG_INFO(expression) NS_LOG_INFO(expression)
+#define NDN_LOG_WARN(expression) NS_LOG_ERROR(expression)
+#define NDN_LOG_ERROR(expression) NS_LOG_WARN(expression)
+#define NDN_LOG_FATAL(expression) NS_LOG_FATAL(expression)
} // namespace util
} // namespace ndn
-#endif // HAVE_NDN_CXX_CUSTOM_LOGGER
-
#endif // NDN_UTIL_LOGGER_HPP
diff --git a/src/util/logging.cpp b/src/util/logging.cpp
deleted file mode 100644
index 0e68092..0000000
--- a/src/util/logging.cpp
+++ /dev/null
@@ -1,260 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2013-2018 Regents of the University of California.
- *
- * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
- *
- * ndn-cxx library is free software: you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- *
- * You should have received copies of the GNU General Public License and GNU Lesser
- * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- */
-
-#include "logging.hpp"
-#include "logger.hpp"
-
-#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>
-#include <sstream>
-
-// suppress warning caused by <boost/log/sinks/text_ostream_backend.hpp>
-#ifdef __clang__
-#pragma clang diagnostic ignored "-Wundefined-func-template"
-#endif
-
-namespace ndn {
-namespace util {
-
-static const LogLevel INITIAL_DEFAULT_LEVEL = LogLevel::NONE;
-
-Logging&
-Logging::get()
-{
- // Initialization of block-scope variables with static storage duration is thread-safe.
- // See ISO C++ standard [stmt.dcl]/4
- static Logging instance;
- return instance;
-}
-
-Logging::Logging()
-{
- this->setDestinationImpl(shared_ptr<std::ostream>(&std::clog, bind([]{})));
-
- const char* environ = std::getenv("NDN_LOG");
- if (environ != nullptr) {
- this->setLevelImpl(environ);
- }
-}
-
-void
-Logging::addLoggerImpl(Logger& logger)
-{
- std::lock_guard<std::mutex> lock(m_mutex);
-
- const std::string& moduleName = logger.getModuleName();
- m_loggers.emplace(moduleName, &logger);
-
- logger.setLevel(findLevel(moduleName));
-}
-
-void
-Logging::registerLoggerNameImpl(std::string name)
-{
- std::lock_guard<std::mutex> lock(m_mutex);
- m_loggers.emplace(std::move(name), nullptr);
-}
-
-std::set<std::string>
-Logging::getLoggerNamesImpl() const
-{
- std::lock_guard<std::mutex> lock(m_mutex);
-
- std::set<std::string> loggerNames;
- boost::copy(m_loggers | boost::adaptors::map_keys, std::inserter(loggerNames, loggerNames.end()));
- return loggerNames;
-}
-
-LogLevel
-Logging::findLevel(std::string mn) const
-{
- 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);
- return it != m_enabledLevel.end() ? it->second : INITIAL_DEFAULT_LEVEL;
-}
-
-#ifdef NDN_CXX_HAVE_TESTS
-bool
-Logging::removeLogger(Logger& logger)
-{
- const std::string& moduleName = logger.getModuleName();
- auto range = m_loggers.equal_range(moduleName);
- for (auto i = range.first; i != range.second; ++i) {
- if (i->second == &logger) {
- m_loggers.erase(i);
- return true;
- }
- }
- return false;
-}
-#endif // NDN_CXX_HAVE_TESTS
-
-void
-Logging::setLevelImpl(const std::string& prefix, LogLevel level)
-{
- std::lock_guard<std::mutex> lock(m_mutex);
-
- 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 (const auto& pair : m_loggers) {
- if (pair.first.compare(0, p.size(), p) == 0 && pair.second != nullptr) {
- pair.second->setLevel(level);
- }
- }
- }
- else {
- m_enabledLevel[prefix] = level;
- auto range = boost::make_iterator_range(m_loggers.equal_range(prefix));
- for (const auto& pair : range) {
- if (pair.second != nullptr) {
- pair.second->setLevel(level);
- }
- }
- }
-}
-
-void
-Logging::setLevelImpl(const std::string& config)
-{
- std::stringstream ss(config);
- std::string configModule;
- while (std::getline(ss, configModule, ':')) {
- size_t ind = configModule.find('=');
- if (ind == std::string::npos) {
- BOOST_THROW_EXCEPTION(std::invalid_argument("malformed logging config: '=' is missing"));
- }
-
- std::string moduleName = configModule.substr(0, ind);
- LogLevel level = parseLogLevel(configModule.substr(ind + 1));
- this->setLevelImpl(moduleName, level);
- }
-}
-
-#ifdef NDN_CXX_HAVE_TESTS
-void
-Logging::resetLevels()
-{
- this->setLevelImpl("*", INITIAL_DEFAULT_LEVEL);
- m_enabledLevel.clear();
-}
-#endif // NDN_CXX_HAVE_TESTS
-
-void
-Logging::setDestination(std::ostream& os)
-{
- setDestination(shared_ptr<std::ostream>(&os, bind([]{})));
-}
-
-void
-Logging::setDestinationImpl(shared_ptr<std::ostream> os)
-{
- std::lock_guard<std::mutex> lock(m_mutex);
-
- 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>(m_destination.get(), bind([]{})));
-
- if (m_sink != nullptr) {
- boost::log::core::get()->remove_sink(m_sink);
- m_sink->flush();
- m_sink.reset();
- }
-
- m_sink = boost::make_shared<Sink>(backend);
- m_sink->set_formatter(boost::log::expressions::stream << boost::log::expressions::message);
- boost::log::core::get()->add_sink(m_sink);
-}
-
-#ifdef NDN_CXX_HAVE_TESTS
-shared_ptr<std::ostream>
-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
-Logging::flushImpl()
-{
- m_sink->flush();
-}
-
-} // namespace util
-} // namespace ndn
diff --git a/src/util/logging.hpp b/src/util/logging.hpp
deleted file mode 100644
index 8fc6342..0000000
--- a/src/util/logging.hpp
+++ /dev/null
@@ -1,214 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2013-2018 Regents of the University of California.
- *
- * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
- *
- * ndn-cxx library is free software: you can redistribute it and/or modify it under the
- * terms of the GNU Lesser General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- *
- * You should have received copies of the GNU General Public License and GNU Lesser
- * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- */
-
-#ifndef NDN_UTIL_LOGGING_HPP
-#define NDN_UTIL_LOGGING_HPP
-
-#include "../common.hpp"
-
-#ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
-#include "ndn-cxx-custom-logging.hpp"
-#else
-
-#include <boost/log/sinks.hpp>
-#include <mutex>
-#include <unordered_map>
-
-namespace ndn {
-namespace util {
-
-enum class LogLevel;
-class Logger;
-
-/** \brief Controls the logging facility.
- *
- * \note Public static methods are thread safe.
- * Non-public methods are not guaranteed to be thread safe.
- */
-class Logging : noncopyable
-{
-public:
- /** \brief Get list of all registered logger names.
- */
- static std::set<std::string>
- getLoggerNames();
-
- /** \brief Set severity 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 that prefix,
- * or `"*"` for all modules
- * \param level minimum severity level
- *
- * Log messages are output only if their severity is greater than the current minimum severity
- * level. The initial severity level is \c LogLevel::NONE, which enables FATAL messages only.
- */
- static void
- setLevel(const std::string& prefix, LogLevel level);
-
- /** \brief Set severity levels with a config string.
- * \param config colon-separated `key=value` pairs
- * \throw std::invalid_argument config string is malformed
- *
- * \code
- * Logging::setLevel("*=INFO:Face=DEBUG:NfdController=WARN");
- * \endcode
- * is equivalent to:
- * \code
- * Logging::setLevel("*", LogLevel::INFO);
- * Logging::setLevel("Face", LogLevel::DEBUG);
- * Logging::setLevel("NfdController", LogLevel::WARN);
- * \endcode
- */
- static void
- setLevel(const std::string& config);
-
- /** \brief Set log destination.
- * \param os a stream for log output
- *
- * The initial destination is `std::clog`.
- */
- static void
- setDestination(shared_ptr<std::ostream> os);
-
- /** \brief Set log destination.
- * \param os a stream for log output; caller must ensure it remains valid
- * until setDestination() is invoked again or program exits
- *
- * This is equivalent to `setDestination(shared_ptr<std::ostream>(&os, nullDeleter))`.
- */
- static void
- setDestination(std::ostream& os);
-
- /** \brief Flush log backend.
- *
- * This ensures all log messages are written to the destination stream.
- */
- static void
- flush();
-
-private:
- Logging();
-
- void
- addLoggerImpl(Logger& logger);
-
- void
- registerLoggerNameImpl(std::string name);
-
- std::set<std::string>
- 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(std::string moduleName) const;
-
- void
- setLevelImpl(const std::string& prefix, LogLevel level);
-
- void
- setLevelImpl(const std::string& config);
-
- void
- setDestinationImpl(shared_ptr<std::ostream> os);
-
- void
- flushImpl();
-
-NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
- static Logging&
- get();
-
-#ifdef NDN_CXX_HAVE_TESTS
- bool
- removeLogger(Logger& logger);
-
- void
- resetLevels();
-
- shared_ptr<std::ostream>
- 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:
- friend Logger;
-
- 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; ///< module name => logger instance
-
- using Sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>;
- boost::shared_ptr<Sink> m_sink;
- shared_ptr<std::ostream> m_destination;
-};
-
-inline std::set<std::string>
-Logging::getLoggerNames()
-{
- return get().getLoggerNamesImpl();
-}
-
-inline void
-Logging::setLevel(const std::string& prefix, LogLevel level)
-{
- get().setLevelImpl(prefix, level);
-}
-
-inline void
-Logging::setLevel(const std::string& config)
-{
- get().setLevelImpl(config);
-}
-
-inline void
-Logging::setDestination(shared_ptr<std::ostream> os)
-{
- get().setDestinationImpl(std::move(os));
-}
-
-inline void
-Logging::flush()
-{
- get().flushImpl();
-}
-
-} // namespace util
-} // namespace ndn
-
-#endif // HAVE_NDN_CXX_CUSTOM_LOGGER
-
-#endif // NDN_UTIL_LOGGING_HPP