1 /*
2 * Copyright (c) 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 "hisysevent_record_convertor.h"
17
18 #include "hilog/log.h"
19 #include "hisysevent_record_c.h"
20 #include "string_util.h"
21
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HISYSEVENT_RECORD_CONVERTOR" };
26 using OHOS::HiviewDFX::HiLog;
27 }
28
ConvertDomain(const HiSysEventRecordCls & recordObj,HiSysEventRecordC & recordStruct)29 int HiSysEventRecordConvertor::ConvertDomain(const HiSysEventRecordCls& recordObj, HiSysEventRecordC& recordStruct)
30 {
31 return OHOS::HiviewDFX::StringUtil::CopyCString(recordStruct.domain, recordObj.GetDomain(),
32 MAX_LENGTH_OF_EVENT_DOMAIN - 1);
33 }
34
ConvertEventName(const HiSysEventRecordCls & recordObj,HiSysEventRecordC & recordStruct)35 int HiSysEventRecordConvertor::ConvertEventName(const HiSysEventRecordCls& recordObj, HiSysEventRecordC& recordStruct)
36 {
37 return OHOS::HiviewDFX::StringUtil::CopyCString(recordStruct.eventName, recordObj.GetEventName(),
38 MAX_LENGTH_OF_EVENT_NAME - 1);
39 }
40
ConvertTimeZone(const HiSysEventRecordCls & recordObj,HiSysEventRecordC & recordStruct)41 int HiSysEventRecordConvertor::ConvertTimeZone(const HiSysEventRecordCls& recordObj, HiSysEventRecordC& recordStruct)
42 {
43 return OHOS::HiviewDFX::StringUtil::CopyCString(recordStruct.tz, recordObj.GetTimeZone(),
44 MAX_LENGTH_OF_TIME_ZONE - 1);
45 }
46
ConvertLevel(const HiSysEventRecordCls & recordObj,HiSysEventRecordC & recordStruct)47 int HiSysEventRecordConvertor::ConvertLevel(const HiSysEventRecordCls& recordObj, HiSysEventRecordC& recordStruct)
48 {
49 return OHOS::HiviewDFX::StringUtil::CreateCString(&recordStruct.level, recordObj.GetLevel());
50 }
51
ConvertTag(const HiSysEventRecordCls & recordObj,HiSysEventRecordC & recordStruct)52 int HiSysEventRecordConvertor::ConvertTag(const HiSysEventRecordCls& recordObj, HiSysEventRecordC& recordStruct)
53 {
54 return OHOS::HiviewDFX::StringUtil::CreateCString(&recordStruct.tag, recordObj.GetTag());
55 }
56
ConvertJsonStr(const HiSysEventRecordCls & recordObj,HiSysEventRecordC & recordStruct)57 int HiSysEventRecordConvertor::ConvertJsonStr(const HiSysEventRecordCls& recordObj, HiSysEventRecordC& recordStruct)
58 {
59 constexpr size_t maxLen = 384 * 1024; // max length of the event is 384KB
60 return OHOS::HiviewDFX::StringUtil::CreateCString(&recordStruct.jsonStr, recordObj.AsJson(), maxLen);
61 }
62
InitRecord(HiSysEventRecordC & record)63 void HiSysEventRecordConvertor::InitRecord(HiSysEventRecordC& record)
64 {
65 OHOS::HiviewDFX::StringUtil::MemsetSafe(&record, sizeof(HiSysEventRecordC));
66 }
67
DeleteRecord(HiSysEventRecordC & record)68 void HiSysEventRecordConvertor::DeleteRecord(HiSysEventRecordC& record)
69 {
70 OHOS::HiviewDFX::StringUtil::DeletePointer<char>(&record.level);
71 OHOS::HiviewDFX::StringUtil::DeletePointer<char>(&record.tag);
72 OHOS::HiviewDFX::StringUtil::DeletePointer<char>(&record.jsonStr);
73 }
74
DeleteRecords(HiSysEventRecordC ** records,size_t len)75 void HiSysEventRecordConvertor::DeleteRecords(HiSysEventRecordC** records, size_t len)
76 {
77 if (records == nullptr || *records == nullptr) {
78 return;
79 }
80 auto realRs = *records;
81 for (size_t i = 0; i < len; i++) {
82 DeleteRecord(realRs[i]);
83 }
84 delete[] realRs;
85 realRs = nullptr;
86 }
87
ConvertRecord(const HiSysEventRecordCls & recordObj,HiSysEventRecordC & recordStruct)88 int HiSysEventRecordConvertor::ConvertRecord(const HiSysEventRecordCls& recordObj, HiSysEventRecordC& recordStruct)
89 {
90 if (int res = ConvertDomain(recordObj, recordStruct); res != 0) {
91 return res;
92 }
93 if (int res = ConvertEventName(recordObj, recordStruct); res != 0) {
94 HiLog::Error(LABEL, "Failed to covert name=%{public}s", recordObj.GetEventName().c_str());
95 return res;
96 }
97 recordStruct.type = HiSysEventEventType(recordObj.GetEventType());
98 recordStruct.time = recordObj.GetTime();
99 if (int res = ConvertTimeZone(recordObj, recordStruct); res != 0) {
100 HiLog::Error(LABEL, "Failed to covert tz=%{public}s", recordObj.GetTimeZone().c_str());
101 return res;
102 }
103 recordStruct.pid = recordObj.GetPid();
104 recordStruct.tid = recordObj.GetTid();
105 recordStruct.uid = recordObj.GetUid();
106 recordStruct.traceId = recordObj.GetTraceId();
107 recordStruct.spandId = recordObj.GetSpanId();
108 recordStruct.pspanId = recordObj.GetPspanId();
109 recordStruct.traceFlag = recordObj.GetTraceFlag();
110 if (int res = ConvertLevel(recordObj, recordStruct); res != 0) {
111 HiLog::Error(LABEL, "Failed to covert level=%{public}s", recordObj.GetLevel().c_str());
112 return res;
113 }
114 if (int res = ConvertTag(recordObj, recordStruct); res != 0) {
115 HiLog::Error(LABEL, "Failed to covert tag=%{public}s", recordObj.GetTag().c_str());
116 return res;
117 }
118 if (int res = ConvertJsonStr(recordObj, recordStruct); res != 0) {
119 HiLog::Error(LABEL, "Failed to covert jsonStr=%{public}s", recordObj.AsJson().c_str());
120 return res;
121 }
122 return 0;
123 }
124 }
125 }