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_record_c.h"
17
18 #include "hisysevent_record.h"
19 #include "string_util.h"
20
21 namespace {
22 using HiSysEventRecordCls = OHOS::HiviewDFX::HiSysEventRecord;
23 constexpr int ERR_NULL = -1;
24
25 template <typename T>
GetParamValue(const HiSysEventRecordC & record,const char * name,T & value)26 int GetParamValue(const HiSysEventRecordC& record, const char* name, T& value)
27 {
28 if (record.jsonStr == nullptr || name == nullptr) {
29 return ERR_NULL;
30 }
31 HiSysEventRecordCls recordObj(record.jsonStr);
32 return recordObj.GetParamValue(name, value);
33 }
34
GetParamValue(const HiSysEventRecordC & record,const char * name,char ** value)35 int GetParamValue(const HiSysEventRecordC& record, const char* name, char** value)
36 {
37 if (record.jsonStr == nullptr || name == nullptr) {
38 return ERR_NULL;
39 }
40 HiSysEventRecordCls recordObj(record.jsonStr);
41 std::string str;
42 if (auto res = recordObj.GetParamValue(name, str); res != 0) {
43 return res;
44 }
45 return OHOS::HiviewDFX::StringUtil::ConvertCString(str, value);
46 }
47
48 template <typename T>
GetParamValues(const HiSysEventRecordC & record,const char * name,T ** value,size_t & len)49 int GetParamValues(const HiSysEventRecordC& record, const char* name, T** value, size_t& len)
50 {
51 if (record.jsonStr == nullptr || name == nullptr) {
52 return ERR_NULL;
53 }
54 HiSysEventRecordCls recordObj(record.jsonStr);
55 std::vector<T> dataVec;
56 if (auto res = recordObj.GetParamValue(name, dataVec); res != 0) {
57 return res;
58 }
59 if (dataVec.empty()) {
60 return 0;
61 }
62 len = dataVec.size();
63 T* data = new(std::nothrow) T[len];
64 for (size_t i = 0; i < len; i++) {
65 data[i] = dataVec[i];
66 }
67 *value = data;
68 return 0;
69 }
70
GetParamValues(const HiSysEventRecordC & record,const char * name,char *** value,size_t & len)71 int GetParamValues(const HiSysEventRecordC& record, const char* name, char*** value, size_t& len)
72 {
73 if (record.jsonStr == nullptr || name == nullptr) {
74 return ERR_NULL;
75 }
76 HiSysEventRecordCls recordObj(record.jsonStr);
77 std::vector<std::string> dataVec;
78 if (auto res = recordObj.GetParamValue(name, dataVec); res != 0) {
79 return res;
80 }
81 if (dataVec.empty()) {
82 return 0;
83 }
84 return OHOS::HiviewDFX::StringUtil::ConvertCStringVec(dataVec, value, len);
85 }
86
GetParamNames(const HiSysEventRecordC & record,char *** names,size_t & len)87 int GetParamNames(const HiSysEventRecordC& record, char*** names, size_t& len)
88 {
89 if (record.jsonStr == nullptr) {
90 return ERR_NULL;
91 }
92 HiSysEventRecordCls recordObj(record.jsonStr);
93 std::vector<std::string> dataVec;
94 recordObj.GetParamNames(dataVec);
95 if (dataVec.empty()) {
96 return 0;
97 }
98 return OHOS::HiviewDFX::StringUtil::ConvertCStringVec(dataVec, names, len);
99 }
100
GetParamInt64Value(const HiSysEventRecordC & record,const char * name,int64_t & value)101 int GetParamInt64Value(const HiSysEventRecordC& record, const char* name, int64_t& value)
102 {
103 return GetParamValue<int64_t>(record, name, value);
104 }
105
GetParamUint64Value(const HiSysEventRecordC & record,const char * name,uint64_t & value)106 int GetParamUint64Value(const HiSysEventRecordC& record, const char* name, uint64_t& value)
107 {
108 return GetParamValue<uint64_t>(record, name, value);
109 }
110
GetParamDoubleValue(const HiSysEventRecordC & record,const char * name,double & value)111 int GetParamDoubleValue(const HiSysEventRecordC& record, const char* name, double& value)
112 {
113 return GetParamValue<double>(record, name, value);
114 }
115
GetParamStringValue(const HiSysEventRecordC & record,const char * name,char ** value)116 int GetParamStringValue(const HiSysEventRecordC& record, const char* name, char** value)
117 {
118 return GetParamValue(record, name, value);
119 }
120
GetParamInt64Values(const HiSysEventRecordC & record,const char * name,int64_t ** value,size_t & len)121 int GetParamInt64Values(const HiSysEventRecordC& record, const char* name, int64_t** value, size_t& len)
122 {
123 return GetParamValues<int64_t>(record, name, value, len);
124 }
125
GetParamUint64Values(const HiSysEventRecordC & record,const char * name,uint64_t ** value,size_t & len)126 int GetParamUint64Values(const HiSysEventRecordC& record, const char* name, uint64_t** value, size_t& len)
127 {
128 return GetParamValues<uint64_t>(record, name, value, len);
129 }
130
GetParamDoubleValues(const HiSysEventRecordC & record,const char * name,double ** value,size_t & len)131 int GetParamDoubleValues(const HiSysEventRecordC& record, const char* name, double** value, size_t& len)
132 {
133 return GetParamValues<double>(record, name, value, len);
134 }
135
GetParamStringValues(const HiSysEventRecordC & record,const char * name,char *** value,size_t & len)136 int GetParamStringValues(const HiSysEventRecordC& record, const char* name, char*** value, size_t& len)
137 {
138 return GetParamValues(record, name, value, len);
139 }
140 }
141
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
145
OH_HiSysEvent_GetParamNames(const HiSysEventRecordC * record,char *** names,size_t * len)146 void OH_HiSysEvent_GetParamNames(const HiSysEventRecordC* record, char*** names, size_t* len)
147 {
148 GetParamNames(*record, names, *len);
149 }
150
OH_HiSysEvent_GetParamInt64Value(const HiSysEventRecordC * record,const char * name,int64_t * value)151 int OH_HiSysEvent_GetParamInt64Value(const HiSysEventRecordC* record, const char* name, int64_t* value)
152 {
153 return GetParamInt64Value(*record, name, *value);
154 }
155
OH_HiSysEvent_GetParamUint64Value(const HiSysEventRecordC * record,const char * name,uint64_t * value)156 int OH_HiSysEvent_GetParamUint64Value(const HiSysEventRecordC* record, const char* name, uint64_t* value)
157 {
158 return GetParamUint64Value(*record, name, *value);
159 }
160
OH_HiSysEvent_GetParamDoubleValue(const HiSysEventRecordC * record,const char * name,double * value)161 int OH_HiSysEvent_GetParamDoubleValue(const HiSysEventRecordC* record, const char* name, double* value)
162 {
163 return GetParamDoubleValue(*record, name, *value);
164 }
165
OH_HiSysEvent_GetParamStringValue(const HiSysEventRecordC * record,const char * name,char ** value)166 int OH_HiSysEvent_GetParamStringValue(const HiSysEventRecordC* record, const char* name, char** value)
167 {
168 return GetParamStringValue(*record, name, value);
169 }
170
OH_HiSysEvent_GetParamInt64Values(const HiSysEventRecordC * record,const char * name,int64_t ** value,size_t * len)171 int OH_HiSysEvent_GetParamInt64Values(const HiSysEventRecordC* record, const char* name, int64_t** value, size_t* len)
172 {
173 return GetParamInt64Values(*record, name, value, *len);
174 }
175
OH_HiSysEvent_GetParamUint64Values(const HiSysEventRecordC * record,const char * name,uint64_t ** value,size_t * len)176 int OH_HiSysEvent_GetParamUint64Values(const HiSysEventRecordC* record, const char* name, uint64_t** value, size_t* len)
177 {
178 return GetParamUint64Values(*record, name, value, *len);
179 }
180
OH_HiSysEvent_GetParamDoubleValues(const HiSysEventRecordC * record,const char * name,double ** value,size_t * len)181 int OH_HiSysEvent_GetParamDoubleValues(const HiSysEventRecordC* record, const char* name, double** value, size_t* len)
182 {
183 return GetParamDoubleValues(*record, name, value, *len);
184 }
185
OH_HiSysEvent_GetParamStringValues(const HiSysEventRecordC * record,const char * name,char *** value,size_t * len)186 int OH_HiSysEvent_GetParamStringValues(const HiSysEventRecordC* record, const char* name, char*** value, size_t* len)
187 {
188 return GetParamStringValues(*record, name, value, *len);
189 }
190 #ifdef __cplusplus
191 }
192 #endif
193