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 "ffrt.h"
24 #include "file_util.h"
25 #include "hiappevent_base.h"
26 #include "hiappevent_clean.h"
27 #include "hiappevent_config.h"
28 #include "hiappevent_read.h"
29 #include "hilog/log.h"
30 #include "time_util.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 ffrt::mutex g_mutex;
43 
GetStorageDirPath()44 std::string GetStorageDirPath()
45 {
46     return HiAppEventConfig::GetInstance().GetStorageDir();
47 }
48 
GetStorageFileName()49 std::string GetStorageFileName()
50 {
51     return "app_event_" + TimeUtil::GetDate() + ".log";
52 }
53 
WriteEventToFile(const std::string & filePath,const std::string & event)54 bool WriteEventToFile(const std::string& filePath, const std::string& event)
55 {
56     LogAssistant::Instance().RealTimeAppLogUpdate(event);
57     return FileUtil::SaveStringToFile(filePath, event);
58 }
59 }
60 
WriteEvent(std::shared_ptr<AppEventPack> appEventPack)61 void WriteEvent(std::shared_ptr<AppEventPack> appEventPack)
62 {
63     if (HiAppEventConfig::GetInstance().GetDisable()) {
64         HILOG_WARN(LOG_CORE, "the HiAppEvent function is disabled.");
65         return;
66     }
67     if (appEventPack == nullptr) {
68         HILOG_ERROR(LOG_CORE, "appEventPack is null.");
69         return;
70     }
71     std::string dirPath = GetStorageDirPath();
72     if (dirPath.empty()) {
73         HILOG_ERROR(LOG_CORE, "dirPath is null, stop writing the event.");
74         return;
75     }
76     std::string event = appEventPack->GetEventStr();
77     HILOG_DEBUG(LOG_CORE, "WriteEvent domain=%{public}s, name=%{public}s.",
78         appEventPack->GetDomain().c_str(), appEventPack->GetName().c_str());
79     {
80         std::lock_guard<ffrt::mutex> lockGuard(g_mutex);
81         if (!FileUtil::IsFileExists(dirPath) && !FileUtil::ForceCreateDirectory(dirPath)) {
82             HILOG_ERROR(LOG_CORE, "failed to create hiappevent dir, errno=%{public}d.", errno);
83             return;
84         }
85         HiAppEventClean::CheckStorageSpace();
86         std::string filePath = FileUtil::GetFilePathByDir(dirPath, GetStorageFileName());
87         if (WriteEventToFile(filePath, event)) {
88             std::vector<std::shared_ptr<AppEventPack>> events;
89             events.emplace_back(appEventPack);
90             AppEventObserverMgr::GetInstance().HandleEvents(events);
91             return;
92         }
93         HILOG_ERROR(LOG_CORE, "failed to write event to log file, errno=%{public}d.", errno);
94     }
95 }
96 
SetEventParam(std::shared_ptr<AppEventPack> appEventPack)97 int SetEventParam(std::shared_ptr<AppEventPack> appEventPack)
98 {
99     if (appEventPack == nullptr) {
100         HILOG_ERROR(LOG_CORE, "appEventPack is null.");
101         return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
102     }
103     int res = AppEventStore::GetInstance().InsertCustomEventParams(appEventPack);
104     if (res == DB_FAILED) {
105         HILOG_ERROR(LOG_CORE, "failed to insert event param, domain=%{public}s.", appEventPack->GetDomain().c_str());
106         return ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL;
107     }
108     return res;
109 }
110 } // namespace HiviewDFX
111 } // namespace OHOS
112