# HiSysEvent Query
- [Overview](#section279684125212)
- [Development Guidelines](#section315316761113)
- [Available APIs](#section03869128521)
- [Development Example](#section14286111855212)
## Overview
HiSysEvent 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.
## Development Guidelines
### Available APIs
**Table 1** HiSysEvent query API
| API| Description|
| -------- | --------- |
| bool HiSysEventManager::QueryHiSysEvent(struct QueryArg& queryArg, std::vector<QueryRule>& queryRules, std::shared_ptr<HiSysEventQueryCallBackBase> queryCallBack) | Queries system events by specifying search criteria such as the time segment, event domain, and event name.
Input arguments:
- **queryArg**: event query parameter.
- **queryRules**: event filtering rules.
- **queryCallBack**: callback object for query results.
Return value:- **true**: The query is successful.
- **false**: The query has failed.
|
**Table 2** Description of QueryArg
| Attribute| Description|
| -------- | --------- |
| beginTime | Start time, in the **long long int** format.|
| endTime | End time, in the **long long int** format.|
| maxEvents | Maximum number of returned events, in the **int** format.|
**Table 3** Description of QueryRule
| API| Description|
| -------- | --------- |
| QueryRule(const std::string& domain, const std::vector<std::string>& eventList) | Constructor used to create a **QueryRule** object based on domain and event name list.
Input arguments:- **domain**:domain to which the event belongs, in the **string** format. By default, an empty string indicates that the domain is successfully matched.
- **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.
|
**Table 4** Description of HiSysEventQueryCallBackBase
| API| Description|
| -------- | --------- |
| void HiSysEventQueryCallBackBase::OnQuery(const ::std::vector<std::string>& sysEvent, const ::std::vector<int64_t>& seq) | Callback object for event query.
Input arguments:- **sysEvent**: event set.
- **seq**: event sequence set.
Return value:
None.|
| void HiSysEventQueryCallBackBase::OnComplete(int32_t reason, int32_t total) | Callback object for completion of event query.
Input arguments:- **reason**: reason for completion of event query. The default value is **0**.
- **total**: total number of events returned in this query.
Return value:
None.|
### Development Example
C++
1. Develop the source code.
- Import the corresponding header file:
hisysevent\_manager.h
- Implement the callback API.
void HiSysEventQueryCallBackBase::OnQuery\(const ::std::vector& sysEvent, const ::std::vector& seq\)
void HiSysEventQueryCallBackBase::OnComplete\(int32\_t reason, int32\_t total\)
- Invoke the query API in the corresponding service logic.
HiSysEventManager::QueryHiSysEvent\(struct QueryArg& queryArg, std::vector& queryRules, std::shared\_ptr queryCallBack\)
```
// In this example, you'll query all system events.
#include "hisysevent_manager.h"
#include
namespace OHOS {
namespace HiviewDFX {
// Implement the query callback API.
void HiSysEventToolQuery::OnQuery(const ::std::vector& sysEvent,
const ::std::vector& seq)
{
for_each(sysEvent.cbegin(), sysEvent.cend(), [](const std::string &tmp) {
std::cout << tmp << std::endl;
});
}
void HiSysEventToolQuery::OnComplete(int32_t reason, int32_t total)
{
return;
}
} // namespace HiviewDFX
} // namespace OHOS
// Invoke the query callback API to obtain system events.
auto queryCallBack = std::make_shared();
struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents);
std::vector mRules;
HiSysEventManager::QueryHiSysEvent(args, mRules, queryCallBack);
```
2. Modify the **BUILD.gn** file.
In the **BUILD.gn** file, add the **libhisyseventmanager** library that depends on the **hisysevent\_native** component.
```
external_deps = [ "hisysevent_native:libhisyseventmanager", ]
```