Add HistoryLogger class 68/251068/8
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 7 Jan 2021 10:34:59 +0000 (19:34 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Wed, 14 Apr 2021 08:33:22 +0000 (17:33 +0900)
Extract functions logs history to class.

Change-Id: Ieda82e8ca44afb41eff7cec3028c8862e4d9432a
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/common/history_logger.cc [new file with mode: 0644]
src/common/history_logger.h [new file with mode: 0644]
src/common/installer/app_installer.cc
src/common/installer/app_installer.h

diff --git a/src/common/history_logger.cc b/src/common/history_logger.cc
new file mode 100644 (file)
index 0000000..263dcc6
--- /dev/null
@@ -0,0 +1,53 @@
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#include "common/history_logger.h"
+
+#include <memory>
+#include <string>
+
+#include "common/utils/file_logbackend.h"
+#include "common/utils/request.h"
+
+namespace {
+
+constexpr const char kHistoryFileName[] = LOGDIR"/installation-history.log";
+constexpr const int kLogRotationSize = 1024 * 256;  // 256KB
+constexpr const int kLogMaximumRotation = 3;
+
+}  // namespace
+
+namespace common_installer {
+
+HistoryLogger::HistoryLogger() : started_(false) {
+  backend_ = std::make_unique<utils::FileLogBackend>(kHistoryFileName,
+      kLogRotationSize, kLogMaximumRotation);
+}
+
+void HistoryLogger::LogHistoryStart(const std::string& pkgid,
+    RequestType type) {
+  if (started_)
+    return;
+
+  HistoryBuilder builder;
+  builder.Append(pkgid).Append(GetRequestTypeString(type)).Append("START");
+  backend_->WriteLog(::utils::LogLevel::LOG_INFO, "", builder.Build());
+  backend_->WriteLogToFile();
+
+  started_ = true;
+}
+
+void HistoryLogger::LogHistoryEnd(const std::string& pkgid,
+    RequestType type, bool success) {
+  HistoryBuilder builder;
+  builder.Append(pkgid).Append(GetRequestTypeString(type));
+  if (success)
+    builder.Append("SUCCESS");
+  else
+    builder.Append("FAIL");
+  backend_->WriteLog(::utils::LogLevel::LOG_INFO, "", builder.Build());
+  backend_->WriteLogToFile();
+}
+
+}  // namespace common_installer
diff --git a/src/common/history_logger.h b/src/common/history_logger.h
new file mode 100644 (file)
index 0000000..f30cc90
--- /dev/null
@@ -0,0 +1,50 @@
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef COMMON_HISTORY_LOGGER_H_
+#define COMMON_HISTORY_LOGGER_H_
+
+#include <memory>
+#include <string>
+
+#include "common/utils/request.h"
+
+namespace utils {
+
+class FileLogBackend;
+
+}  // namespace utils
+
+namespace common_installer {
+
+class HistoryLogger {
+ public:
+  HistoryLogger();
+  ~HistoryLogger() {}
+  void LogHistoryStart(const std::string& pkgid, RequestType type);
+  void LogHistoryEnd(const std::string& pkgid, RequestType type, bool success);
+
+ private:
+  class HistoryBuilder {
+   public:
+    HistoryBuilder& Append(const std::string& string) {
+      if (!history_.empty())
+        history_ += "|";
+      history_ += string;
+      return *this;
+    }
+    std::string Build() { return history_; }
+
+   private:
+    std::string history_;
+  };
+
+  bool started_;
+
+  std::unique_ptr<utils::FileLogBackend> backend_;
+};
+
+}  // namespace common_installer
+
+#endif  // COMMON_HISTORY_LOGGER_H_
index caed2af..905021a 100644 (file)
@@ -223,30 +223,6 @@ AppInstaller::Result AppInstaller::Run() {
   return result_;
 }
 
-void AppInstaller::LogHistoryStart() {
-  std::shared_ptr<utils::FileLogBackend> history_logger =
-      std::shared_ptr<utils::FileLogBackend>(new utils::FileLogBackend(
-          kHistoryFileName, kLogRotationSize, kLogMaximumRotation));
-  std::string history = context_->pkgid.get() + "|" +
-      GetRequestTypeString(context_->request_type.get()) + "|START";
-  history_logger->WriteLog(::utils::LogLevel::LOG_INFO, "", history);
-  history_logger->WriteLogToFile();
-}
-
-void AppInstaller::LogHistoryEnd(bool success) {
-  std::shared_ptr<utils::FileLogBackend> history_logger =
-      std::shared_ptr<utils::FileLogBackend>(new utils::FileLogBackend(
-          kHistoryFileName, kLogRotationSize, kLogMaximumRotation));
-  std::string history = context_->pkgid.get() + "|" +
-      GetRequestTypeString(context_->request_type.get());
-  if (success)
-    history += "|SUCCESS";
-  else
-    history += "|FAIL";
-  history_logger->WriteLog(::utils::LogLevel::LOG_INFO, "", history);
-  history_logger->WriteLogToFile();
-}
-
 AppInstaller::Result AppInstaller::Process() {
   Init();
 
@@ -270,6 +246,9 @@ AppInstaller::Result AppInstaller::Process() {
       }
       break;
     }
+    if (!context_->pkgid.get().empty())
+      history_logger_.LogHistoryStart(context_->pkgid.get(),
+          context_->request_type.get());
     SendProgress(current_step * kProgressRange / total_steps);
   }
 
@@ -297,7 +276,8 @@ AppInstaller::Result AppInstaller::Undo() {
     status_ = Step::Status::ERROR;
   SendFinished(status_);
 
-  LogHistoryEnd(false);
+  history_logger_.LogHistoryEnd(context_->pkgid.get(),
+      context_->request_type.get(), false);
 
   return result_;
 }
@@ -322,7 +302,8 @@ AppInstaller::Result AppInstaller::Clean() {
   sync();
   SendFinished(status_);
 
-  LogHistoryEnd(true);
+  history_logger_.LogHistoryEnd(context_->pkgid.get(),
+      context_->request_type.get(), true);
 
   return result_;
 }
@@ -850,8 +831,6 @@ bool AppInstaller::SendStartIfNotSent(bool is_skippable) {
   pi_->SetRequestType(context_->request_type.get());
   pi_->SendStarted(context_->pkg_type.get(), context_->pkgid.get());
 
-  LogHistoryStart();
-
   return true;
 }
 
index b0e917d..62ef0a0 100644 (file)
@@ -16,6 +16,7 @@
 #include <utility>
 
 #include "common/archive_info.h"
+#include "common/history_logger.h"
 #include "common/pkgmgr_interface.h"
 #include "common/pkgmgr_signal.h"
 #include "common/step/step.h"
@@ -302,10 +303,9 @@ class AppInstaller {
   std::shared_ptr<utils::FileLogBackend> failure_logger_;
   Step::Status status_;
   Result result_;
-  void LogHistoryStart();
-  void LogHistoryEnd(bool success);
 
   int index_;
+  HistoryLogger history_logger_;
 
   friend class InstallerRunner;