core: Logger extension to allow in-class-defined logger

Change-Id: Ifa31fb65eda772df47a81ad5db1c501eaa5f8ca1
diff --git a/daemon/core/logger.hpp b/daemon/core/logger.hpp
index e483ac9..6732384 100644
--- a/daemon/core/logger.hpp
+++ b/daemon/core/logger.hpp
@@ -50,10 +50,24 @@
 std::ostream&
 operator<<(std::ostream& output, const Logger& obj);
 
-#define NFD_LOG_INIT(name)    \
+#define NFD_LOG_INIT(name) \
   static nfd::Logger \
   g_logger = nfd::Logger(name);
 
+#define NFD_LOG_INCLASS_DECLARE()        \
+  static nfd::Logger g_logger;
+
+#define NFD_LOG_INCLASS_DEFINE(cls, name)        \
+  nfd::Logger cls::g_logger = nfd::Logger(name);
+
+#define NFD_LOG_INCLASS_TEMPLATE_DEFINE(cls, name)   \
+  template<class T> \
+  nfd::Logger cls<T>::g_logger = nfd::Logger(name);
+
+#define NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(cls, specialization, name)    \
+  template<> \
+  nfd::Logger cls<specialization>::g_logger = nfd::Logger(name);
+
 #define NFD_LOG_TRACE(expression) \
     if(g_logger.isEnabled(nfd::LOG_TRACE)) \
        std::cerr<<"TRACE: "<<"["<<g_logger<<"] " << expression << "\n"
diff --git a/tests/core/logger.cpp b/tests/core/logger.cpp
index 35fd66c..ca55c37 100644
--- a/tests/core/logger.cpp
+++ b/tests/core/logger.cpp
@@ -15,154 +15,127 @@
 
 BOOST_AUTO_TEST_SUITE(CoreLogger)
 
-BOOST_AUTO_TEST_CASE(Warn)
+struct LoggerFixture
 {
-  std::stringstream input;
-  input << "warnTest";
-  
-  std::stringstream buffer;
+  LoggerFixture()
+    : m_savedBuf(std::cerr.rdbuf())
+  {
+    std::cerr.rdbuf(m_buffer.rdbuf());
+  }
 
-  // save cerr's buffer here
-  std::streambuf* sbuf = std::cerr.rdbuf();
+  ~LoggerFixture()
+  {
+    std::cerr.rdbuf(m_savedBuf);
+  }
+  
+  std::stringstream m_buffer;
+  std::streambuf* m_savedBuf;
+};
 
-  // redirect cerr to the buffer
-  std::cerr.rdbuf(buffer.rdbuf());
-
-  NFD_LOG_INIT("WarnTest");
-  NFD_LOG_WARN(input);
+BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture)
+{
+  NFD_LOG_INIT("BasicTests");
   
-  // redirect cerr back
-  std::cerr.rdbuf(sbuf);
+  NFD_LOG_TRACE("trace message JHGFDSR^1");
+  NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
+  NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
+  NFD_LOG_INFO("info message Jjxjshj13"); 
+  NFD_LOG_ERROR("error message !#$&^%$#@");
+  NFD_LOG_FATAL("fatal message JJSjaamcng");
   
-  std::stringstream trueValue;
-  trueValue << "WARNING: [WarnTest] " << input <<"\n";
-  
-  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+  BOOST_CHECK_EQUAL(m_buffer.str(),
+                    "TRACE: [BasicTests] trace message JHGFDSR^1\n"
+                    "DEBUG: [BasicTests] debug message IGg2474fdksd fo 151617\n"
+                    "WARNING: [BasicTests] warning message XXXhdhd111x\n"
+                    "INFO: [BasicTests] info message Jjxjshj13\n"
+                    "ERROR: [BasicTests] error message !#$&^%$#@\n"
+                    "FATAL: [BasicTests] fatal message JJSjaamcng\n"
+                    );
 }
 
-BOOST_AUTO_TEST_CASE(Debug)
+class InClassLogger : public LoggerFixture
 {
-  std::stringstream input;
-  input << "debugTest";
-  
-  std::stringstream buffer;
+public:
+  void
+  writeLogs()
+  {
+    NFD_LOG_TRACE("trace message JHGFDSR^1");
+    NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
+    NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
+    NFD_LOG_INFO("info message Jjxjshj13"); 
+    NFD_LOG_ERROR("error message !#$&^%$#@");
+    NFD_LOG_FATAL("fatal message JJSjaamcng");
+  }
+    
+private:
+  NFD_LOG_INCLASS_DECLARE();
+};
 
