1 /*
2 * Copyright (c) 2021-2022 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_watcher_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 "hitrace/trace.h"
29 #include "time_util.h"
30
31 namespace OHOS {
32 namespace HiviewDFX {
33 namespace {
34 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_Write" };
35 std::mutex g_mutex;
36
GetStorageDirPath()37 std::string GetStorageDirPath()
38 {
39 return HiAppEventConfig::GetInstance().GetStorageDir();
40 }
41
GetMaxStorageSize()42 uint64_t GetMaxStorageSize()
43 {
44 return HiAppEventConfig::GetInstance().GetMaxStorageSize();
45 }
46
GetStorageFileName()47 std::string GetStorageFileName()
48 {
49 return "app_event_" + TimeUtil::GetDate() + ".log";
50 }
51
CheckStorageSpace(const std::string & dir)52 void CheckStorageSpace(const std::string& dir)
53 {
54 auto maxSize = GetMaxStorageSize();
55 if (!HiAppEventClean::IsStorageSpaceFull(dir, maxSize)) {
56 return;
57 }
58 HiLog::Info(LABEL, "hiappevent dir space is full, start to clean");
59 HiAppEventClean::ReleaseSomeStorageSpace(dir, maxSize);
60 }
61
WriteEventToFile(const std::string & filePath,const std::string & event)62 bool WriteEventToFile(const std::string& filePath, const std::string& event)
63 {
64 LogAssistant::Instance().RealTimeAppLogUpdate(event);
65 return FileUtil::SaveStringToFile(filePath, event);
66 }
67
TraceAppEventPack(const std::shared_ptr<AppEventPack> & appEventPack)68 void TraceAppEventPack(const std::shared_ptr<AppEventPack>& appEventPack)
69 {
70 HiTraceId hitraceId = HiTraceChain::GetId();
71 if (!hitraceId.IsValid()) {
72 return;
73 }
74 appEventPack->AddParam("traceid_", static_cast<int64_t>(hitraceId.GetChainId()));
75 appEventPack->AddParam("spanid_", static_cast<int64_t>(hitraceId.GetSpanId()));
76 appEventPack->AddParam("pspanid_", static_cast<int64_t>(hitraceId.GetParentSpanId()));
77 appEventPack->AddParam("trace_flag_", hitraceId.GetFlags());
78 }
79 }
80
WriteEvent(const std::shared_ptr<AppEventPack> & appEventPack)81 void WriteEvent(const std::shared_ptr<AppEventPack>& appEventPack)
82 {
83 if (HiAppEventConfig::GetInstance().GetDisable()) {
84 HiLog::Warn(LABEL, "the HiAppEvent function is disabled.");
85 return;
86 }
87 if (appEventPack == nullptr) {
88 HiLog::Error(LABEL, "appEventPack is null.");
89 return;
90 }
91 std::string dirPath = GetStorageDirPath();
92 if (dirPath.empty()) {
93 HiLog::Error(LABEL, "dirPath is null, stop writing the event.");
94 return;
95 }
96 TraceAppEventPack(appEventPack);
97 HiLog::Debug(LABEL, "WriteEvent eventInfo=%{public}s.", appEventPack->GetJsonString().c_str());
98
99 {
100 std::lock_guard<std::mutex> lockGuard(g_mutex);
101 if (!FileUtil::IsFileExists(dirPath) && !FileUtil::ForceCreateDirectory(dirPath)) {
102 HiLog::Error(LABEL, "failed to create hiappevent dir, errno=%{public}d.", errno);
103 return;
104 }
105 CheckStorageSpace(dirPath);
106 std::string filePath = FileUtil::GetFilePathByDir(dirPath, GetStorageFileName());
107 std::string event = appEventPack->GetJsonString();
108 if (WriteEventToFile(filePath, event)) {
109 AppEventWatcherMgr::GetInstance()->HandleEvent(appEventPack->GetEventDomain(),
110 appEventPack->GetType(), event);
111 } else {
112 HiLog::Error(LABEL, "failed to write event to log file, errno=%{public}d.", errno);
113 }
114 }
115 }
116 } // namespace HiviewDFX
117 } // namespace OHOS
118