Make logger portable
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 21 Nov 2019 09:23:55 +0000 (18:23 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Tue, 26 Nov 2019 08:24:15 +0000 (17:24 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/CMakeLists.txt
src/vist/common/CMakeLists.txt
src/vist/common/common.cpp
src/vist/logger.hpp
src/vist/logger/CMakeLists.txt [new file with mode: 0644]
src/vist/logger/dlog.cpp [new file with mode: 0644]
src/vist/logger/dlog.hpp [new file with mode: 0644]
src/vist/logger/logger.cpp [moved from src/vist/common/tests/logger.cpp with 69% similarity]
src/vist/logger/tests/logger.cpp [new file with mode: 0644]

index e4c7c9c..63162e1 100644 (file)
@@ -22,25 +22,37 @@ SET(${TARGET_VIST_COMMON_LIB}_SRCS "")
 
 SET(DEPENDENCY klay dlog gflags)
 
-PKG_CHECK_MODULES(VIST_DEPS REQUIRED ${DEPENDENCY})
+PKG_CHECK_MODULES(VIST_COMMON_DEPS REQUIRED ${DEPENDENCY})
 
-INCLUDE_DIRECTORIES(SYSTEM . common ${VIST_DEPS_INCLUDE_DIRS})
+INCLUDE_DIRECTORIES(SYSTEM . common ${VIST_COMMON_DEPS_INCLUDE_DIRS})
 
 ADD_DEFINITIONS(-DDB_PATH="${DB_INSTALL_DIR}/.vist.db"
                                -DDEFAULT_ADMIN_PATH="${DEFAULT_ADMIN_PATH}"
                                -DPLUGIN_INSTALL_DIR="${PLUGIN_INSTALL_DIR}"
                                -DSCRIPT_INSTALL_DIR="${SCRIPT_INSTALL_DIR}")
 
-ADD_SUBDIRECTORY(client)
+# common
 ADD_SUBDIRECTORY(common)
 ADD_SUBDIRECTORY(ipc)
-ADD_SUBDIRECTORY(notification)
-ADD_SUBDIRECTORY(policy)
+ADD_SUBDIRECTORY(logger)
 ADD_SUBDIRECTORY(sdk)
+
+# policy
+ADD_SUBDIRECTORY(policy)
+
+# client
+ADD_SUBDIRECTORY(client)
+
+# service
 ADD_SUBDIRECTORY(service)
+ADD_SUBDIRECTORY(notification)
+
+ADD_LIBRARY(${TARGET_VIST_COMMON_LIB} STATIC ${${TARGET_VIST_COMMON_LIB}_SRCS})
+TARGET_LINK_LIBRARIES(${TARGET_VIST_COMMON_LIB} ${VIST_COMMON_DEPS_LIBRARIES}
+                                                                                               pthread)
 
 ADD_LIBRARY(${TARGET_VIST_LIB} STATIC ${${TARGET_VIST_LIB}_SRCS})
-TARGET_LINK_LIBRARIES(${TARGET_VIST_LIB} ${VIST_DEPS_LIBRARIES}
+TARGET_LINK_LIBRARIES(${TARGET_VIST_LIB} ${TARGET_VIST_COMMON_LIB}
                                                                                 ${TARGET_VIST_POLICY_LIB}
                                                                                 ${TARGET_OSQUERY_LIB})
 
index 240e1e9..883dfc3 100644 (file)
 #  See the License for the specific language governing permissions and
 #  limitations under the License
 
-PKG_CHECK_MODULES(VIST_COMMON_DEPS REQUIRED gflags klay dlog)
-
-INCLUDE_DIRECTORIES(SYSTEM . ${VIST_COMMON_DEPS_INCLUDE_DIRS})
-
 ADD_VIST_COMMON_LIBRARY(vist_common archive.cpp
                                                                        common.cpp
                                                                        stringfy.cpp)
 
 FILE(GLOB COMMON_TESTS "tests/*.cpp")
 ADD_VIST_TEST(${COMMON_TESTS})
-
-ADD_LIBRARY(${TARGET_VIST_COMMON_LIB} STATIC ${${TARGET_VIST_COMMON_LIB}_SRCS})
-TARGET_LINK_LIBRARIES(${TARGET_VIST_COMMON_LIB} ${VIST_COMMON_DEPS_LIBRARIES}
-                                                                                               pthread)
index 14ffe17..3ff1007 100644 (file)
@@ -15,5 +15,4 @@
  */
 
 #include <vist/exception.hpp>
-#include <vist/logger.hpp>
 #include <vist/result.hpp>
index 28ffe8b..d4fdb0c 100644 (file)
  *  limitations under the License
  */
 /*
- * @brief Logging macro for dlog.
+ * @brief Header Only Library for logging.
  * @usage
  *  boilerplate : ${LEVEL}(${TAG}) << ${MESSAGE_STREAM}
  *
- *  INFO(VIST) << "Info message" << 1;
- *  DEBUG(VIST) << "Debug message" << 2 << 'a';
+ *  INFO("vist") << "Info message" << 1;
+ *  DEBUG("vist") << "Debug message" << 2 << 'a';
  *  WARN(VIST) << "Warning message" << 3 << 'b' << true;
  *  ERROR(VIST) << "Error message" << 4 << 'c' << false << 0.0f;
  */
@@ -30,8 +30,7 @@
 #include <memory>
 #include <sstream>
 #include <string>
-
-#include <dlog.h>
+#include <iostream>
 
 namespace vist {
 
@@ -51,6 +50,74 @@ struct LogRecord {
        std::string message;
 };
 
+struct LogBackend {
+       LogBackend() = default;
+       virtual ~LogBackend() = default;
+
+       LogBackend(const LogBackend&) = delete;
+       LogBackend& operator=(const LogBackend&) = delete;
+
+       LogBackend(LogBackend&&) = default;
+       LogBackend& operator=(LogBackend&&) = default;
+
+       virtual void info(const LogRecord& record) const noexcept = 0;
+       virtual void debug(const LogRecord& record) const noexcept = 0;
+       virtual void warn(const LogRecord& record) const noexcept = 0;
+       virtual void error(const LogRecord& record) const noexcept = 0;
+};
+
+struct Console final : public LogBackend {
+       enum class ColorCode {
+               Black = 30,
+               Red = 31,
+               Green = 32,
+               Yellow = 33,
+               Blue = 34,
+               Magenta = 35,
+               Cyan = 36,
+               White = 37,
+               Default = 39
+       };
+
+       struct Colorize {
+               explicit Colorize(ColorCode code = ColorCode::Default) : code(code) {}
+
+               friend std::ostream& operator<<(std::ostream& os, const Colorize& c)
+               {
+                       return os << "\033[" << static_cast<int>(c.code) << "m";
+               }
+
+               ColorCode code;
+       };
+
+       void info(const LogRecord& record) const noexcept override
+       {
+               std::cout << Colorize(ColorCode::Green);
+               std::cout << "[I][" << record.tag << "]" << record.message << std::endl;
+               std::cout << Colorize(ColorCode::Default);
+       }
+
+       void debug(const LogRecord& record) const noexcept override
+       {
+               std::cout << Colorize(ColorCode::Default);
+               std::cout << "[D][" << record.tag << "]" << record.message << std::endl;
+       }
+
+       void warn(const LogRecord& record) const noexcept override
+       {
+               std::cout << Colorize(ColorCode::Magenta);
+               std::cout << "[W][" << record.tag << "]" << record.message << std::endl;
+               std::cout << Colorize(ColorCode::Default);
+       }
+
+       void error(const LogRecord& record) const noexcept override
+       {
+               std::cout << Colorize(ColorCode::Red);
+               std::cout << "[E][" << record.tag << "]" << record.message << std::endl;
+               std::cout << Colorize(ColorCode::Default);
+       }
+};
+
 class LogStream final {
 public :
        LogStream(LogRecord record) noexcept : record(std::move(record)) {}
@@ -77,21 +144,31 @@ public :
                return *this;
        }
 
+       static std::shared_ptr<LogBackend> Init(std::shared_ptr<LogBackend>&& backend = nullptr)
+       {
+               static std::shared_ptr<LogBackend> base = std::make_shared<Console>();
+               if (backend != nullptr)
+                       base = backend;
+
+               return base;
+       }
+
 private:
        inline void log() const noexcept
        {
+               auto backend = Init();
                switch (record.level) {
                case LogLevel::Info:
-                       SLOG(LOG_INFO, record.tag.c_str(), "%s", record.message.c_str());
+                       backend->info(this->record);
                        return;
                case LogLevel::Debug:
-                       SLOG(LOG_DEBUG, record.tag.c_str(), "%s", record.message.c_str());
+                       backend->debug(this->record);
                        return;
                case LogLevel::Warn:
-                       SLOG(LOG_WARN, record.tag.c_str(), "%s", record.message.c_str());
+                       backend->warn(this->record);
                        return;
                case LogLevel::Error:
-                       SLOG(LOG_ERROR, record.tag.c_str(), "%s", record.message.c_str());
+                       backend->error(this->record);
                        return;
                }
        }
diff --git a/src/vist/logger/CMakeLists.txt b/src/vist/logger/CMakeLists.txt
new file mode 100644 (file)
index 0000000..efab7bb
--- /dev/null
@@ -0,0 +1,19 @@
+#  Copyright (c) 2019 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
+
+ADD_VIST_COMMON_LIBRARY(vist_logger dlog.cpp
+                                                                       logger.cpp)
+
+FILE(GLOB LOGGER_TESTS "tests/*.cpp")
+ADD_VIST_TEST(${LOGGER_TESTS})
diff --git a/src/vist/logger/dlog.cpp b/src/vist/logger/dlog.cpp
new file mode 100644 (file)
index 0000000..05670ae
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  Copyright (c) 2019 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 <vist/logger/dlog.hpp>
+
+#include <dlog.h>
+
+#include <memory>
+
+namespace vist {
+
+/// Make logger backend as Dlog.
+std::shared_ptr<LogBackend> Dlog::backend = LogStream::Init(std::make_shared<Dlog>());
+
+void Dlog::info(const LogRecord& record) const noexcept
+{
+       SLOG(LOG_INFO, record.tag.c_str(), "%s", record.message.c_str());
+}
+
+void Dlog::debug(const LogRecord& record) const noexcept
+{
+       SLOG(LOG_DEBUG, record.tag.c_str(), "%s", record.message.c_str());
+}
+
+void Dlog::warn(const LogRecord& record) const noexcept
+{
+       SLOG(LOG_WARN, record.tag.c_str(), "%s", record.message.c_str());
+}
+
+void Dlog::error(const LogRecord& record) const noexcept
+{
+       SLOG(LOG_ERROR, record.tag.c_str(), "%s", record.message.c_str());
+}
+
+} // namespace vist
diff --git a/src/vist/logger/dlog.hpp b/src/vist/logger/dlog.hpp
new file mode 100644 (file)
index 0000000..235600b
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ *  Copyright (c) 2019 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 <vist/logger.hpp>
+
+#include <memory>
+
+namespace vist {
+
+class Dlog final : public LogBackend {
+public:
+       void info(const LogRecord& record) const noexcept override;
+       void debug(const LogRecord& record) const noexcept override;
+       void warn(const LogRecord& record) const noexcept override;
+       void error(const LogRecord& record) const noexcept override;
+
+private:
+       static std::shared_ptr<LogBackend> backend;
+};
+
+} // namespace vist
similarity index 69%
rename from src/vist/common/tests/logger.cpp
rename to src/vist/logger/logger.cpp
index 78336ec..5f9d912 100644 (file)
  *  limitations under the License
  */
 
-#include <gtest/gtest.h>
-
 #include <vist/logger.hpp>
-
-class LoggerTests : public testing::Test {};
-
-TEST_F(LoggerTests, logging)
-{
-       INFO(VIST) << "Info message" << 1;
-       DEBUG(VIST) << "Debug message" << 2 << 'a';
-       WARN(VIST) << "Warn message" << 3 << 'b' << true;
-       ERROR(VIST) << "Error message" << 4 << 'c' << false << 0.0f;
-}
diff --git a/src/vist/logger/tests/logger.cpp b/src/vist/logger/tests/logger.cpp
new file mode 100644 (file)
index 0000000..15c98c8
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  Copyright (c) 2019 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 <gtest/gtest.h>
+
+#include <vist/logger.hpp>
+#include <vist/logger/dlog.hpp>
+
+using namespace vist;
+
+TEST(LoggerTests, default_)
+{
+       INFO(VIST) << "Info message" << 1;
+       DEBUG(VIST) << "Debug message" << 2 << 'a';
+       WARN(VIST) << "Warn message" << 3 << 'b' << true;
+       ERROR(VIST) << "Error message" << 4 << 'c' << false << 0.0f;
+}
+
+TEST(LoggerTests, console)
+{
+       LogStream::Init(std::make_shared<Console>());
+       INFO(VIST) << "Info message" << 1;
+       DEBUG(VIST) << "Debug message" << 2 << 'a';
+       WARN(VIST) << "Warn message" << 3 << 'b' << true;
+       ERROR(VIST) << "Error message" << 4 << 'c' << false << 0.0f;
+}
+
+TEST(LoggerTests, dlog)
+{
+       LogStream::Init(std::make_shared<Dlog>());
+       INFO(VIST) << "Info message" << 1;
+       DEBUG(VIST) << "Debug message" << 2 << 'a';
+       WARN(VIST) << "Warn message" << 3 << 'b' << true;
+       ERROR(VIST) << "Error message" << 4 << 'c' << false << 0.0f;
+}