1# HiSysEvent Query<a name="EN-US_TOPIC_0000001231455461"></a> 2 3## Overview<a name="section279684125212"></a> 4 5HiSysEvent provides an API for you to query system events. You can query concerned events by specifying search criteria. For example, for a power consumption module, you can query required system events for analysis. 6 7## Development Guidelines<a name="section315316761113"></a> 8 9### Available APIs<a name="section03869128521"></a> 10 11**Table 1** Description of the HiSysEvent query API 12 13| Name| Description | 14| -------- | --------- | 15| bool HiSysEventManager::QueryHiSysEvent(struct QueryArg& queryArg, std::vector<QueryRule>& queryRules, std::shared_ptr<HiSysEventQueryCallBack> queryCallBack) | Queries system events by specifying search criteria such as the time segment, event domain, and event name.<br><br>Input arguments:<ul><li>**queryArg**: event query parameter. </li><li>**queryRules**: event filtering rules. </li><li>**queryRules**: callback object for query results. </li></ul>Return value:<ul><li>**true**: The query is successful. </li><li>**false**: The query has failed.</li></ul> | 16 17 18**Table 2** Description of QueryArg 19 20| Attribute| Description | 21| -------- | --------- | 22| beginTime | Start time, in the **long long int** format.| 23| endTime | End time, in the **long long int** format.| 24| maxEvents | Maximum number of returned events, in the **int** format.| 25 26**Table 3** Description of QueryRule 27 28| API| Description | 29| -------- | --------- | 30| QueryRule(const std::string& domain, const std::vector<std::string>& eventList) | Constructor used to create a **QueryRule** object.<br><br>Input arguments:<ul><li>**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. </li><li>**eventList**: event name list, in the **std::vector<std::string>** format. By default, an empty string indicates that the event names on the list are successfully matched.</li></ul> | 31 32**Table 4** Description of HiSysEventQueryCallBack 33 34| API| Description | 35| -------- | --------- | 36| void HiSysEventQueryCallBack::OnQuery(const ::std::vector<std::string>& sysEvent, const ::std::vector<int64_t>& seq) | Callback object for event query.<br><br>Input arguments:<ul><li>**sysEvent**: event set. </li><li>**seq**: event sequence set. </li></ul>Return value:<br>None.| 37| void HiSysEventQueryCallBack::OnComplete(int32_t reason, int32_t total) | Callback object for completion of event query.<br><br>Input arguments:<ul><li>**reason**: reason for completion of event query. The default value is **0**. </li><li>**total**: total number of events returned in this query. </li></ul>Return value:<br>None.| 38 39### Development Example<a name="section14286111855212"></a> 40 41C++ 42 431. Develop the source code. 44 45 - Import the corresponding header file: 46 47 hisysevent\_manager.h 48 49 - Implement the callback API. 50 51 void HiSysEventQueryCallBack::OnQuery\(const ::std::vector<std::string>& sysEvent, const ::std::vector<int64\_t\>& seq\) 52 53 void HiSysEventQueryCallBack::OnComplete\(int32\_t reason, int32\_t total\) 54 55 - Invoke the query API in the corresponding service logic. 56 57 HiSysEventManager::QueryHiSysEvent\(struct QueryArg& queryArg, std::vector<QueryRule\>& queryRules, std::shared\_ptr<HiSysEventQueryCallBack\> queryCallBack\) 58 59 60 ``` 61 // In this example, you'll query all system events. 62 #include "hisysevent_manager.h" 63 #include <iostream> 64 65 namespace OHOS { 66 namespace HiviewDFX { 67 // Implement the query callback API. 68 void HiSysEventToolQuery::OnQuery(const ::std::vector<std::string>& sysEvent, 69 const ::std::vector<int64_t>& seq) 70 { 71 for_each(sysEvent.cbegin(), sysEvent.cend(), [](const std::string &tmp) { 72 std::cout << tmp << std::endl; 73 }); 74 } 75 76 void HiSysEventToolQuery::OnComplete(int32_t reason, int32_t total) 77 { 78 return; 79 } 80 } // namespace HiviewDFX 81 } // namespace OHOS 82 83 // Invoke the query callback API to obtain system events. 84 std::shared_ptr<HiSysEventToolQuery> queryCallBack = nullptr; 85 try { 86 queryCallBack = std::make_shared<HiSysEventToolQuery>(); 87 } catch(...) { 88 // Catch exception thrown by make_shared 89 } 90 if (queryCallBack != nullptr) { 91 struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents); 92 std::vector<QueryRule> rules; 93 HiSysEventManager::QueryHiSysEvent(args, rules, queryCallBack); 94 } 95 ``` 96 972. Modify the **BUILD.gn** file. 98 99 In the **BUILD.gn** file, add the **libhisyseventmanager** library that depends on the **hisysevent\_native** component. 100 101 ``` 102 external_deps = [ "hisysevent_native:libhisyseventmanager", ] 103 ``` 104