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