1 /*
2 * Copyright (c) 2021-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 "hiappevent_write.h"
17
18 #include <mutex>
19 #include <string>
20
21 #include "app_event_store.h"
22 #include "app_event_observer_mgr.h"
23 #include "file_util.h"
24 #include "hiappevent_base.h"
25 #include "hiappevent_clean.h"
26 #include "hiappevent_config.h"
27 #include "hiappevent_ffrt.h"
28 #include "hilog/log.h"
29 #include "time_util.h"
30 #include "xcollie/watchdog.h"
31
32 #undef LOG_DOMAIN
33 #define LOG_DOMAIN 0xD002D07
34
35 #undef LOG_TAG
36 #define LOG_TAG "Write"
37
38 namespace OHOS {
39 namespace HiviewDFX {
40 namespace {
41 constexpr int DB_FAILED = -1;
42 const std::string MAIN_THREAD_JANK = "MAIN_THREAD_JANK";
43 std::mutex g_mutex;
44 constexpr int SUBMIT_FAILED_NUM = 50;
45 static int g_submitFailedCnt = 0;
46 static std::mutex g_submitFailedCntMutex;
47
GetStorageDirPath()48 std::string GetStorageDirPath()
49 {
50 return HiAppEventConfig::GetInstance().GetStorageDir();
51 }
52
GetStorageFileName()53 std::string GetStorageFileName()
54 {
55 return "app_event_" + TimeUtil::GetDate() + ".log";
56 }
57
WriteEventToFile(const std::string & filePath,const std::string & event)58 bool WriteEventToFile(const std::string& filePath, const std::string& event)
59 {
60 return FileUtil::SaveStringToFile(filePath, event);
61 }
62 }
63
SubmitWritingTask(std::shared_ptr<AppEventPack> appEventPack,const std::string & taskName)64 void SubmitWritingTask(std::shared_ptr<AppEventPack> appEventPack, const std::string& taskName)
65 {
66 auto ret = HiAppEvent::Submit([appEventPack]() {
67 WriteEvent(appEventPack);
68 }, {}, {}, ffrt::task_attr().name(taskName.c_str()));
69 if (ret != ffrt_success) {
70 std::lock_guard<std::mutex> lockGuard(g_submitFailedCntMutex);
71 ++g_submitFailedCnt;
72 if (g_submitFailedCnt >= SUBMIT_FAILED_NUM) {
73 HILOG_ERROR(LOG_CORE, "failed to submit %{public}s %{public}s, ret=%{public}d",
74 taskName.c_str(), appEventPack->GetParamApiStr().c_str(), ret);
75 g_submitFailedCnt = 0;
76 }
77 }
78 }
79
WriteEvent(std::shared_ptr<AppEventPack> appEventPack)80 void WriteEvent(std::shared_ptr<AppEventPack> appEventPack)
81 {
82 if (HiAppEventConfig::GetInstance().GetDisable()) {
83 HILOG_WARN(LOG_CORE, "the HiAppEvent function is disabled.");
84 return;
85 }
86 if (HiAppEventConfig::GetInstance().IsFreeSizeOverLimit()) {
87 HILOG_WARN(LOG_CORE, "Write:free size over limit.");
88 return;
89 }
90 if (appEventPack == nullptr) {
91 HILOG_ERROR(LOG_CORE, "appEventPack is null.");
92 return;
93 }
94 std::string dirPath = GetStorageDirPath();
95 if (dirPath.empty()) {
96 HILOG_ERROR(LOG_CORE, "dirPath is null, stop writing the event.");
97 return;
98 }
99 std::string event = appEventPack->GetEventStr();
100 HILOG_DEBUG(LOG_CORE, "WriteEvent domain=%{public}s, name=%{public}s.",
101 appEventPack->GetDomain().c_str(), appEventPack->GetName().c_str());
102 {
103 std::lock_guard<std::mutex> lockGuard(g_mutex);
104 if (!FileUtil::IsFileExists(dirPath) && !FileUtil::ForceCreateDirectory(dirPath)) {
105 HILOG_ERROR(LOG_CORE, "failed to create hiappevent dir, errno=%{public}d.", errno);
106 return;
107 }
108 HiAppEventClean::CheckStorageSpace();
109 std::string filePath = FileUtil::GetFilePathByDir(dirPath, GetStorageFileName());
110 if (!WriteEventToFile(filePath, event)) {
111 HILOG_ERROR(LOG_CORE, "failed to write event to log file, errno=%{public}d.", errno);
112 return;
113 }
114 }
115 std::vector<std::shared_ptr<AppEventPack>> events;
116 events.emplace_back(appEventPack);
117 AppEventObserverMgr::GetInstance().HandleEvents(events);
118 }
119
SetEventParam(std::shared_ptr<AppEventPack> appEventPack)120 int SetEventParam(std::shared_ptr<AppEventPack> appEventPack)
121 {
122 if (appEventPack == nullptr) {
123 HILOG_ERROR(LOG_CORE, "appEventPack is null.");
124 return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
125 }
126 if (HiAppEventConfig::GetInstance().IsFreeSizeOverLimit()) {
127 HILOG_WARN(LOG_CORE, "free size over limit.");
128 return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
129 }
130 int res = AppEventStore::GetInstance().InsertCustomEventParams(appEventPack);
131 if (res == DB_FAILED) {
132 HILOG_ERROR(LOG_CORE, "failed to insert event param, domain=%{public}s.", appEventPack->GetDomain().c_str());
133 return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
134 }
135 return res;
136 }
137
SetEventConfig(const std::string & name,const std::map<std::string,std::string> & configMap)138 int SetEventConfig(const std::string& name, const std::map<std::string, std::string> &configMap)
139 {
140 if (name != MAIN_THREAD_JANK) {
141 HILOG_ERROR(LOG_CORE, "Failed to set event config, name is invalid. name=%{public}s", name.c_str());
142 return ErrorCode::ERROR_INVALID_PARAM_VALUE;
143 }
144 return Watchdog::GetInstance().SetEventConfig(configMap);
145 }
146 } // namespace HiviewDFX
147 } // namespace OHOS
148