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