Add dlog backend to logger system 25/93725/6
authorsangwan.kwon <sangwan.kwon@samsung.com>
Tue, 25 Oct 2016 11:37:24 +0000 (20:37 +0900)
committersangwan.kwon <sangwan.kwon@samsung.com>
Fri, 28 Oct 2016 06:31:18 +0000 (15:31 +0900)
Change-Id: I5412fe14dcbb7a89ed28c7c68faed930f16de08c
Signed-off-by: sangwan.kwon <sangwan.kwon@samsung.com>
CMakeLists.txt
include/klay/audit/dlog-sink.h [new file with mode: 0644]
include/klay/audit/logger.h
packaging/klay.spec
src/CMakeLists.txt
src/audit/dlog-sink.cpp [new file with mode: 0644]
src/audit/logger.cpp
test/logger.cpp

index b6afa86f195a46e91d59fe579dd04e858c7aa75d..760010ca9370ab719a0c85b5274d4c0ee64fae90 100755 (executable)
@@ -43,8 +43,8 @@ SET(CMAKE_C_FLAGS_PROFILING   "${COMPILE_BASE_FLAGS} -O0 -pg")
 SET(CMAKE_CXX_FLAGS_PROFILING  "${COMPILE_BASE_FLAGS} -O0 -pg -std=${CXX_STD} -fno-rtti")
 SET(CMAKE_C_FLAGS_DEBUG                "${COMPILE_BASE_FLAGS} -O0 -ggdb")
 SET(CMAKE_CXX_FLAGS_DEBUG      "${COMPILE_BASE_FLAGS} -O0 -ggdb -std=${CXX_STD} -fno-rtti")
-SET(CMAKE_C_FLAGS_RELEASE      "${COMPILE_BASE_FLAGS} -O2 -DNDEBUG")
-SET(CMAKE_CXX_FLAGS_RELEASE    "${COMPILE_BASE_FLAGS} -O2 -DNDEBUG -std=${CXX_STD} -fno-rtti")
+SET(CMAKE_C_FLAGS_RELEASE      "${COMPILE_BASE_FLAGS} -O2 ")
+SET(CMAKE_CXX_FLAGS_RELEASE    "${COMPILE_BASE_FLAGS} -O2 -std=${CXX_STD} -fno-rtti")
 SET(CMAKE_C_FLAGS_CCOV         "${COMPILE_BASE_FLAGS} -O0 --coverage")
 SET(CMAKE_CXX_FLAGS_CCOV       "${COMPILE_BASE_FLAGS} -O0 --coverage -std=${CXX_STD} -fno-rtti")
 
diff --git a/include/klay/audit/dlog-sink.h b/include/klay/audit/dlog-sink.h
new file mode 100644 (file)
index 0000000..d884ead
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __AUDIT_DLOG_LOGSINK_H__
+#define __AUDIT_DLOG_LOGSINK_H__
+
+#include <string>
+
+#include "logsink.h"
+
+namespace audit {
+
+class DlogLogSink : public LogSink {
+public:
+       void sink(const std::string& message) override;
+};
+
+} // namespace audit
+#endif //__AUDIT_DLOG_LOGSINK_H__
index 209aa69ce58de02eb47072f63e90609f1ef140c2..3104373a84bc37cb78a3a40e5b77c45b997ea9ec 100644 (file)
@@ -39,6 +39,9 @@ class Logger {
 public:
        static void setLogLevel(const LogLevel level);
        static LogLevel getLogLevel(void);
+       static void setTag(const std::string& tag);
+       static std::string getTag(void);
+       static void setBackend(LogSink *logSink);
        static void log(LogLevel severity,
                                        const std::string& file,
                                        const unsigned int line,
@@ -46,7 +49,10 @@ public:
                                        const std::string& message);
 
 private:
+       static LogSink *getBackend(void);
+
        static LogLevel logLevel;
+       static std::unique_ptr<std::string> tag;
        static std::unique_ptr<LogSink> backend;
 };
 
@@ -55,11 +61,15 @@ private:
 (::strrchr(__FILE__, '/') ? ::strrchr(__FILE__, '/') + 1 : __FILE__)
 #endif
 
