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 #include "cloud_file_fault_event.h"
16
17 #include <cstdlib>
18 #include <ctime>
19 #include <mutex>
20 #include <unordered_map>
21 #include <vector>
22
23 #include "cloud_file_log.h"
24 #include "dfs_error.h"
25
26 namespace OHOS {
27 namespace FileManagement {
28 namespace CloudFile {
29
GetInstance()30 CloudFaultReportStatus& CloudFaultReportStatus::GetInstance()
31 {
32 static CloudFaultReportStatus instance_;
33 return instance_;
34 }
35
IsAllowToReport(const FaultType & faultType)36 bool CloudFaultReportStatus::IsAllowToReport(const FaultType &faultType)
37 {
38 std::lock_guard<std::mutex> timeMapLock(timeMapLock_);
39 if (lastTimeMap_.find(faultType) != lastTimeMap_.end()) {
40 struct timespec t;
41 clock_gettime(CLOCK_REALTIME, &t);
42 int64_t currentTime_ = t.tv_sec;
43 if (lastTimeMap_[faultType] == 0 ||
44 (currentTime_ > lastTimeMap_[faultType] &&
45 currentTime_ - lastTimeMap_[faultType] > MINIMUM_FAULT_REPORT_INTERVAL)) {
46 lastTimeMap_[faultType] = currentTime_;
47 return true;
48 } else {
49 return false;
50 }
51 }
52 return true;
53 }
54
CloudFaultReportStatus()55 CloudFaultReportStatus::CloudFaultReportStatus()
56 {
57 for (const auto &faultType : PERIODIC_REPORT_FAULT_TYPE) {
58 lastTimeMap_[faultType] = 0;
59 }
60 }
61
CloudSyncFaultReport(const std::string & funcName,const int lineNum,const CloudSyncFaultInfo & event)62 int32_t CloudFileFaultEvent::CloudSyncFaultReport(const std::string &funcName,
63 const int lineNum,
64 const CloudSyncFaultInfo &event)
65 {
66 if (CloudFaultReportStatus::GetInstance().IsAllowToReport(event.faultType_)) {
67 int32_t ret = CLOUD_SYNC_SYS_EVENT("CLOUD_FILE_SYNC_FAULT",
68 HiviewDFX::HiSysEvent::EventType::FAULT,
69 "bundle_name", event.bundleName_,
70 "fault_scenario", static_cast<uint32_t>(event.faultScenario_),
71 "fault_type", static_cast<uint32_t>(event.faultType_),
72 "fault_error_code", static_cast<uint32_t>(abs(event.faultErrorCode_)),
73 "function_name", funcName + ":" + std::to_string(lineNum),
74 "message", event.message_);
75 if (ret != E_OK) {
76 LOGE("report CLOUD_FILE_SYNC_FAULT error %{public}d", ret);
77 }
78 }
79 LOGE("%{public}s", event.message_.c_str());
80 return event.faultErrorCode_;
81 }
82
CloudFileFaultReport(const std::string & funcName,const int lineNum,const CloudFileFaultInfo & event)83 int32_t CloudFileFaultEvent::CloudFileFaultReport(const std::string &funcName,
84 const int lineNum,
85 const CloudFileFaultInfo &event)
86 {
87 if (CloudFaultReportStatus::GetInstance().IsAllowToReport(event.faultType_)) {
88 int32_t ret = CLOUD_SYNC_SYS_EVENT("CLOUD_FILE_ACCESS_FAULT",
89 HiviewDFX::HiSysEvent::EventType::FAULT,
90 "bundle_name", event.bundleName_,
91 "fault_operation", static_cast<uint32_t>(event.faultOperation_),
92 "fault_type", static_cast<uint32_t>(event.faultType_),
93 "fault_error_code", static_cast<uint32_t>(abs(event.faultErrorCode_)),
94 "function_name", funcName + ":" + std::to_string(lineNum),
95 "message", event.message_);
96 if (ret != E_OK) {
97 LOGE("report CLOUD_FILE_ACCESS_FAULT error %{public}d", ret);
98 }
99 }
100 LOGE("%{public}s", event.message_.c_str());
101 return event.faultErrorCode_;
102 }
103 } // namespace CloudFile
104 } // namespace FileManagement
105 } // namespace OHOS
106