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_convertor.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HISYSEVENT_C_QUERY" };
23 using HiSysEventRecordCls = OHOS::HiviewDFX::HiSysEventRecord;
24 using OHOS::HiviewDFX::HiLog;
25 using OHOS::HiviewDFX::HiSysEventRecordConvertor;
26 }
27
OnQuery(std::shared_ptr<std::vector<OHOS::HiviewDFX::HiSysEventRecord>> sysEvents)28 void HiSysEventQueryCallbackC::OnQuery(std::shared_ptr<std::vector<OHOS::HiviewDFX::HiSysEventRecord>> sysEvents)
29 {
30 if (onQuery_ == nullptr) {
31 HiLog::Error(LABEL, "OnQuery callback is null");
32 return;
33 }
34 if (sysEvents == nullptr || sysEvents->empty()) {
35 onQuery_(nullptr, 0);
36 return;
37 }
38 size_t size = sysEvents->size();
39 auto records = new(std::nothrow) HiSysEventRecordC[size];
40 for (size_t i = 0; i < size; i++) {
41 HiSysEventRecordConvertor::InitRecord(records[i]);
42 if (HiSysEventRecordConvertor::ConvertRecord(sysEvents->at(i), records[i]) != 0) {
43 HiLog::Error(LABEL, "Failed to covert record, index=%{public}zu, size=%{public}zu", i, size);
44 HiSysEventRecordConvertor::DeleteRecords(&records, i + 1); // +1 for release the current record
45 return;
46 }
47 }
48 onQuery_(records, size);
49 HiSysEventRecordConvertor::DeleteRecords(&records, size);
50 }
51
OnComplete(int32_t reason,int32_t total)52 void HiSysEventQueryCallbackC::OnComplete(int32_t reason, int32_t total)
53 {
54 if (onComplete_ == nullptr) {
55 HiLog::Error(LABEL, "OnComplete callback is null");
56 return;
57 }
58 onComplete_(reason, total);
59 }
60