+#define FORMAT(ITEMS)                                                  \
+(static_cast<std::ostringstream &>(std::ostringstream() << ITEMS)).str()
+
 #define LOG(SEVERITY, MESSAGE)                                         \
 do {                                                                   \
        if (audit::LogLevel::SEVERITY <= audit::Logger::getLogLevel()) {   \
                audit::Logger::log(audit::LogLevel::SEVERITY,                  \
-                                                  __FILENAME__, __LINE__, __func__, MESSAGE); \
+                                                  __FILENAME__, __LINE__, __func__,           \
+                                                  FORMAT(MESSAGE));                           \
        }                                                                  \
 } while (0)
 
@@ -77,6 +87,7 @@ do {                                                                   \
 #endif //NDEBUG
 
 std::string LogLevelToString(const LogLevel level);
+LogLevel StringToLogLevel(const std::string& level);
 
 } // namespace audit
 #endif //__AUDIT_LOGGER_H__
index 0c3fba01b7b3f602fd4d651a7e1689726ff3eb1e..381c567beb442cde0ac410ce5b53c6e4f4ad9e65 100755 (executable)
@@ -10,6 +10,7 @@ BuildRequires: gcc
 BuildRequires: cmake
 BuildRequires: pkgconfig(glib-2.0)
 BuildRequires: pkgconfig(sqlite3)
+BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(libxml-2.0)
 BuildRequires: pkgconfig(libtzplatform-config)
 
index c67dc428329b1fa3a7d929725f94db1194e77c42..a9f6ad21986fd24a1aaabb3a2ea4fc3257692131 100755 (executable)
@@ -43,6 +43,7 @@ SET (KLAY_SOURCES             ${KLAY_SRC}/error.cpp
                                                ${KLAY_SRC}/audit/logger.cpp
                                                ${KLAY_SRC}/audit/null-sink.cpp
                                                ${KLAY_SRC}/audit/console-sink.cpp
+                                               ${KLAY_SRC}/audit/dlog-sink.cpp
 )
 
 SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,noexecstack")
