• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "hilog/log.h"
28 #include "time_util.h"
29 
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD002D07
32 
33 #undef LOG_TAG
34 #define LOG_TAG "Write"
35 
36 namespace OHOS {
37 namespace HiviewDFX {
38 namespace {
39 constexpr int DB_FAILED = -1;
40 std::mutex g_mutex;
41 constexpr int SUBMIT_FAILED_NUM = 50;
42 static int g_submitFailedCnt = 0;
43 static std::mutex g_submitFailedCntMutex;
44 
GetStorageDirPath()45 std::string GetStorageDirPath()
46 {
47     return HiAppEventConfig::GetInstance().GetStorageDir();
48 }
49 
GetStorageFileName()50 std::string GetStorageFileName()
51 {
52     return "app_event_" + TimeUtil::GetDate() + ".log";
53 }
54 
WriteEventToFile(const std::string & filePath,const std::string & event)55 bool WriteEventToFile(const std::string& filePath, const std::string& event)
56 {
57     return FileUtil::SaveStringToFile(filePath, event);
58 }
59 }
60 
SubmitWritingTask(std::shared_ptr<AppEventPack> appEventPack,const std::string & taskName)61 void SubmitWritingTask(std::shared_ptr<AppEventPack> appEventPack, const std::string& taskName)
62 {
63     AppEventObserverMgr::GetInstance().SubmitTaskToFFRTQueue([appEventPack]() {
64         WriteEvent(appEventPack);
65         }, taskName);
66 }
67 
WriteEvent(std::shared_ptr<AppEventPack> appEventPack)68 void WriteEvent(std::shared_ptr<AppEventPack> appEventPack)
69 {
70     if (HiAppEventConfig::GetInstance().GetDisable()) {
71         HILOG_WARN(LOG_CORE, "the HiAppEvent function is disabled.");
72         return;
73     }
74     if (HiAppEventConfig::GetInstance().IsFreeSizeOverLimit()) {
75         HILOG_WARN(LOG_CORE, "Write:free size over limit.");
76         return;
77     }
78     if (appEventPack == nullptr) {
79         HILOG_ERROR(LOG_CORE, "appEventPack is null.");
80         return;
81     }
82     std::string dirPath = GetStorageDirPath();
83     if (dirPath.empty()) {
84         HILOG_ERROR(LOG_CORE, "dirPath is null, stop writing the event.");
85         return;
86     }
87     std::string event = appEventPack->GetEventStr();
88     HILOG_DEBUG(LOG_CORE, "WriteEvent domain=%{public}s, name=%{public}s.",
89         appEventPack->GetDomain().c_str(), appEventPack->GetName().c_str());
90     {
91         std::lock_guard<std::mutex> lockGuard(g_mutex);
92         if (!FileUtil::IsFileExists(dirPath) && !FileUtil::ForceCreateDirectory(dirPath)) {
93             HILOG_ERROR(LOG_CORE, "failed to create hiappevent dir, errno=%{public}d.", errno);
94             return;
95         }
96         HiAppEventClean::CheckStorageSpace();
97         std::string filePath = FileUtil::GetFilePathByDir(dirPath, GetStorageFileName());
98         if (!WriteEventToFile(filePath, event)) {
99             HILOG_ERROR(LOG_CORE, "failed to write event to log file, errno=%{public}d.", errno);
100             return;
101         }
102     }
103     std::vector<std::shared_ptr<AppEventPack>> events;
104     events.emplace_back(appEventPack);
105     AppEventObserverMgr::GetInstance().HandleEvents(events);
106 }
107 
SetEventParam(std::shared_ptr<AppEventPack> appEventPack)108 int SetEventParam(std::shared_ptr<AppEventPack> appEventPack)
109 {
110     if (appEventPack == nullptr) {
111         HILOG_ERROR(LOG_CORE, "appEventPack is null.");
112         return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
113     }
114     if (HiAppEventConfig::GetInstance().IsFreeSizeOverLimit()) {
115         HILOG_WARN(LOG_CORE, "free size over limit.");
116         return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
117     }
118     int res = AppEventStore::GetInstance().InsertCustomEventParams(appEventPack);
119     if (res == DB_FAILED) {
120         HILOG_ERROR(LOG_CORE, "failed to insert event param, domain=%{public}s.", appEventPack->GetDomain().c_str());
121         return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
122     }
123     return res;
124 }
125 } // namespace HiviewDFX
126 } // namespace OHOS
127