core: Implementation of the logging module for NFD

ref: #1113

Change-Id: Ifb958ace38610b154841aab4f317b2ea9fde46e9
diff --git a/daemon/common.hpp b/daemon/common.hpp
index c377a65..fc37c02 100644
--- a/daemon/common.hpp
+++ b/daemon/common.hpp
@@ -25,6 +25,8 @@
 #include <list>
 #include <set>
 
+#include "core/logger.hpp"
+
 namespace nfd {
 
 using boost::noncopyable;
diff --git a/daemon/core/logger.cpp b/daemon/core/logger.cpp
new file mode 100644
index 0000000..14dd380
--- /dev/null
+++ b/daemon/core/logger.cpp
@@ -0,0 +1,27 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ *
+ * Author: Ilya Moiseenko <iliamo@ucla.edu>
+ */
+
+#include "logger.hpp"
+
+namespace nfd
+{
+
+Logger::Logger(const std::string& name)
+  : m_moduleName(name),
+    m_isEnabled(true)
+{
+}
+
+std::ostream&
+operator<<(std::ostream& output, const Logger& obj)
+{
+  output << obj.getName();
+  return output;
+}
+
+} // namespace nfd
diff --git a/daemon/core/logger.hpp b/daemon/core/logger.hpp
new file mode 100644
index 0000000..e483ac9
--- /dev/null
+++ b/daemon/core/logger.hpp
@@ -0,0 +1,84 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ *
+ * Author: Ilya Moiseenko <iliamo@ucla.edu>
+ */
+
+#ifndef NFD_CORE_LOGGER_HPP
+#define NFD_CORE_LOGGER_HPP
+
+#include "common.hpp"
+#include <iostream>
+
+namespace nfd {
+
+enum LogLevel {
+  LOG_NONE           = 0, // no messages
+  LOG_ERROR          = 1, // serious error messages
+  LOG_WARN           = 2, // warning messages
+  LOG_TRACE          = 3, // trace messages
+  LOG_DEBUG          = 4, // debug messages
+  LOG_INFO           = 5, // informational messages
+  LOG_FATAL          = 6, // fatal error messages
+  LOG_ALL            = 0x0fffffff, // all messages
+};
+
+class Logger
+{
+public:
+  Logger(const std::string& name);
+  
+  bool
+  isEnabled(LogLevel level)
+  {
+    return m_isEnabled;
+  }
+  
+  const std::string&
+  getName() const
+  {
+    return m_moduleName;
+  }
+  
+private:
+  std::string m_moduleName;
+  bool m_isEnabled;
+};
+
+std::ostream&
+operator<<(std::ostream& output, const Logger& obj);
+
+#define NFD_LOG_INIT(name)    \
+  static nfd::Logger \
+  g_logger = nfd::Logger(name);
+
+#define NFD_LOG_TRACE(expression) \
+    if(g_logger.isEnabled(nfd::LOG_TRACE)) \
+       std::cerr<<"TRACE: "<<"["<<g_logger<<"] " << expression << "\n"
+
+#define NFD_LOG_DEBUG(expression)\
+    if(g_logger.isEnabled(nfd::LOG_DEBUG)) \
+       std::cerr<<"DEBUG: "<<"["<<g_logger<<"] " << expression <<"\n"
+
+#define NFD_LOG_WARN(expression) \
+    if(g_logger.isEnabled(nfd::LOG_WARN)) \
+       std::cerr<<"WARNING: "<<"["<<g_logger<<"] " << expression <<"\n"
+
+#define NFD_LOG_INFO(expression)\
+    if(g_logger.isEnabled(nfd::LOG_INFO)) \
+       std::cerr<<"INFO: "<<"["<<g_logger<<"] " << expression <<"\n"
+  
+#define NFD_LOG_ERROR(expression)\
+    if(g_logger.isEnabled(nfd::LOG_ERROR)) \
+       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"
+  
+} //namespace nfd
+
+
+#endif
diff --git a/tests/core/logger.cpp b/tests/core/logger.cpp
new file mode 100644
index 0000000..35fd66c
--- /dev/null
+++ b/tests/core/logger.cpp
@@ -0,0 +1,170 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ *
+ * Author: Ilya Moiseenko <iliamo@ucla.edu>
+ */
+
+#include "core/logger.hpp"
+#include <boost/test/unit_test.hpp>
+#include <iostream>
+
+
+namespace nfd {
+
+BOOST_AUTO_TEST_SUITE(CoreLogger)
+
+BOOST_AUTO_TEST_CASE(Warn)
+{
+  std::stringstream input;
+  input << "warnTest";
+  
+  std::stringstream buffer;
+
+  // save cerr's buffer here
+  std::streambuf* sbuf = std::cerr.rdbuf();
+
+  // redirect cerr to the buffer
+  std::cerr.rdbuf(buffer.rdbuf());
+
+  NFD_LOG_INIT("WarnTest");
+  NFD_LOG_WARN(input);
+  
+  // redirect cerr back
+  std::cerr.rdbuf(sbuf);
+  
+  std::stringstream trueValue;
+  trueValue << "WARNING: [WarnTest] " << input <<"\n";
+  
+  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+}
+
+BOOST_AUTO_TEST_CASE(Debug)
+{
+  std::stringstream input;
+  input << "debugTest";
+  
+  std::stringstream buffer;
+
+  // save cerr's buffer here
+  std::streambuf* sbuf = std::cerr.rdbuf();
+
+  // redirect cerr to the buffer
+  std::cerr.rdbuf(buffer.rdbuf());
+
+  NFD_LOG_INIT("DebugTest");
+  NFD_LOG_DEBUG(input);
+  
+  // redirect cerr back
+  std::cerr.rdbuf(sbuf);
+  
+  std::stringstream trueValue;
+  trueValue << "DEBUG: [DebugTest] " << input <<"\n";
+  
+  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+}
+
+BOOST_AUTO_TEST_CASE(Info)
+{
+  std::stringstream input;
+  input << "infotest";
+  
+  std::stringstream buffer;
+
+  // save cerr's buffer here
+  std::streambuf* sbuf = std::cerr.rdbuf();
+
+  // redirect cerr to the buffer
+  std::cerr.rdbuf(buffer.rdbuf());
+
+  NFD_LOG_INIT("InfoTest");
+  NFD_LOG_INFO(input);
+  
+  // redirect cerr back
+  std::cerr.rdbuf(sbuf);
+  
+  std::stringstream trueValue;
+  trueValue << "INFO: [InfoTest] " << input <<"\n";
+  
+  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+}
+
+BOOST_AUTO_TEST_CASE(Fatal)
+{
+  std::stringstream input;
+  input << "fataltest";
+  
+  std::stringstream buffer;
+
+  // save cerr's buffer here
+  std::streambuf* sbuf = std::cerr.rdbuf();
+
+  // redirect cerr to the buffer
+  std::cerr.rdbuf(buffer.rdbuf());
+
+  NFD_LOG_INIT("FatalTest");
+  NFD_LOG_FATAL(input);
+  
+  // redirect cerr back
+  std::cerr.rdbuf(sbuf);
+  
+  std::stringstream trueValue;
+  trueValue << "FATAL: [FatalTest] " << input <<"\n";
+  
+  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+}
+
+BOOST_AUTO_TEST_CASE(Error)
+{
+  std::stringstream input;
+  input << "errortest";
+  
+  std::stringstream buffer;
+
+  // save cerr's buffer here
+  std::streambuf* sbuf = std::cerr.rdbuf();
+
+  // redirect cerr to the buffer
+  std::cerr.rdbuf(buffer.rdbuf());
+
+  NFD_LOG_INIT("ErrorTest");
+  NFD_LOG_ERROR(input);
+  
+  // redirect cerr back
+  std::cerr.rdbuf(sbuf);
+  
+  std::stringstream trueValue;
+  trueValue << "ERROR: [ErrorTest] " << input <<"\n";
+  
+  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+}
+
+BOOST_AUTO_TEST_CASE(Trace)
+{
+  std::stringstream input;
+  input << "tracetest";
+  
+  std::stringstream buffer;
+
+  // save cerr's buffer here
+  std::streambuf* sbuf = std::cerr.rdbuf();
+
+  // redirect cerr to the buffer
+  std::cerr.rdbuf(buffer.rdbuf());
+
+  NFD_LOG_INIT("TraceTest");
+  NFD_LOG_TRACE(input);
+  
+  // redirect cerr back
+  std::cerr.rdbuf(sbuf);
+  
+  std::stringstream trueValue;
+  trueValue << "TRACE: [TraceTest] " << input <<"\n";
+  
+  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace nfd