Fix logger usage according to KLAY API changed 97/139197/1 accepted/tizen/unified/20170727.190710 submit/tizen/20170726.025713
authorsangwan.kwon <sangwan.kwon@samsung.com>
Mon, 17 Jul 2017 08:23:27 +0000 (17:23 +0900)
committersangwan.kwon <sangwan.kwon@samsung.com>
Tue, 18 Jul 2017 02:24:03 +0000 (11:24 +0900)
Change-Id: I60fb8056ee8866eef6d17a95dcc5d84ba7b4a5ef
Signed-off-by: sangwan.kwon <sangwan.kwon@samsung.com>
src/exception.cpp
src/exception.hxx
src/init-lib.cpp
src/logger.hxx [new file with mode: 0644]
src/logic.cpp
src/trust-anchor.cpp
tests/CMakeLists.txt
tests/main.cpp

index 9b35bac94bf957107a7a8d1ae0b74d119c2e6065..5fee6afa4b7b1de5d7f60149087d4ef178122fc7 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <klay/exception.h>
 
+#include "logger.hxx"
+
 namespace tanchor {
 
 int exceptionGuard(const std::function<int()> &func)
@@ -46,22 +48,22 @@ int exceptionGuard(const std::function<int()> &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
index 0a15371f27537670aa566b80e706d5c7d3aa9628..8ed103180b4cd86c6305a184b4308afd6e3d45fb 100644 (file)
@@ -27,7 +27,7 @@
 #include <string>
 #include <exception>
 
-#include <klay/audit/logger.h>
+#include "logger.hxx"
 
 #define EXCEPTION_GUARD_START return tanchor::exceptionGuard([&]() {
 #define EXCEPTION_GUARD_END   });
index 8a7ec9227390a267321a08836b9d4777f202fac4..9fdc2cdd78f1f11e7d997bba8f16e85e24299596 100644 (file)
  * @version     1.0
  * @brief       Init global configuration for library
  */
-
-#include <klay/audit/logger.h>
 #include <klay/audit/dlog-sink.h>
 
 #include <memory>
 
+#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<audit::DlogLogSink> m_sink = nullptr;
 };
 
 static std::unique_ptr<InitLib> init(new(std::nothrow)(InitLib));
 
 } // namespace anonymous
+
+audit::LogSink *SINK = dynamic_cast<audit::LogSink*>((init->m_sink).get());
+
 } // namespace tanchor
diff --git a/src/logger.hxx b/src/logger.hxx
new file mode 100644 (file)
index 0000000..9e77c59
--- /dev/null
@@ -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 <klay/audit/logger.h>
+
+namespace tanchor {
+
+extern audit::LogSink *SINK;
+
+} // namespace tanchor
index c6889115129299551f4ffc3ab58099bc9dfdf1e9..741a6d52f75f721b507c433fec9240a37b16eddc 100644 (file)
@@ -30,8 +30,8 @@
 #include <sys/stat.h>
 
 #include <klay/filesystem.h>
-#include <klay/audit/logger.h>
 
+#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
index 4c1a1542a58749218e202fe2fb2582d80225a1be..05654b5cc2580b33362933be9f4e65760ee8bbd2 100644 (file)
@@ -21,9 +21,8 @@
  */
 #include "tanchor/trust-anchor.hxx"
 
-#include <klay/audit/logger.h>
-
 #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<int>(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;
index c1db1df6996dad074bf05e9883ffcfae4d5af6fa..42a2ce777cffb807df90ad1ab6aca33ce23ccf8c 100644 (file)
@@ -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
index 765b8680f214127df1456462d354a6898200d9ea..1eb6dd6a78c9ed0e674f83040c2b848806c76dbb 100644 (file)
  * @brief       Implemetation of testbench driver
  */
 
-#include <klay/audit/logger.h>
-
 #include <klay/testbench.h>
 
 #include <curl/curl.h>
 
 int main(int /*argc*/, char** /*argv*/)
 {
-       audit::Logger::setLogLevel(audit::LogLevel::Trace);
-
        curl_global_init(CURL_GLOBAL_DEFAULT);
 
        testbench::Testbench::runAllTestSuites();