1 /*
2 * Copyright (c) 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 "hisysevent_query_callback_c.h"
17
18 #include "hilog/log.h"
19 #include "hisysevent_record_c.h"
20 #include "string_util.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HISYSEVENT_C_QUERY" };
24 using HiSysEventRecordCls = OHOS::HiviewDFX::HiSysEventRecord;
25 using OHOS::HiviewDFX::HiLog;
26
ConvertDomain(const HiSysEventRecordCls & recordObj,HiSysEventRecord & recordStruct)27 int ConvertDomain(const HiSysEventRecordCls& recordObj, HiSysEventRecord& recordStruct)
28 {
29 constexpr size_t maxLen = 16;
30 return OHOS::HiviewDFX::StringUtil::CopyCString(recordStruct.domain, recordObj.GetDomain(), maxLen);
31 }
32
ConvertEventName(const HiSysEventRecordCls & recordObj,HiSysEventRecord & recordStruct)33 int ConvertEventName(const HiSysEventRecordCls& recordObj, HiSysEventRecord& recordStruct)
34 {
35 constexpr size_t maxLen = 32;
36 return OHOS::HiviewDFX::StringUtil::CopyCString(recordStruct.eventName, recordObj.GetEventName(), maxLen);
37 }
38
ConvertTimeZone(const HiSysEventRecordCls & recordObj,HiSysEventRecord & recordStruct)39 int ConvertTimeZone(const HiSysEventRecordCls& recordObj, HiSysEventRecord& recordStruct)
40 {
41 constexpr size_t maxLen = 5;
42 return OHOS::HiviewDFX::StringUtil::CopyCString(recordStruct.tz, recordObj.GetTimeZone(), maxLen);
43 }
44
ConvertLevel(const HiSysEventRecordCls & recordObj,HiSysEventRecord & recordStruct)45 int ConvertLevel(const HiSysEventRecordCls& recordObj, HiSysEventRecord& recordStruct)
46 {
47 return OHOS::HiviewDFX::StringUtil::CreateCString(&recordStruct.level, recordObj.GetLevel());
48 }
49
ConvertTag(const HiSysEventRecordCls & recordObj,HiSysEventRecord & recordStruct)50 int ConvertTag(const HiSysEventRecordCls& recordObj, HiSysEventRecord& recordStruct)
51 {
52 return OHOS::HiviewDFX::StringUtil::CreateCString(&recordStruct.tag, recordObj.GetTag());
53 }
54
ConvertJsonStr(const HiSysEventRecordCls & recordObj,HiSysEventRecord & recordStruct)55 int ConvertJsonStr(const HiSysEventRecordCls& recordObj, HiSysEventRecord& recordStruct)
56 {
57 constexpr size_t maxLen = 384 * 1024; // max length of the event is 384KB
58 return OHOS::HiviewDFX::StringUtil::CreateCString(&recordStruct.jsonStr, recordObj.AsJson(), maxLen);
59 }
60
InitRecord(HiSysEventRecord & record)61 void InitRecord(HiSysEventRecord& record)
62 {
63 OHOS::HiviewDFX::StringUtil::MemsetSafe(&record, sizeof(struct HiSysEventRecord));
64 }
65
DeleteRecord(HiSysEventRecord & record)66 void DeleteRecord(HiSysEventRecord& record)
67 {
68 OHOS::HiviewDFX::StringUtil::DeletePointer<char>(&record.level);
69 OHOS::HiviewDFX::StringUtil::DeletePointer<char>(&record.tag);
70 OHOS::HiviewDFX::StringUtil::DeletePointer<char>(&record.jsonStr);
71 }
72
DeleteRecords(HiSysEventRecord ** records,size_t len)73 void DeleteRecords(HiSysEventRecord** records, size_t len)
74 {
75 if (records == nullptr || *records == nullptr) {
76 return;
77 }
78 auto realRs = *records;
79 for (size_t i = 0; i < len; i++) {
80 DeleteRecord(realRs[i]);
81 }
82 delete[] realRs;
83 realRs = nullptr;
84 }
85
ConvertRecord(const HiSysEventRecordCls & recordObj,HiSysEventRecord & recordStruct)86 int ConvertRecord(const HiSysEventRecordCls& recordObj, HiSysEventRecord& recordStruct)
87 {
88 if (int res = ConvertDomain(recordObj, recordStruct); res != 0) {
89 HiLog::Error(LABEL, "failed to covert domain=%{public}s", recordObj.GetDomain().c_str());
90 return res;
91 }
92 if (int res = ConvertEventName(recordObj, recordStruct); res != 0) {
93 HiLog::Error(LABEL, "failed to covert name=%{public}s", recordObj.GetEventName().c_str());
94 return res;
95 }
96 recordStruct.type = HiSysEventEventType(recordObj.GetEventType());
97 recordStruct.time = recordObj.GetTime();
98 if (int res = ConvertTimeZone(recordObj, recordStruct); res != 0) {
99 HiLog::Error(LABEL, "failed to covert tz=%{public}s", recordObj.GetTimeZone().c_str());
100 return res;
101 }
102 recordStruct.pid = recordObj.GetPid();
103 recordStruct.tid = recordObj.GetTid();
104 recordStruct.uid = recordObj.GetUid();
105 recordStruct.traceId = recordObj.GetTraceId();
106 recordStruct.spandId = recordObj.GetSpanId();
107 recordStruct.pspanId = recordObj.GetPspanId();
108 recordStruct.traceFlag = recordObj.GetTraceFlag();
109 if (int res = ConvertLevel(recordObj, recordStruct); res != 0) {
110 HiLog::Error(LABEL, "failed to covert level=%{public}s", recordObj.GetLevel().c_str());
111 return res;
112 }
113 if (int res = ConvertTag(recordObj, recordStruct); res != 0) {
114 HiLog::Error(LABEL, "failed to covert tag=%{public}s", recordObj.GetTag().c_str());
115 return res;
116 }
117 if (int res = ConvertJsonStr(recordObj, recordStruct); res != 0) {
118 HiLog::Error(LABEL, "failed to covert jsonStr=%{public}s", recordObj.AsJson().c_str());
119 return res;
120 }
121 return 0;
122 }
123 }
OnQuery(std::shared_ptr<std::vector<OHOS::HiviewDFX::HiSysEventRecord>> sysEvents)124 void HiSysEventQueryCallbackC::OnQuery(std::shared_ptr<std::vector<OHOS::HiviewDFX::HiSysEventRecord>> sysEvents)
125 {
126 if (onQuery_ == nullptr) {
127 HiLog::Error(LABEL, "the OnQuery function of the callback is null");
128 return;
129 }
130 if (sysEvents == nullptr || sysEvents->empty()) {
131 onQuery_(nullptr, 0);
132 return;
133 }
134 size_t size = sysEvents->size();
135 auto records = new(std::nothrow) HiSysEventRecord[size];
136 for (size_t i = 0; i < size; i++) {
137 InitRecord(records[i]);
138 if (ConvertRecord(sysEvents->at(i), records[i]) != 0) {
139 HiLog::Error(LABEL, "failed to covert record, index=%{public}zu, size=%{public}zu", i, size);
140 DeleteRecords(&records, i + 1); // +1 for release the current record
141 return;
142 }
143 }
144 onQuery_(records, size);
145 DeleteRecords(&records, size);
146 }
147
OnComplete(int32_t reason,int32_t total)148 void HiSysEventQueryCallbackC::OnComplete(int32_t reason, int32_t total)
149 {
150 if (onComplete_ == nullptr) {
151 HiLog::Error(LABEL, "the OnComplete function of the callback is null");
152 return;
153 }
154 onComplete_(reason, total);
155 }
156