1 /*
2 * Copyright (c) 2021-2023 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_observer_mgr.h"
22 #include "file_util.h"
23 #include "hiappevent_base.h"
24 #include "hiappevent_clean.h"
25 #include "hiappevent_config.h"
26 #include "hiappevent_read.h"
27 #include "hilog/log.h"
28 #include "time_util.h"
29
30 namespace OHOS {
31 namespace HiviewDFX {
32 namespace {
33 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_Write" };
34 std::mutex g_mutex;
35
GetStorageDirPath()36 std::string GetStorageDirPath()
37 {
38 return HiAppEventConfig::GetInstance().GetStorageDir();
39 }
40
GetMaxStorageSize()41 uint64_t GetMaxStorageSize()
42 {
43 return HiAppEventConfig::GetInstance().GetMaxStorageSize();
44 }
45
GetStorageFileName()46 std::string GetStorageFileName()
47 {
48 return "app_event_" + TimeUtil::GetDate() + ".log";
49 }
50
CheckStorageSpace(const std::string & dir)51 void CheckStorageSpace(const std::string& dir)
52 {
53 auto maxSize = GetMaxStorageSize();
54 if (!HiAppEventClean::IsStorageSpaceFull(dir, maxSize)) {
55 return;
56 }
57 HiLog::Info(LABEL, "hiappevent dir space is full, start to clean");
58 HiAppEventClean::ReleaseSomeStorageSpace(dir, maxSize);
59 }
60
WriteEventToFile(const std::string & filePath,const std::string & event)61 bool WriteEventToFile(const std::string& filePath, const std::string& event)
62 {
63 LogAssistant::Instance().RealTimeAppLogUpdate(event);
64 return FileUtil::SaveStringToFile(filePath, event);
65 }
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(LABEL, "the HiAppEvent function is disabled.");
72 return;
73 }
74 if (appEventPack == nullptr) {
75 HiLog::Error(LABEL, "appEventPack is null.");
76 return;
77 }
78 std::string dirPath = GetStorageDirPath();
79 if (dirPath.empty()) {
80 HiLog::Error(LABEL, "dirPath is null, stop writing the event.");
81 return;
82 }
83 std::string event = appEventPack->GetEventStr();
84 HiLog::Debug(LABEL, "WriteEvent eventInfo=%{public}s.", event.c_str());
85 {
86 std::lock_guard<std::mutex> lockGuard(g_mutex);
87 if (!FileUtil::IsFileExists(dirPath) && !FileUtil::ForceCreateDirectory(dirPath)) {
88 HiLog::Error(LABEL, "failed to create hiappevent dir, errno=%{public}d.", errno);
89 return;
90 }
91 CheckStorageSpace(dirPath);
92 std::string filePath = FileUtil::GetFilePathByDir(dirPath, GetStorageFileName());
93 if (WriteEventToFile(filePath, event)) {
94 std::vector<std::shared_ptr<AppEventPack>> events;
95 events.emplace_back(appEventPack);
96 AppEventObserverMgr::GetInstance().HandleEvents(events);
97 return;
98 }
99 HiLog::Error(LABEL, "failed to write event to log file, errno=%{public}d.", errno);
100 }
101 }
102 } // namespace HiviewDFX
103 } // namespace OHOS
104