From 6dc52f1304e50de8515dd7af2d729ab1c713c4bd Mon Sep 17 00:00:00 2001 From: "sangwan.kwon" Date: Mon, 17 Jul 2017 17:23:27 +0900 Subject: [PATCH] Fix logger usage according to KLAY API changed Change-Id: I60fb8056ee8866eef6d17a95dcc5d84ba7b4a5ef Signed-off-by: sangwan.kwon --- src/exception.cpp | 16 +++++++++------- src/exception.hxx | 2 +- src/init-lib.cpp | 12 ++++++++---- src/logger.hxx | 30 ++++++++++++++++++++++++++++++ src/logic.cpp | 36 +++++++++++++++++++----------------- src/trust-anchor.cpp | 26 +++++++++++++------------- tests/CMakeLists.txt | 1 + tests/main.cpp | 4 ---- 8 files changed, 81 insertions(+), 46 deletions(-) create mode 100644 src/logger.hxx diff --git a/src/exception.cpp b/src/exception.cpp index 9b35bac..5fee6af 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -23,6 +23,8 @@ #include +#include "logger.hxx" + namespace tanchor { int exceptionGuard(const std::function &func) @@ -46,22 +48,22 @@ int exceptionGuard(const std::function &func) errStr = "Internal error."; break; } - ERROR(errStr + e.what()); + ERROR(SINK, errStr + e.what()); return e.error(); } catch (const runtime::Exception &e) { - ERROR(e.what()); + ERROR(SINK, e.what()); return TRUST_ANCHOR_ERROR_INTERNAL; } catch (const std::invalid_argument &e) { - ERROR(e.what()); + ERROR(SINK, e.what()); return TRUST_ANCHOR_ERROR_INVALID_PARAMETER; } catch (const std::logic_error &e) { - ERROR(e.what()); + ERROR(SINK, e.what()); return TRUST_ANCHOR_ERROR_INTERNAL; } catch (const std::exception &e) { - ERROR(e.what()); + ERROR(SINK, e.what()); return TRUST_ANCHOR_ERROR_INTERNAL; } catch (...) { - ERROR("Unknown exception occurred."); + ERROR(SINK, "Unknown exception occurred."); return TRUST_ANCHOR_ERROR_INTERNAL; } } @@ -72,7 +74,7 @@ Exception::Exception(int ec, const char *file, const char *function, m_message(FORMAT("[" << file << ":" << line << " " << function << "()]" << message)) { - ERROR(this->m_message); + ERROR(SINK, this->m_message); } const char *Exception::what() const noexcept diff --git a/src/exception.hxx b/src/exception.hxx index 0a15371..8ed1031 100644 --- a/src/exception.hxx +++ b/src/exception.hxx @@ -27,7 +27,7 @@ #include #include -#include +#include "logger.hxx" #define EXCEPTION_GUARD_START return tanchor::exceptionGuard([&]() { #define EXCEPTION_GUARD_END }); diff --git a/src/init-lib.cpp b/src/init-lib.cpp index 8a7ec92..9fdc2cd 100644 --- a/src/init-lib.cpp +++ b/src/init-lib.cpp @@ -19,12 +19,12 @@ * @version 1.0 * @brief Init global configuration for library */ - -#include #include #include +#include "logger.hxx" + namespace tanchor { namespace { @@ -32,13 +32,17 @@ class InitLib { public: InitLib() { - audit::Logger::setBackend(new audit::DlogLogSink()); - audit::Logger::setTag("TRUST_ANCHOR"); + this->m_sink.reset(new audit::DlogLogSink("TRUST_ANCHOR")); }; ~InitLib() = default; + + std::unique_ptr m_sink = nullptr; }; static std::unique_ptr init(new(std::nothrow)(InitLib)); } // namespace anonymous + +audit::LogSink *SINK = dynamic_cast((init->m_sink).get()); + } // namespace tanchor diff --git a/src/logger.hxx b/src/logger.hxx new file mode 100644 index 0000000..9e77c59 --- /dev/null +++ b/src/logger.hxx @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017 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 + */ +/* + * @file logic.hxx + * @author Sangwan Kwon (sangwan.kwon@samsung.com) + * @version 1.0 + * @brief + */ +#pragma once + +#include + +namespace tanchor { + +extern audit::LogSink *SINK; + +} // namespace tanchor diff --git a/src/logic.cpp b/src/logic.cpp index c688911..741a6d5 100644 --- a/src/logic.cpp +++ b/src/logic.cpp @@ -30,8 +30,8 @@ #include #include -#include +#include "logger.hxx" #include "certificate.hxx" #include "file-system.hxx" #include "exception.hxx" @@ -50,7 +50,7 @@ void Logic::init(void) const { runtime::File customBaseDir(this->m_customBasePath); if (customBaseDir.exists()) { - WARN("Pkg custom directory is already exist. remove it!"); + WARN(SINK, "Pkg custom directory is already exist. remove it!"); customBaseDir.remove(true); } customBaseDir.makeDirectory(true); @@ -60,7 +60,7 @@ void Logic::init(void) const runtime::File customBundleDir(this->m_customBundlePath); customBundleDir.makeDirectory(); - DEBUG("Success to init[" << this->m_customBasePath << "]"); + DEBUG(SINK, "Success to init[" << this->m_customBasePath << "]"); } void Logic::deinit(bool isRollback) const @@ -71,7 +71,7 @@ void Logic::deinit(bool isRollback) const else if (!isRollback) throw std::invalid_argument("tanchor is never installed before."); - DEBUG("Success to deinit[" << this->m_customBasePath << "]"); + DEBUG(SINK, "Success to deinit[" << this->m_customBasePath << "]"); } void Logic::makeCustomCerts(void) @@ -86,7 +86,7 @@ void Logic::makeCustomCerts(void) this->m_customCertNameSet.emplace(File::getName(iter->getPath())); ++iter; } - DEBUG("Success to migrate system certificates."); + DEBUG(SINK, "Success to migrate system certificates."); } // link pkg certificates to the custom directory as subjectNameHash @@ -100,6 +100,8 @@ void Logic::makeCustomCerts(void) this->m_customCertsData.emplace_back(cert.getCertificateData()); ++iter; } + + INFO(SINK, "Success to make pkg custom certs."); } void Logic::makeCustomBundle(void) @@ -107,11 +109,11 @@ void Logic::makeCustomBundle(void) runtime::File customBundle(this->m_customBundlePath + "/" + File::getName(path::SYS_BUNDLE_PATH)); if (customBundle.exists()) { - WARN("Pkg custom bundle is already exist. remove it!"); + WARN(SINK, "Pkg custom bundle is already exist. remove it!"); customBundle.remove(); } - DEBUG("Start to migrate previous bundle."); + DEBUG(SINK, "Start to migrate previous bundle."); if (this->isSystemCertsUsed()) { runtime::File sysBundle(path::SYS_BUNDLE_PATH); sysBundle.copyTo(this->m_customBundlePath); @@ -119,10 +121,10 @@ void Logic::makeCustomBundle(void) runtime::File tanchorBundle(path::TANCHOR_BUNDLE_PATH); tanchorBundle.copyTo(this->m_customBundlePath); } - DEBUG("Finish migrating previous bundle."); + DEBUG(SINK, "Finish migrating previous bundle."); if (this->m_customCertsData.empty()) { - DEBUG("System certificates is changed after TrustAnchor installation."); + DEBUG(SINK, "System certificates is changed after TrustAnchor installation."); runtime::DirectoryIterator iter(this->getPkgCertsPath()), end; while (iter != end) { Certificate cert(iter->getPath()); @@ -131,7 +133,7 @@ void Logic::makeCustomBundle(void) } } - DEBUG("Start to add pkg's certificate to bundle."); + DEBUG(SINK, "Start to add pkg's certificate to bundle."); customBundle.open(O_RDWR | O_APPEND); for (const auto &cert : this->m_customCertsData) { customBundle.write(cert.c_str(), cert.length()); @@ -139,7 +141,7 @@ void Logic::makeCustomBundle(void) customBundle.write(newLine.c_str(), newLine.length()); } - INFO("Success to make pkg custom bundle."); + INFO(SINK, "Success to make pkg custom bundle."); } bool Logic::isPkgCertsValid(const std::string &path) const @@ -173,14 +175,14 @@ void Logic::setPkgCertsPath(const std::string &path) const pkgCertsPath.write(path.c_str(), path.length()); pkgCertsPath.unlock(); - DEBUG("Success to set package certificates path : " << path); + DEBUG(SINK, "Success to set package certificates path : " << path); } std::string Logic::getPkgCertsPath(void) const { std::string path = File::read(this->m_customBasePath + "/" + File::getName(path::TANCHOR_PKG_CERTS_PATH)); - DEBUG("Package certificates path : " << path); + DEBUG(SINK, "Package certificates path : " << path); return path; } @@ -188,7 +190,7 @@ void Logic::setSystemCertsUsed(void) const { runtime::File tanchorSysCA(path::TANCHOR_SYSCA_PATH); tanchorSysCA.copyTo(this->m_customBasePath); - DEBUG("Success to set SYSCA flag."); + DEBUG(SINK, "Success to set SYSCA flag."); } bool Logic::isSystemCertsUsed(void) const @@ -196,7 +198,7 @@ bool Logic::isSystemCertsUsed(void) const runtime::File customSysCA(this->m_customBasePath + "/" + File::getName(path::TANCHOR_SYSCA_PATH)); - DEBUG("Wheter system certificates use or not : " << customSysCA.exists()); + DEBUG(SINK, "Wheter system certificates use or not : " << customSysCA.exists()); return customSysCA.exists(); } @@ -212,7 +214,7 @@ bool Logic::isSystemCertsModified(void) const if (::stat(customBundle.c_str(), &customAttr)) ThrowErrno(errno, customBundle); - DEBUG("System bundle mtime : " << ::ctime(&systemAttr.st_mtime) << ", " << + DEBUG(SINK, "System bundle mtime : " << ::ctime(&systemAttr.st_mtime) << ", " << "Custom bundle mtime : " << ::ctime(&customAttr.st_mtime)); return systemAttr.st_mtime > customAttr.st_mtime; @@ -227,7 +229,7 @@ void Logic::disassociateNS(void) const if (::mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL)) ThrowErrno(errno, "Failed to mount."); - DEBUG("Success to disassociate namespace."); + DEBUG(SINK, "Success to disassociate namespace."); } void Logic::mountCustomCerts(void) const diff --git a/src/trust-anchor.cpp b/src/trust-anchor.cpp index 4c1a154..05654b5 100644 --- a/src/trust-anchor.cpp +++ b/src/trust-anchor.cpp @@ -21,9 +21,8 @@ */ #include "tanchor/trust-anchor.hxx" -#include - #include "logic.hxx" +#include "logger.hxx" #include "exception.hxx" #include "environment.hxx" @@ -51,13 +50,14 @@ TrustAnchor::Impl::Impl(const std::string &packageId, uid_t uid) noexcept : std::to_string(static_cast(uid)) + "/" + packageId) { - INFO("Start tanchor about uid[" << uid << "], pkg[" << packageId << "]"); + INFO(SINK, "Start tanchor about uid[" << uid << + "], pkg[" << packageId << "]"); } void TrustAnchor::Impl::preInstall(void) { this->m_logic.init(); - DEBUG("Success to pre-install stage."); + DEBUG(SINK, "Success to pre-install stage."); } int TrustAnchor::Impl::install(const std::string &pkgCertsPath, @@ -74,7 +74,7 @@ int TrustAnchor::Impl::install(const std::string &pkgCertsPath, this->m_logic.makeCustomCerts(); this->m_logic.makeCustomBundle(); - INFO("Success to install."); + INFO(SINK, "Success to install."); return TRUST_ANCHOR_ERROR_NONE; EXCEPTION_GUARD_END @@ -86,7 +86,7 @@ int TrustAnchor::Impl::rollback(void) const noexcept this->m_logic.deinit(true); - INFO("Success to rollback."); + INFO(SINK, "Success to rollback."); return TRUST_ANCHOR_ERROR_NONE; EXCEPTION_GUARD_END @@ -98,7 +98,7 @@ int TrustAnchor::Impl::uninstall(void) const noexcept this->m_logic.deinit(false); - INFO("Success to uninstall."); + INFO(SINK, "Success to uninstall."); return TRUST_ANCHOR_ERROR_NONE; EXCEPTION_GUARD_END @@ -109,12 +109,12 @@ void TrustAnchor::Impl::preLaunch(void) if (!this->m_logic.isSystemCertsUsed()) return; - DEBUG("This package use system certificates."); + DEBUG(SINK, "This package use system certificates."); if (this->m_logic.isSystemCertsModified()) { - WARN("System certificates be changed. Do re-install for refresh."); + WARN(SINK, "System certificates be changed. Do re-install for refresh."); this->install(this->m_logic.getPkgCertsPath(), true); } - DEBUG("Success to pre-launch stage."); + DEBUG(SINK, "Success to pre-launch stage."); } int TrustAnchor::Impl::launch() noexcept @@ -127,7 +127,7 @@ int TrustAnchor::Impl::launch() noexcept this->m_logic.mountCustomCerts(); this->m_logic.mountCustomBundle(); - INFO("Success to launch."); + INFO(SINK, "Success to launch."); return TRUST_ANCHOR_ERROR_NONE; EXCEPTION_GUARD_END @@ -147,9 +147,9 @@ int TrustAnchor::install(const std::string &pkgCertsPath, int ret = this->m_pImpl->install(pkgCertsPath, withSystemCerts); if (ret != TRUST_ANCHOR_ERROR_NONE) { - ERROR("Failed to intall ACTA. Remove custom directory for rollback."); + ERROR(SINK, "Failed to intall ACTA. Remove custom directory for rollback."); if (this->m_pImpl->rollback() != TRUST_ANCHOR_ERROR_NONE) - ERROR("Failed to rollback ACTA."); + ERROR(SINK, "Failed to rollback ACTA."); } return ret; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c1db1df..42a2ce7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -58,6 +58,7 @@ SET(INTERNAL_SRCS main.cpp test-util.cpp test-certificate.cpp ${TANCHOR_SRC}/exception.cpp + ${TANCHOR_SRC}/init-lib.cpp ${TANCHOR_SRC}/file-system.cpp ${TANCHOR_SRC}/certificate.cpp) SET(SSL_SRCS main.cpp diff --git a/tests/main.cpp b/tests/main.cpp index 765b868..1eb6dd6 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -20,16 +20,12 @@ * @brief Implemetation of testbench driver */ -#include - #include #include int main(int /*argc*/, char** /*argv*/) { - audit::Logger::setLogLevel(audit::LogLevel::Trace); - curl_global_init(CURL_GLOBAL_DEFAULT); testbench::Testbench::runAllTestSuites(); -- 2.34.1