-  // save cerr's buffer here
-  std::streambuf* sbuf = std::cerr.rdbuf();
+NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger");
 
-  // redirect cerr to the buffer
-  std::cerr.rdbuf(buffer.rdbuf());
-
-  NFD_LOG_INIT("DebugTest");
-  NFD_LOG_DEBUG(input);
+BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger)
+{
+  writeLogs();
   
-  // redirect cerr back
-  std::cerr.rdbuf(sbuf);
-  
-  std::stringstream trueValue;
-  trueValue << "DEBUG: [DebugTest] " << input <<"\n";
-  
-  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+  BOOST_CHECK_EQUAL(m_buffer.str(),
+                    "TRACE: [InClassLogger] trace message JHGFDSR^1\n"
+                    "DEBUG: [InClassLogger] debug message IGg2474fdksd fo 151617\n"
+                    "WARNING: [InClassLogger] warning message XXXhdhd111x\n"
+                    "INFO: [InClassLogger] info message Jjxjshj13\n"
+                    "ERROR: [InClassLogger] error message !#$&^%$#@\n"
+                    "FATAL: [InClassLogger] fatal message JJSjaamcng\n"
+                    );
 }
 
-BOOST_AUTO_TEST_CASE(Info)
+
+template<class T>
+class InClassTemplateLogger : public LoggerFixture
 {
-  std::stringstream input;
-  input << "infotest";
-  
-  std::stringstream buffer;
+public:
+  void
+  writeLogs()
+  {
+    NFD_LOG_TRACE("trace message JHGFDSR^1");
+    NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17);
+    NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x");
+    NFD_LOG_INFO("info message Jjxjshj13"); 
+    NFD_LOG_ERROR("error message !#$&^%$#@");
+    NFD_LOG_FATAL("fatal message JJSjaamcng");
+  }
+    
+private:
+  NFD_LOG_INCLASS_DECLARE();
+};
 
-  // save cerr's buffer here
-  std::streambuf* sbuf = std::cerr.rdbuf();
+NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger");
+NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger");
 
-  // redirect cerr to the buffer
-  std::cerr.rdbuf(buffer.rdbuf());
-
-  NFD_LOG_INIT("InfoTest");
-  NFD_LOG_INFO(input);
+BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>)
+{
+  writeLogs();
   
-  // redirect cerr back
-  std::cerr.rdbuf(sbuf);
-  
-  std::stringstream trueValue;
-  trueValue << "INFO: [InfoTest] " << input <<"\n";
-  
-  BOOST_CHECK_EQUAL(buffer.str(), trueValue.str());
+  BOOST_CHECK_EQUAL(m_buffer.str(),
+                    "TRACE: [GenericInClassTemplateLogger] trace message JHGFDSR^1\n"
+                    "DEBUG: [GenericInClassTemplateLogger] debug message IGg2474fdksd fo 151617\n"
+                    "WARNING: [GenericInClassTemplateLogger] warning message XXXhdhd111x\n"
+                    "INFO: [GenericInClassTemplateLogger] info message Jjxjshj13\n"
+                    "ERROR: [GenericInClassTemplateLogger] error message !#$&^%$#@\n"
+                    "FATAL: [GenericInClassTemplateLogger] fatal message JJSjaamcng\n"
+                    );
 }
 
-BOOST_AUTO_TEST_CASE(Fatal)
+BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>)
 {
-  std::stringstream input;
-  input << "fataltest";
+  writeLogs();
   
-  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_CHECK_EQUAL(m_buffer.str(),
+                    "TRACE: [IntInClassLogger] trace message JHGFDSR^1\n"
+                    "DEBUG: [IntInClassLogger] debug message IGg2474fdksd fo 151617\n"
+                    "WARNING: [IntInClassLogger] warning message XXXhdhd111x\n"
+                    "INFO: [IntInClassLogger] info message Jjxjshj13\n"
+                    "ERROR: [IntInClassLogger] error message !#$&^%$#@\n"
+                    "FATAL: [IntInClassLogger] fatal message JJSjaamcng\n"
+                    );
 }
 
 BOOST_AUTO_TEST_SUITE_END()