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})
# 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)
*/
#include <vist/exception.hpp>
-#include <vist/logger.hpp>
#include <vist/result.hpp>
* 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;
*/
#include <memory>
#include <sstream>
#include <string>
-
-#include <dlog.h>
+#include <iostream>
namespace vist {
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)) {}
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;
}
}
--- /dev/null
+# 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})
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
* 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;
-}
--- /dev/null
+/*
+ * 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;
+}