@@ -52,6 +53,7 @@ ADD_LIBRARY(${PROJECT_NAME} STATIC ${KLAY_SOURCES})
 PKG_CHECK_MODULES(KLAY_DEPS REQUIRED   gio-2.0
                                                                                libxml-2.0
                                                                                sqlite3
+                                                                               dlog
 )
 
 INCLUDE_DIRECTORIES(SYSTEM     ${KLAY_INCLUDE}
diff --git a/src/audit/dlog-sink.cpp b/src/audit/dlog-sink.cpp
new file mode 100644 (file)
index 0000000..55d714d
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#include <iostream>
+
+#include <dlog.h>
+
+#include <klay/audit/dlog-sink.h>
+#include <klay/audit/logger.h>
+
+namespace audit {
+
+void DlogLogSink::sink(const std::string &message)
+{
+       auto level = message.substr(0, message.find('<'));
+       auto subMsg = message.substr(message.find(':') + 1);
+       auto tag = Logger::getTag();
+
+       switch (StringToLogLevel(level)) {
+       case LogLevel::Error:
+               SLOG(LOG_ERROR, tag.c_str(), "%s", subMsg.c_str());
+               return;
+       case LogLevel::Warning:
+               SLOG(LOG_WARN, tag.c_str(), "%s", subMsg.c_str());
+               return;
+       case LogLevel::Debug:
+               SLOG(LOG_DEBUG, tag.c_str(), "%s", subMsg.c_str());
+               return;
+       case LogLevel::Info:
+               SLOG(LOG_INFO, tag.c_str(), "%s", subMsg.c_str());
+               return;
+       case LogLevel::Trace:
+               SLOG(LOG_VERBOSE, tag.c_str(), "%s", subMsg.c_str());
+               return;
+       default:
+               SLOG(LOG_SILENT, tag.c_str(), "%s", subMsg.c_str());
+               return;
+       }
+}
+
+} // namespace audit
index 092edf4133686356de6a574b7092c0727def6f0c..b0619d3959f32f8b6c9219ca6c7ec0717f30211f 100644 (file)
 namespace audit {
 
 LogLevel Logger::logLevel = LogLevel::Trace;
-std::unique_ptr<LogSink> Logger::backend(new ConsoleLogSink());
+std::unique_ptr<std::string> Logger::tag([]() {
+       auto tag = Logger::getTag();
+       return tag.empty() ? new std::string("KLAY") : new std::string(tag);
+}());
+
+std::unique_ptr<LogSink> Logger::backend([]() {
+       auto *backend = Logger::getBackend();
+       return backend != nullptr ? std::move(backend) : new ConsoleLogSink();
+}());
 
 std::string LogLevelToString(const LogLevel level)
 {
@@ -44,6 +52,20 @@ std::string LogLevelToString(const LogLevel level)
        }
 }
 
+LogLevel StringToLogLevel(const std::string& level)
+{
+       if (level == "ERROR")
+               return LogLevel::Error;
+       else if (level == "WARN")
+               return LogLevel::Warning;
+       else if (level == "DEBUG")
+               return LogLevel::Debug;
+       else if (level == "INFO")
+               return LogLevel::Info;
+       else
+               return LogLevel::Trace;
+}
+
 void Logger::setLogLevel(const LogLevel level)
 {
        Logger::logLevel = level;
@@ -54,6 +76,31 @@ LogLevel Logger::getLogLevel(void)
        return Logger::logLevel;
 }
 
+void Logger::setTag(const std::string& tag)
+{
+       Logger::tag.reset(new std::string(tag));
+}
+
+std::string Logger::getTag(void)
+{
+       auto *pTag = Logger::tag.get();
+       return (pTag != nullptr) ? *pTag : std::string();
+}
+
+void Logger::setBackend(LogSink *logSink)
+{
+       if (logSink == nullptr)
+               return;
+
+       Logger::backend.reset(logSink);
+}
+
+LogSink *Logger::getBackend(void)
+{
+       auto *pBackend = Logger::backend.get();
+       return (pBackend != nullptr) ? pBackend : nullptr;
+}
+
 void Logger::log(LogLevel severity,
                                 const std::string& file,
                                 const unsigned int line,
@@ -65,7 +112,7 @@ void Logger::log(LogLevel severity,
        buffer << LogLevelToString(severity)
                   << "<" << ::getpid() << ">:"
                   << file << ":" << line
-                  << " " << func << " " << message
+                  << ", " << func << "() > " << message
                   << std::endl;
 
        Logger::backend->sink(buffer.str());
index 79646ccd96e2f1c9b8b53f1985a94d208ad28b13..9641dcb7ab7b560b92272c6f7b868d8efb99dea6 100644 (file)
@@ -20,6 +20,9 @@
 #include <klay/error.h>
 #include <klay/exception.h>
 #include <klay/audit/logger.h>
+#include <klay/audit/console-sink.h>
+#include <klay/audit/dlog-sink.h>
+#include <klay/audit/null-sink.h>
 
 #include <klay/testbench.h>
 
@@ -39,3 +42,25 @@ TESTCASE(LogSeverityTest)
        } catch (runtime::Exception& e) {
        }
 }
+
+TESTCASE(BackendTest)
+{
+       audit::Logger::setBackend(new audit::DlogLogSink());
+       audit::Logger::setTag("KLAY");
+       INFO("Dlog Test : " << "Info");
+       DEBUG("Dlog Test : " << "Debug");
+       WARN("Dlog Test : " << "Warning");
+       ERROR("Dlog Test : " << "Error");
+
+       audit::Logger::setBackend(new audit::ConsoleLogSink());
+       INFO("Console Test : " << "Info");
+       DEBUG("Console Test : " << "Debug");
+       WARN("Console Test : " << "Warning");
+       ERROR("Console Test : " << "Error");
+
+       audit::Logger::setBackend(new audit::NullLogSink());
+       INFO("Null Test : " << "Info");
+       DEBUG("Null Test : " << "Debug");
+       WARN("Null Test : " << "Warning");
+       ERROR("Null Test : " << "Error");
+}