From 582f985af33c4ff72ff2cd9344e1d4e34d50e73c Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Thu, 21 Nov 2019 18:23:55 +0900 Subject: [PATCH] Make logger portable Signed-off-by: Sangwan Kwon --- src/vist/CMakeLists.txt | 24 +++++-- src/vist/common/CMakeLists.txt | 8 --- src/vist/common/common.cpp | 1 - src/vist/logger.hpp | 95 +++++++++++++++++++++++++--- src/vist/logger/CMakeLists.txt | 19 ++++++ src/vist/logger/dlog.cpp | 48 ++++++++++++++ src/vist/logger/dlog.hpp | 34 ++++++++++ src/vist/{common/tests => logger}/logger.cpp | 12 ---- src/vist/logger/tests/logger.cpp | 48 ++++++++++++++ 9 files changed, 253 insertions(+), 36 deletions(-) create mode 100644 src/vist/logger/CMakeLists.txt create mode 100644 src/vist/logger/dlog.cpp create mode 100644 src/vist/logger/dlog.hpp rename src/vist/{common/tests => logger}/logger.cpp (69%) create mode 100644 src/vist/logger/tests/logger.cpp diff --git a/src/vist/CMakeLists.txt b/src/vist/CMakeLists.txt index e4c7c9c..63162e1 100644 --- a/src/vist/CMakeLists.txt +++ b/src/vist/CMakeLists.txt @@ -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}) diff --git a/src/vist/common/CMakeLists.txt b/src/vist/common/CMakeLists.txt index 240e1e9..883dfc3 100644 --- a/src/vist/common/CMakeLists.txt +++ b/src/vist/common/CMakeLists.txt @@ -12,17 +12,9 @@ # 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) diff --git a/src/vist/common/common.cpp b/src/vist/common/common.cpp index 14ffe17..3ff1007 100644 --- a/src/vist/common/common.cpp +++ b/src/vist/common/common.cpp @@ -15,5 +15,4 @@ */ #include -#include #include diff --git a/src/vist/logger.hpp b/src/vist/logger.hpp index 28ffe8b..d4fdb0c 100644 --- a/src/vist/logger.hpp +++ b/src/vist/logger.hpp @@ -14,12 +14,12 @@ * 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 #include #include - -#include +#include 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(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 Init(std::shared_ptr&& backend = nullptr) + { + static std::shared_ptr base = std::make_shared(); + 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 index 0000000..efab7bb --- /dev/null +++ b/src/vist/logger/CMakeLists.txt @@ -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 index 0000000..05670ae --- /dev/null +++ b/src/vist/logger/dlog.cpp @@ -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 + +#include + +#include + +namespace vist { + +/// Make logger backend as Dlog. +std::shared_ptr Dlog::backend = LogStream::Init(std::make_shared()); + +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 index 0000000..235600b --- /dev/null +++ b/src/vist/logger/dlog.hpp @@ -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 + +#include + +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 backend; +}; + +} // namespace vist diff --git a/src/vist/common/tests/logger.cpp b/src/vist/logger/logger.cpp similarity index 69% rename from src/vist/common/tests/logger.cpp rename to src/vist/logger/logger.cpp index 78336ec..5f9d912 100644 --- a/src/vist/common/tests/logger.cpp +++ b/src/vist/logger/logger.cpp @@ -14,16 +14,4 @@ * limitations under the License */ -#include - #include - -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 index 0000000..15c98c8 --- /dev/null +++ b/src/vist/logger/tests/logger.cpp @@ -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 + +#include +#include + +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()); + 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()); + 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; +} -- 2.7.4