[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/ndn-cxx/security/certificate-fetcher-from-network.cpp b/ndn-cxx/security/certificate-fetcher-from-network.cpp
index a117ee4..db6f046 100644
--- a/ndn-cxx/security/certificate-fetcher-from-network.cpp
+++ b/ndn-cxx/security/certificate-fetcher-from-network.cpp
@@ -30,7 +30,7 @@
namespace security {
inline namespace v2 {
-NDN_LOG_INIT(ndn.security.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/ndn-cxx/util/logger.cpp b/ndn-cxx/util/logger.cpp
index f845ff8..c17c219 100644
--- a/ndn-cxx/util/logger.cpp
+++ b/ndn-cxx/util/logger.cpp
@@ -20,9 +20,7 @@
*/
#include "ndn-cxx/util/logger.hpp"
-#include "ndn-cxx/util/logging.hpp"
-
-#include <cstring> // for std::strspn()
+#include "ndn-cxx/detail/common.hpp"
namespace ndn {
namespace util {
@@ -75,47 +73,5 @@
NDN_THROW(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)) {
- NDN_THROW(std::invalid_argument("Logger name '" + m_moduleName + "' is invalid"));
- }
- this->setLevel(LogLevel::NONE);
- this->add_attribute(log::module.get_name(), boost::log::attributes::constant<std::string>(m_moduleName));
- Logging::get().addLoggerImpl(*this);
-}
-
-void
-Logger::registerModuleName(const char* name)
-{
- std::string moduleName(name);
- if (!isValidLoggerName(moduleName)) {
- NDN_THROW(std::invalid_argument("Logger name '" + moduleName + "' is invalid"));
- }
- Logging::get().registerLoggerNameImpl(std::move(moduleName));
-}
-
} // namespace util
} // namespace ndn
diff --git a/ndn-cxx/util/logger.hpp b/ndn-cxx/util/logger.hpp
index bdf1c07..c3bdc46 100644
--- a/ndn-cxx/util/logger.hpp
+++ b/ndn-cxx/util/logger.hpp
@@ -22,17 +22,7 @@
#ifndef NDN_CXX_UTIL_LOGGER_HPP
#define NDN_CXX_UTIL_LOGGER_HPP
-#include "ndn-cxx/detail/common.hpp"
-
-#ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
-#include "ndn-cxx/util/custom-logger.hpp"
-#else
-
-#include <boost/log/common.hpp>
-#include <boost/log/expressions/keyword.hpp>
-#include <boost/log/sources/severity_logger.hpp>
-
-#include <atomic>
+#include "ns3/log.h"
namespace ndn {
namespace util {
@@ -62,50 +52,6 @@
LogLevel
parseLogLevel(const std::string& s);
-namespace log {
-
-BOOST_LOG_ATTRIBUTE_KEYWORD(module, "Module", std::string)
-BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", LogLevel)
-
-} // namespace log
-
-/** \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::severity_logger_mt<LogLevel>
-{
-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 {
/** \cond */
@@ -130,152 +76,33 @@
} // 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.
+/** \brief declare a log module
*/
-#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
+#define NDN_LOG_INIT(name) NS_LOG_COMPONENT_DEFINE("ndn-cxx." BOOST_STRINGIZE(name))
-/** \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
+ static ::ns3::LogComponent g_log
-/** \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
+ ::ns3::LogComponent cls::g_log = ::ns3::LogComponent("ndn-cxx." BOOST_STRINGIZE(name), __FILE__)
-/** \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
- */
+// ::ns3::LogComponent ::ndn::util::detail::ArgumentType<void(cls)>::g_log = ::ns3::LogComponent("ndn-cxx." BOOST_STRINGIZE(name), __FILE__)
+
#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()
+ static ::ns3::LogComponent g_log
-/** \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
+ ::ns3::LogComponent ::ndn::util::detail::ArgumentType<void(cls)>::g_log = ::ns3::LogComponent("ndn-cxx." BOOST_STRINGIZE(name), __FILE__)
-/** \cond */
-// implementation detail
-#define NDN_LOG_INTERNAL(lvl, expression) \
- do { \
- if (ndn_cxx_getLogger().isLevelEnabled(::ndn::util::LogLevel::lvl)) { \
- BOOST_LOG_SEV(ndn_cxx_getLogger(), ::ndn::util::LogLevel::lvl) \
- << 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, 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, 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, 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, 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, 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, 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_CXX_UTIL_LOGGER_HPP
+#endif // NDN_UTIL_LOGGER_HPP
diff --git a/ndn-cxx/util/logging.cpp b/ndn-cxx/util/logging.cpp
deleted file mode 100644
index ba20396..0000000
--- a/ndn-cxx/util/logging.cpp
+++ /dev/null
@@ -1,344 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2013-2022 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 "ndn-cxx/util/logging.hpp"
-#include "ndn-cxx/util/logger.hpp"
-#include "ndn-cxx/util/time.hpp"
-
-#ifdef __ANDROID__
-#include "ndn-cxx/util/impl/logger-android.hpp"
-#endif
-
-#include <boost/log/attributes/function.hpp>
-#include <boost/log/expressions.hpp>
-#include <boost/log/expressions/attr.hpp>
-#include <boost/log/expressions/formatters/date_time.hpp>
-#include <boost/log/support/date_time.hpp>
-#include <boost/range/adaptor/map.hpp>
-#include <boost/range/algorithm/copy.hpp>
-#include <boost/range/iterator_range.hpp>
-
-#include <cinttypes> // for PRIdLEAST64
-#include <cstdio> // for std::snprintf()
-#include <cstdlib> // for std::abs()
-#include <iostream>
-#include <sstream>
-
-namespace ndn {
-namespace util {
-namespace log {
-
-static std::string
-makeTimestamp()
-{
- 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'
- std::string buffer(10 + 1 + 6 + 1, '\0'); // note 1 extra byte still needed for snprintf
- 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(&buffer.front(), buffer.size(), "%" PRIdLEAST64 ".%06" PRIdLEAST64,
- usecs / usecsPerSec, usecs % usecsPerSec);
-
- // need to remove extra 1 byte ('\0')
- buffer.pop_back();
- return buffer;
-}
-
-BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "Timestamp", std::string)
-
-} // namespace log
-
-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()
-{
-#ifndef __ANDROID__
- bool wantAutoFlush = std::getenv("NDN_LOG_NOFLUSH") == nullptr;
- auto destination = makeDefaultStreamDestination(shared_ptr<std::ostream>(&std::clog, [] (auto&&) {}),
- wantAutoFlush);
-#else
- auto destination = detail::makeAndroidLogger();
-#endif // __ANDROID__
-
- // cannot call the static setDestination(), as the singleton object is not yet constructed
- this->setDestinationImpl(std::move(destination));
-
- const char* env = std::getenv("NDN_LOG");
- if (env != nullptr) {
- this->setLevelImpl(env);
- }
-
- boost::log::core::get()->add_global_attribute("Timestamp",
- boost::log::attributes::make_function(&log::makeTimestamp));
-}
-
-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) {
- NDN_THROW(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, bool wantAutoFlush)
-{
- auto destination = makeDefaultStreamDestination(shared_ptr<std::ostream>(&os, [] (auto&&) {}),
- wantAutoFlush);
- setDestination(std::move(destination));
-}
-
-class TextOstreamBackend : public boost::log::sinks::text_ostream_backend
-{
-public:
- TextOstreamBackend(std::shared_ptr<std::ostream> os, bool wantAutoFlush)
- : m_stdPtr(std::move(os))
- {
- auto_flush(wantAutoFlush);
- add_stream(boost::shared_ptr<std::ostream>(m_stdPtr.get(), [] (auto&&) {}));
- }
-
-private:
- // Quite a mess right now because Boost.Log uses boost::shared_ptr and we are using
- // std::shared_ptr. When it is finally fixed, we can remove this mess.
- std::shared_ptr<std::ostream> m_stdPtr;
-};
-
-boost::shared_ptr<boost::log::sinks::sink>
-Logging::makeDefaultStreamDestination(shared_ptr<std::ostream> os, bool wantAutoFlush)
-{
- auto backend = boost::make_shared<TextOstreamBackend>(std::move(os), wantAutoFlush);
- auto destination = boost::make_shared<boost::log::sinks::asynchronous_sink<TextOstreamBackend>>(backend);
-
- namespace expr = boost::log::expressions;
- destination->set_formatter(expr::stream
- << expr::attr<std::string>(log::timestamp.get_name())
- << " " << std::setw(5) << expr::attr<LogLevel>(log::severity.get_name()) << ": "
- << "[" << expr::attr<std::string>(log::module.get_name()) << "] "
- << expr::smessage);
- return destination;
-}
-
-void
-Logging::setDestinationImpl(boost::shared_ptr<boost::log::sinks::sink> destination)
-{
- std::lock_guard<std::mutex> lock(m_mutex);
-
- if (destination == m_destination) {
- return;
- }
-
- if (m_destination != nullptr) {
- boost::log::core::get()->remove_sink(m_destination);
- m_destination->flush();
- }
-
- m_destination = std::move(destination);
-
- if (m_destination != nullptr) {
- boost::log::core::get()->add_sink(m_destination);
- }
-}
-
-#ifdef NDN_CXX_HAVE_TESTS
-boost::shared_ptr<boost::log::sinks::sink>
-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()
-{
- std::lock_guard<std::mutex> lock(m_mutex);
-
- if (m_destination != nullptr) {
- m_destination->flush();
- }
-}
-
-} // namespace util
-} // namespace ndn
diff --git a/ndn-cxx/util/logging.hpp b/ndn-cxx/util/logging.hpp
deleted file mode 100644
index 0019569..0000000
--- a/ndn-cxx/util/logging.hpp
+++ /dev/null
@@ -1,225 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2013-2021 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_CXX_UTIL_LOGGING_HPP
-#define NDN_CXX_UTIL_LOGGING_HPP
-
-#include "ndn-cxx/detail/common.hpp"
-
-#ifdef HAVE_NDN_CXX_CUSTOM_LOGGER
-#include "ndn-cxx/util/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 or replace log destination.
- * \param destination log backend, e.g., returned by `makeDefaultStreamDestination`
- *
- * The initial destination is `std::clog`.
- *
- * Note that if \p destination is nullptr, the destination will be removed and the
- * application is expected to add its own. If the application does not set a custom
- * destination (using this function or directly using Boost.Log routines), the default
- * Boost.Log destination will be used. Refer to Boost.Log documentation and source code
- * for details.
- */
- static void
- setDestination(boost::shared_ptr<boost::log::sinks::sink> destination);
-
- /** \brief Helper method to set stream log destination.
- * \param os a stream for log output; caller must ensure it remains valid
- * until setDestination() is invoked again or program exits
- * \param wantAutoFlush if true, the created logging sink will be auto-flushed
- *`
- * This is equivalent to `setDestination(makeDefaultStreamDestination(shared_ptr<std::ostream>(&os, nullDeleter)))`.
- *
- */
- static void
- setDestination(std::ostream& os, bool wantAutoFlush);
-
- /** \brief Flush log backend.
- *
- * This ensures all log messages are written to the destination stream.
- */
- static void
- flush();
-
- /** \brief Create stream log destination using default formatting
- */
- static boost::shared_ptr<boost::log::sinks::sink>
- makeDefaultStreamDestination(shared_ptr<std::ostream> os, bool wantAutoFlush = true);
-
-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(boost::shared_ptr<boost::log::sinks::sink> sink);
-
- void
- flushImpl();
-
-NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
- static Logging&
- get();
-
-#ifdef NDN_CXX_HAVE_TESTS
- bool
- removeLogger(Logger& logger);
-
- void
- resetLevels();
-
- boost::shared_ptr<boost::log::sinks::sink>
- 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
-
- boost::shared_ptr<boost::log::sinks::sink> 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(boost::shared_ptr<boost::log::sinks::sink> destination)
-{
- get().setDestinationImpl(std::move(destination));
-}
-
-inline void
-Logging::flush()
-{
- get().flushImpl();
-}
-
-} // namespace util
-} // namespace ndn
-
-#endif // HAVE_NDN_CXX_CUSTOM_LOGGER
-
-#endif // NDN_CXX_UTIL_LOGGING_HPP