Extract functions logs history to class.
Change-Id: Ieda82e8ca44afb41eff7cec3028c8862e4d9432a
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
--- /dev/null
+// 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
--- /dev/null
+// 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_
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();
}
break;
}
+ if (!context_->pkgid.get().empty())
+ history_logger_.LogHistoryStart(context_->pkgid.get(),
+ context_->request_type.get());
SendProgress(current_step * kProgressRange / total_steps);
}
status_ = Step::Status::ERROR;
SendFinished(status_);
- LogHistoryEnd(false);
+ history_logger_.LogHistoryEnd(context_->pkgid.get(),
+ context_->request_type.get(), false);
return result_;
}
sync();
SendFinished(status_);
- LogHistoryEnd(true);
+ history_logger_.LogHistoryEnd(context_->pkgid.get(),
+ context_->request_type.get(), true);
return result_;
}
pi_->SetRequestType(context_->request_type.get());
pi_->SendStarted(context_->pkg_type.get(), context_->pkgid.get());
- LogHistoryStart();
-
return true;
}
#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"
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;