core: Allow customization of Logger and LoggerFactory
Change-Id: I48a8217ab4cfb34ce2993f11a26266723d8d2923
Refs: #2433
diff --git a/core/logger-factory.cpp b/core/logger-factory.cpp
index b0ef3cd..483387f 100644
--- a/core/logger-factory.cpp
+++ b/core/logger-factory.cpp
@@ -1,11 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014 Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,10 +21,14 @@
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#include "logger-factory.hpp"
+#ifdef HAVE_CUSTOM_LOGGER
+#error "This file should not be compiled when custom logger is used"
+#endif
+
namespace nfd {
NFD_LOG_INIT("LoggerFactory");
diff --git a/core/logger-factory.hpp b/core/logger-factory.hpp
index cbf7424..9f4a9f3 100644
--- a/core/logger-factory.hpp
+++ b/core/logger-factory.hpp
@@ -1,11 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014 Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,12 +21,17 @@
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#ifndef NFD_CORE_LOGGER_FACTORY_HPP
#define NFD_CORE_LOGGER_FACTORY_HPP
#include "common.hpp"
+
+#ifdef HAVE_CUSTOM_LOGGER
+#include "custom-logger-factory.hpp"
+#else
+
#include "config-file.hpp"
#include "logger.hpp"
@@ -107,4 +113,6 @@
} // namespace nfd
+#endif // HAVE_CUSTOM_LOGGER
+
#endif // NFD_CORE_LOGGER_FACTORY_HPP
diff --git a/core/logger.cpp b/core/logger.cpp
index e971613..5ba8c39 100644
--- a/core/logger.cpp
+++ b/core/logger.cpp
@@ -24,6 +24,11 @@
*/
#include "logger.hpp"
+
+#ifdef HAVE_CUSTOM_LOGGER
+#error "This file should not be compiled when custom logger is used"
+#endif
+
#include <ndn-cxx/util/time.hpp>
#include <cinttypes>
#include <stdio.h>
diff --git a/core/logger.hpp b/core/logger.hpp
index 5efee3c..6d01fa0 100644
--- a/core/logger.hpp
+++ b/core/logger.hpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -28,19 +28,23 @@
#include "common.hpp"
+#ifdef HAVE_CUSTOM_LOGGER
+#include "custom-logger.hpp"
+#else
+
namespace nfd {
/** \brief indicates a log level
* \note This type is internal. Logger should be accessed through NFD_LOG_* macros.
*/
enum LogLevel {
+ LOG_FATAL = -1, // fatal (will be logged unconditionally)
LOG_NONE = 0, // no messages
LOG_ERROR = 1, // serious error messages
LOG_WARN = 2, // warning messages
LOG_INFO = 3, // informational messages
LOG_DEBUG = 4, // debug messages
LOG_TRACE = 5, // trace messages (most verbose)
- // LOG_FATAL is not a level and is logged unconditionally
LOG_ALL = 255 // all messages
};
@@ -57,10 +61,7 @@
bool
isEnabled(LogLevel level) const
{
- // std::cerr << m_moduleName <<
- // " enabled = " << m_enabledLogLevel
- // << " level = " << level << std::endl;
- return (m_enabledLogLevel >= level);
+ return m_enabledLogLevel >= level;
}
void
@@ -140,13 +141,10 @@
#define NFD_LOG_INFO(expression) NFD_LOG(INFO, INFO, expression)
#define NFD_LOG_WARN(expression) NFD_LOG(WARN, WARNING, expression)
#define NFD_LOG_ERROR(expression) NFD_LOG(ERROR, ERROR, expression)
-
-#define NFD_LOG_FATAL(expression) \
-do { \
- std::clog << ::nfd::Logger::now() << " FATAL: " \
- << "[" << g_logger << "] " << expression << "\n"; \
-} while (false)
+#define NFD_LOG_FATAL(expression) NFD_LOG(FATAL, FATAL, expression)
} // namespace nfd
+#endif // HAVE_CUSTOM_LOGGER
+
#endif // NFD_CORE_LOGGER_HPP
diff --git a/wscript b/wscript
index b5b601c..8133b93 100644
--- a/wscript
+++ b/wscript
@@ -55,6 +55,11 @@
nfdopt.add_option('--with-other-tests', action='store_true', default=False,
dest='with_other_tests', help='''Build other tests''')
+ nfdopt.add_option('--with-custom-logger', type='string', default=None,
+ dest='with_custom_logger',
+ help='''Path to custom-logger.hpp and custom-logger-factory.hpp '''
+ '''implementing Logger and LoggerFactory interfaces''')
+
def configure(conf):
conf.load(['compiler_cxx', 'gnu_dirs',
'default-compiler-flags', 'pch', 'boost-kqueue',
@@ -129,6 +134,11 @@
conf.check_cxx(function_name='pcap_set_immediate_mode', header_name='pcap/pcap.h',
cxxflags='-Wno-error', use='LIBPCAP', mandatory=False)
+ if conf.options.with_custom_logger:
+ conf.define('HAVE_CUSTOM_LOGGER', 1)
+ conf.env['INCLUDES_CUSTOM_LOGGER'] = [conf.options.with_custom_logger]
+ conf.env['HAVE_CUSTOM_LOGGER'] = 1
+
conf.load('coverage')
conf.define('DEFAULT_CONFIG_FILE', '%s/ndn/nfd.conf' % conf.env['SYSCONFDIR'])
@@ -161,13 +171,19 @@
target='core-objects',
name='core-objects',
features='cxx pch',
- source=bld.path.ant_glob(['core/**/*.cpp']),
+ source=bld.path.ant_glob(['core/**/*.cpp'],
+ excl=['core/logger*.cpp']),
use='version BOOST NDN_CXX LIBRT',
includes='. core',
export_includes='. core',
headers='common.hpp',
)
+ if bld.env['HAVE_CUSTOM_LOGGER']:
+ core.use += " CUSTOM_LOGGER"
+ else:
+ core.source += bld.path.ant_glob(['core/logger*.cpp'])
+
nfd_objects = bld(
target='daemon-objects',
name='daemon-objects',