1e41f4b71Sopenharmony_ci# HiSysEvent Query
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## Overview
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciHiSysEvent allows you to query system events by specifying search criteria. For example, for a power consumption module, you can query required system events for analysis.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## How to Develop
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci### Available APIs
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci#### C++ APIs
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciHiSysEvent query is implemented using the API provided by the **HiSysEventManager** class. For details, see the [API Header Files](/base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent_manager/include/).
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci> **NOTE**
18e41f4b71Sopenharmony_ci>
19e41f4b71Sopenharmony_ci> For details about **HiSysEventRecord** in the **OnQuery()** API of **HiSysEventQueryCallback**, see Table 4 in [HiSysEvent Listening](subsys-dfx-hisysevent-listening.md).
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci  **Table 1** Description of the C++ HiSysEvent query API
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci| Name| Description|
24e41f4b71Sopenharmony_ci| -------- | -------- |
25e41f4b71Sopenharmony_ci| int32_t Query(struct&nbsp;QueryArg&amp;&nbsp;arg,<br>std::vector&lt;QueryRule&gt;&amp;&nbsp;rules,<br>std::shared_ptr&lt;HiSysEventQueryCallback&gt;&nbsp;callback) | Queries system events by search criteria such as the time segment, event domain, and event name.<br>Input arguments:<br>- **arg**: event query parameter.<br>- **rules**: event filtering rules.<br>- **callback**: callback object for event query.<br>Return value:<br>- **0**: Query is successful.<br>- A negative value: Query has failed.|
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci  **Table 2** Description of QueryArg objects
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci| Field| Type| Description|
30e41f4b71Sopenharmony_ci| -------- | -------- | -------- |
31e41f4b71Sopenharmony_ci| beginTime | long long | Start time for query. The value is a Unix timestamp, in milliseconds.|
32e41f4b71Sopenharmony_ci| endTime | long long | End time for query. The value is a Unix timestamp, in milliseconds.|
33e41f4b71Sopenharmony_ci| maxEvents | int | Maximum number of returned events.|
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci   **Table 3** Description of EventType enums
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci| Event Type| Value| Description|
38e41f4b71Sopenharmony_ci| ------------ | ---- | ------------------ |
39e41f4b71Sopenharmony_ci| FAULT        | 1    | Fault type.    |
40e41f4b71Sopenharmony_ci| STATISTIC    | 2    | Statistical type.    |
41e41f4b71Sopenharmony_ci| SECURITY     | 3    | Security type.    |
42e41f4b71Sopenharmony_ci| BEHAVIOR     | 4    | User behavior type.|
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci  **Table 4** RuleType enum values
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci| Rule Type| Value| Description|
47e41f4b71Sopenharmony_ci| ------------ | ---- | ------------------ |
48e41f4b71Sopenharmony_ci| WHOLE_WORD   | 1    | Whole word matching.    |
49e41f4b71Sopenharmony_ci| PREFIX       | 2    | Prefix matching.    |
50e41f4b71Sopenharmony_ci| REGULAR      | 3    | Regular expression matching.    |
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci  **Table 5** Description of QueryRule objects
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci| Name| Description|
55e41f4b71Sopenharmony_ci| -------- | -------- |
56e41f4b71Sopenharmony_ci| QueryRule(const&nbsp;std::string&amp;&nbsp;domain,<br>const&nbsp;std::vector&lt;std::string&gt;&amp;&nbsp;eventList,<br>RuleType&nbsp;ruleType,<br>uint32_t&nbsp;eventType,<br>const&nbsp;std::string&&nbsp;cond) | Constructor used to create a **QueryRule** object.<br>Input arguments:<br>- **domain**: domain to which the event of the **QueryRule** object belongs, in the string format. By default, an empty string indicates that the domain is successfully matched.<br>**eventList**: event name list, in the **std::vector&lt;std::string&gt;** format. By default, an empty string indicates that the event names on the list are successfully matched.<br>- **ruleType**: rule type. For details, see Table 4.<br>- **eventType**: system event type. The value is of the uint32_t type. For details about the system event types, see Table 3. When **eventType** is set to **0**, all types of events are queried.<br>- **cond**: query criteria. The value is of the string type.|
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ciThe **condition** parameter must be in the specified JSON string format. For example:
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci    ```json
61e41f4b71Sopenharmony_ci    {
62e41f4b71Sopenharmony_ci        "version":"V1",
63e41f4b71Sopenharmony_ci        "condition":{
64e41f4b71Sopenharmony_ci            "and":[
65e41f4b71Sopenharmony_ci                {"param":"type_","op":">","value":0},
66e41f4b71Sopenharmony_ci                {"param":"uid_","op":"=","value":1201}
67e41f4b71Sopenharmony_ci            ]
68e41f4b71Sopenharmony_ci        }
69e41f4b71Sopenharmony_ci    }
70e41f4b71Sopenharmony_ci    ```
71e41f4b71Sopenharmony_ci- The **version** field is mandatory, indicating the supported version of the input condition. Currently, only **V1** is supported.
72e41f4b71Sopenharmony_ci- The **condition** field is mandatory, indicating the input condition.
73e41f4b71Sopenharmony_ci  - The **and** field is optional, indicating the AND relationship between conditions.
74e41f4b71Sopenharmony_ci    - The **param** field is mandatory, indicating the parameter name for condition matching. The value must be a string.
75e41f4b71Sopenharmony_ci    - The **op** field is mandatory, indicating the parameter comparison operator for condition matching. The value must be a string. Supported comparison operators include the following: =, >, <, >=, and <=.
76e41f4b71Sopenharmony_ci    - The **value** field is mandatory, indicating the parameter value for condition matching. The value must be a string or an integer.
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci**Table 6** Description of HiSysEventQueryCallback objects
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci| Name| Description|
85e41f4b71Sopenharmony_ci| -------- | -------- |
86e41f4b71Sopenharmony_ci| void&nbsp;HiSysEventQueryCallback::OnQuery(std::shared_ptr&lt;std::vector&lt;HiSysEventRecord&gt;&gt;&nbsp;sysEvents) | Callback object for event query.<br>Input arguments:<br>- **sysEvents**: event list.|
87e41f4b71Sopenharmony_ci| void&nbsp;HiSysEventQueryCallback::OnComplete(int32_t&nbsp;reason,&nbsp;int32_t&nbsp;total) | Callback object for completion of event query.<br>Input arguments:<br>- **reason**: reason for completion of event query. The value **0** indicates that the query is normal, and any other value indicates that the query has failed.<br>- **total**: total number of events returned in this query.|
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci#### C APIs
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ciHiSysEvent query is implemented using the API provided in the following table. For details, see the [API Header Files](/base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent_manager/include/).
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci  **Table 7** Description of the C HiSysEvent query API
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci| Name                                                    | Description                                                        |
96e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ |
97e41f4b71Sopenharmony_ci| int OH_HiSysEvent_Query(const HiSysEventQueryArg& arg, HiSysEventQueryRule rules[], size_t ruleSize, HiSysEventQueryCallback& callback); | Queries system events by search criteria such as the time segment, event domain, event name, and event parameter.<br>Input arguments:<br>- **arg**: event query parameter.<br>- **rules**: event filtering rules.<br>- **ruleSize**: number of event filtering rules.<br>- **callback**: callback object for event query.<br>Return value:<br>- **0**: Query is successful.<br>- A negative value: Query has failed.|
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci  **Table 8** Description of the HiSysEventQueryArg structure
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci| Field | Type| Description                                                |
102e41f4b71Sopenharmony_ci| --------- | -------- | ---------------------------------------------------- |
103e41f4b71Sopenharmony_ci| beginTime | int64_t  | Start time for query. The value is a Unix timestamp, in milliseconds.|
104e41f4b71Sopenharmony_ci| endTime   | int64_t  | End time for query. The value is a Unix timestamp, in milliseconds.|
105e41f4b71Sopenharmony_ci| maxEvents | int32_t  | Maximum number of returned events.                    |
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci  **Table 9** Description of the HiSysEventQueryRule structure
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci| Field     | Type | Description                              |
110e41f4b71Sopenharmony_ci| ------------- | --------- | ---------------------------------- |
111e41f4b71Sopenharmony_ci| domain        | char[]    | Event domain.          |
112e41f4b71Sopenharmony_ci| eventList     | char\[][] | Event name list.      |
113e41f4b71Sopenharmony_ci| eventListSize | size_t    | Size of the event name list.  |
114e41f4b71Sopenharmony_ci| condition     | char*     | Custom event parameter conditions for the query.|
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci  **Table 10** Description of the HiSysEventQueryCallback structure
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci| Field  | Type                                          | Description                                                        |
119e41f4b71Sopenharmony_ci| ---------- | -------------------------------------------------- | ------------------------------------------------------------ |
120e41f4b71Sopenharmony_ci| OnQuery    | void (*)(HiSysEventRecord records[], size_t size); | Callback object for event query.<br>Input arguments:<br>- **records**: event list.<br>- **size**: size of the event list.|
121e41f4b71Sopenharmony_ci| OnComplete | void (*)(int32_t reason, int32_t total);           | Callback object for completion of event query.<br>Input arguments:<br>- **reason**: reason for completion of event query. The value **0** indicates that the query is normal, and any other value indicates that the query has failed.<br>- **total**: total number of events returned in this query.|
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci  **Table 11** Description of the HiSysEventRecord event structure
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci| Field | Type           | Description                      |
126e41f4b71Sopenharmony_ci| --------- | ------------------- | -------------------------- |
127e41f4b71Sopenharmony_ci| domain    | char[]              | Event domain.          |
128e41f4b71Sopenharmony_ci| eventName | char\[]             | Event name.              |
129e41f4b71Sopenharmony_ci| type      | HiSysEventEventType | Event type.              |
130e41f4b71Sopenharmony_ci| time      | uint64_t            | Event timestamp.            |
131e41f4b71Sopenharmony_ci| tz        | char\[]             | Event time zone.              |
132e41f4b71Sopenharmony_ci| pid       | int64_t             | Process ID of the event.            |
133e41f4b71Sopenharmony_ci| tid       | int64_t             | Thread ID of the event.            |
134e41f4b71Sopenharmony_ci| uid       | int64_t             | User ID of the event.            |
135e41f4b71Sopenharmony_ci| traceId   | uint64_t            | Distributed call chain trace ID of the event.    |
136e41f4b71Sopenharmony_ci| spandId   | uint64_t            | Span ID for the distributed call chain trace of the event.  |
137e41f4b71Sopenharmony_ci| pspanId   | uint64_t            | Parent span ID for the distributed call chain trace of the event.|
138e41f4b71Sopenharmony_ci| traceFlag | int                 | Distributed call chain trace flag of the event.  |
139e41f4b71Sopenharmony_ci| level     | char*               | Event level.              |
140e41f4b71Sopenharmony_ci| tag       | char*               | Event tag.              |
141e41f4b71Sopenharmony_ci| jsonStr   | char*               | Event content.              |
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci  **Table 12** Description of HiSysEventRecord APIs
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci| Name                                                    |                                                              |
146e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ |
147e41f4b71Sopenharmony_ci| void OH_HiSysEvent_GetParamNames(const HiSysEventRecord& record, char*** params, size_t& len); | Obtains all parameter names of an event.<br>Input arguments:<br>- **record**: event structure.<br>- **params**: parameter name array.<br>- **len**: size of the parameter name array.|
148e41f4b71Sopenharmony_ci| int OH_HiSysEvent_GetParamInt64Value(const HiSysEventRecord& record, const char* name, int64_t& value); | Parses the parameter value in the event to an int64_t value and assigns the value to **value**.<br>Input arguments:<br>- **record**: event structure.<br>- **name**: parameter name.<br>- **value**: parameter value of the int64_t type.|
149e41f4b71Sopenharmony_ci| int OH_HiSysEvent_GetParamUint64Value(const HiSysEventRecord& record, const char* name, uint64_t& value); | Parses the parameter value in the event to a uint64_t value and assigns the value to **value**.<br>Input arguments:<br>- **record**: event structure.<br>- **name**: parameter name.<br>- **value**: parameter value of the uint64_t type.|
150e41f4b71Sopenharmony_ci| int OH_HiSysEvent_GetParamDoubleValue(const HiSysEventRecord& record, const char* name, double& value); | Parses the parameter value in the event to a double value and assigns the value to **value**.<br>Input arguments:<br>- **record**: event structure.<br>- **name**: parameter name.<br>- **value**: parameter value of the double type.|
151e41f4b71Sopenharmony_ci| int OH_HiSysEvent_GetParamStringValue(const HiSysEventRecord& record, const char* name, char** value); | Parses the parameter value in the event to a char array value and assigns the value to **value**. You need to release the memory manually after usage.<br>Input arguments:<br>- **record**: event structure.<br>- **name**: parameter name.<br>- **value**: char\* reference.|
152e41f4b71Sopenharmony_ci| int OH_HiSysEvent_GetParamInt64Values(const HiSysEventRecord& record, const char* name, int64_t** value, size_t& len); | Parses the parameter value in the event to a int64_t array value and assigns the value to **value**. You need to release the memory manually after usage.<br>Input arguments:<br>- **record**: event structure.<br>- **name**: parameter name.<br>- **value**: int64_t\* reference.<br>- **len**: array size.|
153e41f4b71Sopenharmony_ci| int OH_HiSysEvent_GetParamUint64Values(const HiSysEventRecord& record, const char* name, uint64_t** value, size_t& len); | Parses the parameter value in the event to a uint64_t array value and assigns the value to **value**. You need to release the memory manually after usage.<br>Input arguments:<br>- **record**: event structure.<br>- **name**: parameter name.<br>- **value**: uint64_t\* reference.<br>- **len**: array size.|
154e41f4b71Sopenharmony_ci| int OH_HiSysEvent_GetParamDoubleValues(const HiSysEventRecord& record, const char* name, double** value, size_t& len); | Parses the parameter value in the event to a double array value and assigns the value to **value**. You need to release the memory manually after usage.<br>Input arguments:<br>- **record**: event structure.<br>- **name**: parameter name.<br>- **value**: double\* reference.<br>- **len**: array size.|
155e41f4b71Sopenharmony_ci| int OH_HiSysEvent_GetParamStringValues(const HiSysEventRecord& record, const char* name, char*** value, size_t& len); | Parses the parameter value in the event to a char* array value and assigns the value to **value**. You need to release the memory manually after usage.<br>Input arguments:<br>- **record**: event structure.<br>- **name**: parameter name.<br>- **value**: char\*\* reference.<br>- **len**: array size.|
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ciThe return values of the HiSysEventRecord APIs are described as follows:
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci- **0**: The parsing is successful.
160e41f4b71Sopenharmony_ci- -**1**: The event fails to be initialized.
161e41f4b71Sopenharmony_ci- -**2**: The parameter name does not exist.
162e41f4b71Sopenharmony_ci- -**3**: The type of the parameter value to be parsed does not match the type of the input parameter value.
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci### How to Develop
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci#### C++ HiSysEvent Query
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci1. Import the corresponding header file.
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci    ```c++
171e41f4b71Sopenharmony_ci    #include "hisysevent_manager.h"
172e41f4b71Sopenharmony_ci    ```     
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci2. Implement the callback API for the service domain.
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci    ```c++
177e41f4b71Sopenharmony_ci    class TestQueryCallback : public HiSysEventQueryCallback {
178e41f4b71Sopenharmony_ci    public:
179e41f4b71Sopenharmony_ci        void OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents) override
180e41f4b71Sopenharmony_ci        {
181e41f4b71Sopenharmony_ci            if (sysEvents == nullptr) {
182e41f4b71Sopenharmony_ci                return;
183e41f4b71Sopenharmony_ci            }
184e41f4b71Sopenharmony_ci            for_each((*sysEvents).cbegin(), (*sysEvents).cend(), [](const HiSysEventRecord& event) {
185e41f4b71Sopenharmony_ci                std::cout << event.AsJson() << std::endl;
186e41f4b71Sopenharmony_ci            });
187e41f4b71Sopenharmony_ci        }
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci        void OnComplete(int32_t reason, int32_t total) override
190e41f4b71Sopenharmony_ci        {
191e41f4b71Sopenharmony_ci            std::cout << "Query completed" << std::endl;
192e41f4b71Sopenharmony_ci            return;
193e41f4b71Sopenharmony_ci        }
194e41f4b71Sopenharmony_ci    };
195e41f4b71Sopenharmony_ci    ```
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci3. Call the query API while passing in **QueryArg**, **QueryRule**, and **QueryCallback**.
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci    ```c++
200e41f4b71Sopenharmony_ci    // Create a QueryArg object.
201e41f4b71Sopenharmony_ci    long long startTime = 0;
202e41f4b71Sopenharmony_ci    long long endTime = 1668245644000; //2022-11-12 09:34:04
203e41f4b71Sopenharmony_ci    int queryCount = 10;
204e41f4b71Sopenharmony_ci    QueryArg arg(startTime, endTime, queryCount);
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci    // Create a QueryRule object.
207e41f4b71Sopenharmony_ci    QueryRule rule("HIVIEWDFX", { "PLUGIN_LOAD" });
208e41f4b71Sopenharmony_ci    std::vector<QueryRule> queryRules = { rule };
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci    // Create a QueryCallback object.
211e41f4b71Sopenharmony_ci    auto queryCallback = std::make_shared<TestQueryCallback>();
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci    // Call the query API.
214e41f4b71Sopenharmony_ci    HiSysEventManager::Query(arg, queryRules, queryCallback);
215e41f4b71Sopenharmony_ci    ```
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci#### C HiSysEvent Query
218e41f4b71Sopenharmony_ci
219e41f4b71Sopenharmony_ci1. Import the corresponding header file.
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci    ```c++
222e41f4b71Sopenharmony_ci    #include "hisysevent_manager_c.h"
223e41f4b71Sopenharmony_ci    ```
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci2. Implement the callback API for the service domain.
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci    ```c++
228e41f4b71Sopenharmony_ci    void OnQueryTest(HiSysEventRecord records[], size_t size)
229e41f4b71Sopenharmony_ci    {
230e41f4b71Sopenharmony_ci        for (size_t i = 0; i < size; i++) {
231e41f4b71Sopenharmony_ci            printf("OnQuery: event=%s", records[i].jsonStr);
232e41f4b71Sopenharmony_ci        }
233e41f4b71Sopenharmony_ci    }
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ci    void OnCompleteTest(int32_t reason, int32_t total)
236e41f4b71Sopenharmony_ci    {
237e41f4b71Sopenharmony_ci        printf("OnCompleted, res=%d, total=%d\n", reason, total);
238e41f4b71Sopenharmony_ci    }
239e41f4b71Sopenharmony_ci    ```
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_ci3. Call the query API while passing in **HiSysEventQueryArg**, **HiSysEventQueryRule**, and **HiSysEventQueryCallback**.
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci    ```c++
244e41f4b71Sopenharmony_ci    // Create a HiSysEventQueryArg object.
245e41f4b71Sopenharmony_ci    HiSysEventQueryArg arg;
246e41f4b71Sopenharmony_ci    arg.beginTime = 0;
247e41f4b71Sopenharmony_ci    arg.endTime = 1668245644000; //2022-11-12 09:34:04
248e41f4b71Sopenharmony_ci    arg.maxEvents = 10;
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci    // Create a HiSysEventQueryRule object.
251e41f4b71Sopenharmony_ci    constexpr char TEST_DOMAIN[] = "HIVIEWDFX";
252e41f4b71Sopenharmony_ci    constexpr char TEST_NAME[] = "PLUGIN_LOAD";
253e41f4b71Sopenharmony_ci    HiSysEventQueryRule rule;
254e41f4b71Sopenharmony_ci    (void)strcpy_s(rule.domain, strlen(TEST_DOMAIN) + 1, TEST_DOMAIN);
255e41f4b71Sopenharmony_ci    (void)strcpy_s(rule.eventList[0], strlen(TEST_NAME) + 1, TEST_NAME);
256e41f4b71Sopenharmony_ci    rule.eventListSize = 1;
257e41f4b71Sopenharmony_ci    rule.condition = nullptr;
258e41f4b71Sopenharmony_ci    HiSysEventQueryRule rules[] = { rule };
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci    // Create a HiSysEventQueryCallback object.
261e41f4b71Sopenharmony_ci    HiSysEventQueryCallback callback;
262e41f4b71Sopenharmony_ci    callback.OnQuery = OnQueryTest;
263e41f4b71Sopenharmony_ci    callback.OnComplete = OnCompleteTest;
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci    // Call the query API.
266e41f4b71Sopenharmony_ci    OH_HiSysEvent_Query(arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), callback);
267e41f4b71Sopenharmony_ci    ```
268e41f4b71Sopenharmony_ci
269e41f4b71Sopenharmony_ci### Development Examples
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci#### C++ HiSysEvent Query
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ciSuppose you want to query all events generated up to the current time with domain being **HIVIEWDFX** and name being **PLUGIN_LOAD**. The sample code is as follows:
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci1. Add the **libhisysevent** and **libhisyseventmanager** dependencies of the **hisysevent** component to the **BUILD.gn** file of the service module.
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ci    ```c++
278e41f4b71Sopenharmony_ci    external_deps = [
279e41f4b71Sopenharmony_ci      "hisysevent:libhisysevent",
280e41f4b71Sopenharmony_ci      "hisysevent:libhisyseventmanager",
281e41f4b71Sopenharmony_ci    ]
282e41f4b71Sopenharmony_ci    ```
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci2. Call the query API in the **TestQuery()** function of the service module.
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci    ```c++
287e41f4b71Sopenharmony_ci    #include "hisysevent_manager.h"
288e41f4b71Sopenharmony_ci    #include <iostream>
289e41f4b71Sopenharmony_ci    #include <unistd.h>
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci    using namespace OHOS::HiviewDFX;
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ci    class TestQueryCallback : public HiSysEventQueryCallback {
294e41f4b71Sopenharmony_ci    public:
295e41f4b71Sopenharmony_ci        void OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents) override
296e41f4b71Sopenharmony_ci        {
297e41f4b71Sopenharmony_ci            if (sysEvents == nullptr) {
298e41f4b71Sopenharmony_ci                return;
299e41f4b71Sopenharmony_ci            }
300e41f4b71Sopenharmony_ci            for_each((*sysEvents).cbegin(), (*sysEvents).cend(), [](const HiSysEventRecord& event) {
301e41f4b71Sopenharmony_ci                std::cout << event.AsJson() << std::endl;
302e41f4b71Sopenharmony_ci            });
303e41f4b71Sopenharmony_ci        }
304e41f4b71Sopenharmony_ci
305e41f4b71Sopenharmony_ci        void OnComplete(int32_t reason, int32_t total) override
306e41f4b71Sopenharmony_ci        {
307e41f4b71Sopenharmony_ci            std::cout << "Query completed" << std::endl;
308e41f4b71Sopenharmony_ci            return;
309e41f4b71Sopenharmony_ci        }
310e41f4b71Sopenharmony_ci    };
311e41f4b71Sopenharmony_ci
312e41f4b71Sopenharmony_ci    int64_t GetMilliseconds()
313e41f4b71Sopenharmony_ci    {
314e41f4b71Sopenharmony_ci        auto now = std::chrono::system_clock::now();
315e41f4b71Sopenharmony_ci        auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
316e41f4b71Sopenharmony_ci        return millisecs.count();
317e41f4b71Sopenharmony_ci    }
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci    void TestQuery()
320e41f4b71Sopenharmony_ci    {
321e41f4b71Sopenharmony_ci        long long startTime = 0;
322e41f4b71Sopenharmony_ci        long long endTime = GetMilliseconds();
323e41f4b71Sopenharmony_ci        int maxEvents = 100;
324e41f4b71Sopenharmony_ci        QueryArg arg(startTime, endTime, maxEvents);
325e41f4b71Sopenharmony_ci        QueryRule rule("HIVIEWDFX", { "PLUGIN_LOAD" });
326e41f4b71Sopenharmony_ci        std::vector<QueryRule> queryRules = { rule };
327e41f4b71Sopenharmony_ci        auto queryCallback = std::make_shared<TestQueryCallback>();
328e41f4b71Sopenharmony_ci        int ret = HiSysEventManager::Query(arg, queryRules, queryCallback);
329e41f4b71Sopenharmony_ci    }
330e41f4b71Sopenharmony_ci    ```
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci#### C HiSysEvent Query
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ciSuppose you want to query all events generated up to the current time with domain being **HIVIEWDFX** and name being **PLUGIN_LOAD**. The sample code is as follows:
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_ci1. Add the **libhisyseventmanager** dependency of the **hisysevent** component to the **BUILD.gn** file of the service module.
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_ci```c++
339e41f4b71Sopenharmony_ci    external_deps = [ "hisysevent:libhisyseventmanager" ]
340e41f4b71Sopenharmony_ci
341e41f4b71Sopenharmony_ci    // for strcpy_s
342e41f4b71Sopenharmony_ci    deps = [ "//third_party/bounds_checking_function:libsec_shared" ]
343e41f4b71Sopenharmony_ci```
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci2. Call the query API in the **TestQuery()** function of the service module.
346e41f4b71Sopenharmony_ci
347e41f4b71Sopenharmony_ci    ```c++
348e41f4b71Sopenharmony_ci    #include "hisysevent_manager_c.h"
349e41f4b71Sopenharmony_ci    #include <securec.h>
350e41f4b71Sopenharmony_ci    #include <time.h>
351e41f4b71Sopenharmony_ci     
352e41f4b71Sopenharmony_ci    void OnQueryTest(HiSysEventRecord records[], size_t size)
353e41f4b71Sopenharmony_ci    {
354e41f4b71Sopenharmony_ci        for (size_t i = 0; i < size; i++) {
355e41f4b71Sopenharmony_ci            printf("OnQuery: event=%s", records[i].jsonStr);
356e41f4b71Sopenharmony_ci        }
357e41f4b71Sopenharmony_ci    }
358e41f4b71Sopenharmony_ci
359e41f4b71Sopenharmony_ci    void OnCompleteTest(int32_t reason, int32_t total)
360e41f4b71Sopenharmony_ci    {
361e41f4b71Sopenharmony_ci        printf("OnCompleted, res=%d, total=%d\n", reason, total);
362e41f4b71Sopenharmony_ci    }
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci    int64_t GetMilliseconds()
365e41f4b71Sopenharmony_ci    {
366e41f4b71Sopenharmony_ci        return time(NULL);
367e41f4b71Sopenharmony_ci    }
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci    void TestQuery()
370e41f4b71Sopenharmony_ci    {
371e41f4b71Sopenharmony_ci        HiSysEventQueryArg arg;
372e41f4b71Sopenharmony_ci        arg.beginTime = 0;
373e41f4b71Sopenharmony_ci        arg.endTime = GetMilliseconds();
374e41f4b71Sopenharmony_ci        arg.maxEvents = 100;
375e41f4b71Sopenharmony_ci        constexpr char TEST_DOMAIN[] = "HIVIEWDFX";
376e41f4b71Sopenharmony_ci        constexpr char TEST_NAME[] = "PLUGIN_LOAD";
377e41f4b71Sopenharmony_ci        HiSysEventQueryRule rule;
378e41f4b71Sopenharmony_ci        (void)strcpy_s(rule.domain, strlen(TEST_DOMAIN) + 1, TEST_DOMAIN);
379e41f4b71Sopenharmony_ci        (void)strcpy_s(rule.eventList[0], strlen(TEST_NAME) + 1, TEST_NAME);
380e41f4b71Sopenharmony_ci        rule.eventListSize = 1;
381e41f4b71Sopenharmony_ci        rule.condition = nullptr;
382e41f4b71Sopenharmony_ci        HiSysEventQueryRule rules[] = { rule };
383e41f4b71Sopenharmony_ci        HiSysEventQueryCallback callback;
384e41f4b71Sopenharmony_ci        callback.OnQuery = OnQueryTest;
385e41f4b71Sopenharmony_ci        callback.OnComplete = OnCompleteTest;
386e41f4b71Sopenharmony_ci        int ret = OH_HiSysEvent_Query(arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), callback);
387e41f4b71Sopenharmony_ci    }
388e41f4b71Sopenharmony_ci    ```
389