• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "event_write_handler.h"
17 
18 #include "file_util.h"
19 #include "hiview_logger.h"
20 #include "string_util.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 DEFINE_LOG_TAG("HiView-EventWriteHandler");
HandleRequest(RequestPtr req)25 bool EventWriteHandler::HandleRequest(RequestPtr req)
26 {
27     auto writeReq = BaseRequest::DownCastTo<EventWriteRequest>(req);
28     for (const auto& sysEvent : writeReq->sysEvents) {
29         auto writer = GetEventWriter(sysEvent->version, writeReq);
30         if (!writer->AppendEvent(sysEvent->domain, sysEvent->name, sysEvent->eventStr)) {
31             HIVIEW_LOGE("failed to append event to event writer");
32             Rollback();
33             return false;
34         }
35     }
36     if (!writeReq->isQueryCompleted) {
37         return true;
38     }
39     for (const auto& writer : allJsonFileWriters_) {
40         if (writer.second == nullptr) {
41             continue;
42         }
43         if (!writer.second->Write()) {
44             HIVIEW_LOGE("failed to write export event");
45             Rollback();
46             return false;
47         }
48     }
49     CopyTmpZipFilesToDest();
50     return true;
51 }
52 
GetEventWriter(const std::string & sysVersion,std::shared_ptr<EventWriteRequest> writeReq)53 std::shared_ptr<ExportJsonFileWriter> EventWriteHandler::GetEventWriter(const std::string& sysVersion,
54     std::shared_ptr<EventWriteRequest> writeReq)
55 {
56     auto writerKey = std::make_pair(writeReq->moduleName, sysVersion);
57     auto iter = allJsonFileWriters_.find(writerKey);
58     if (iter == allJsonFileWriters_.end()) {
59         HIVIEW_LOGI("create json file writer with version %{public}s", sysVersion.c_str());
60         auto jsonFileWriter = std::make_shared<ExportJsonFileWriter>(writeReq->moduleName, sysVersion,
61             writeReq->exportDir, writeReq->maxSingleFileSize);
62         jsonFileWriter->SetExportJsonFileZippedListener([this] (const std::string& srcPath,
63             const std::string& destPath) {
64             zippedExportFileMap_[srcPath] = destPath;
65         });
66         allJsonFileWriters_.emplace(writerKey, jsonFileWriter);
67         return jsonFileWriter;
68     }
69     return iter->second;
70 }
71 
CopyTmpZipFilesToDest()72 void EventWriteHandler::CopyTmpZipFilesToDest()
73 {
74     // move all tmp zipped event export file to dest dir
75     std::for_each(zippedExportFileMap_.begin(), zippedExportFileMap_.end(), [] (const auto& item) {
76         if (!FileUtil::RenameFile(item.first, item.second)) {
77             HIVIEW_LOGE("failed to move %{public}s to %{public}s", StringUtil::HideDeviceIdInfo(item.first).c_str(),
78                 StringUtil::HideDeviceIdInfo(item.second).c_str());
79         }
80         HIVIEW_LOGI("zip file to export: %{public}s", StringUtil::HideDeviceIdInfo(item.second).c_str());
81     });
82     zippedExportFileMap_.clear();
83 }
84 
Rollback()85 void EventWriteHandler::Rollback()
86 {
87     for (const auto& writer : allJsonFileWriters_) {
88         if (writer.second == nullptr) {
89             continue;
90         }
91         writer.second->ClearEventCache();
92     }
93     // delete all tmp zipped export file
94     std::for_each(zippedExportFileMap_.begin(), zippedExportFileMap_.end(), [] (const auto& item) {
95         if (!FileUtil::RemoveFile(item.first)) {
96             HIVIEW_LOGE("failed to delete %{public}s", StringUtil::HideDeviceIdInfo(item.first).c_str());
97         }
98     });
99     zippedExportFileMap_.clear();
100 }
101 } // HiviewDFX
102 } // OHOS