提交 0644bb3a 编写于 作者: S shawn_he

update doc

Signed-off-by: Nshawn_he <shawn.he@huawei.com>
上级 d253f86e
...@@ -89,7 +89,7 @@ Writes event information to the event file. This API uses a promise to return th ...@@ -89,7 +89,7 @@ Writes event information to the event file. This API uses a promise to return th
| Name | Type | Mandatory| Description| | Name | Type | Mandatory| Description|
| --------- | ----------------------- | ---- | --------------- | | --------- | ----------------------- | ---- | --------------- |
| eventType | [EventType](#eventtype) | Yes | Application event type.| | info | [SysEventInfo](#syseventinfo) | Yes | System event information.|
**Return value** **Return value**
......
# Internationalization – I18N # Internationalization – I18N
This module provides system-related or enhanced I18N capabilities, such as locale management, phone number formatting, and calendar, through supplementary I18N APIs that are not defined in ECMA 402.
The [Intl](js-apis-intl.md) module provides basic I18N capabilities through the standard I18N APIs defined in ECMA 402. It works with the I18N module to provide a complete suite of I18N capabilities.
> **NOTE** > **NOTE**
> >
> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. > The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
> - This module provides system-related or enhanced I18N capabilities, such as locale management, phone number formatting, and calendar, through supplementary I18N interfaces that are not defined in ECMA 402. For details about the basic I18N capabilities, see [Intl](js-apis-intl.md).
## Modules to Import ## Modules to Import
......
# Internationalization – Intl # Internationalization – Intl
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** This module provides basic I18N capabilities, such as time and date formatting, number formatting, and string sorting, through the standard I18N APIs defined in ECMA 402.
> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The [I18N](js-apis-i18n.md) module provides enhanced I18N capabilities through supplementary APIs that are not defined in ECMA 402. It works with the Intl module to provide a complete suite of I18N capabilities.
> - This module provides basic I18N capabilities, such as time and date formatting, number formatting, and string sorting, through the standard I18N interfaces defined in ECMA 402. For details about the enhanced I18N capabilities, see [I18N](js-apis-i18n.md).
> **NOTE**
>
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import ## Modules to Import
......
# HiSysEvent Listening<a name="EN-US_TOPIC_0000001185655868"></a> # HiSysEvent Listening
## Overview<a name="section315316685112"></a>
### Introduction<a name="section123181433335224"></a> ## Overview
HiSysEvent supports listening for events across processes. You can register a listener to listen for concerned events on a real-time basis. For example, you can enable the battery module to listen for power consumption events for power usage analysis.
### Constraints<a name="section123181433375224"></a>
Before subscribing to system events, you need to configure HiSysEvent logging. For details, see [HiSysEvent Logging Configuration](subsys-dfx-hisysevent-logging-config.md).
## Development Guidelines<a name="section315316685113"></a>
### Available APIs<a name="section0342191810519"></a>
**Table 1** Description of EventListener APIs
| Name| Description | ### Introduction
| -------- | --------- |
|int32_t HiSysEventManager::AddEventListener(std::shared_ptr&lt;HiSysEventSubscribeCallBack&gt; listener, std::vector&lt;ListenerRule&gt;&amp; rules)|Registers a listener for system events. You can listen for certain events by specifying rules.<br><br>Input arguments: <ul><li>**listener**: callback object for system events. </li><li>**rules**: rules for event listening. </li></ul>Return value:<ul><li>**0**: registration is successful. </li><li>Other values: Registration has failed.</li></ul>|
|int32_t HiSysEventManager::RemoveListener(std::shared_ptr&lt;HiSysEventSubscribeCallBack&gt; listener)|Removes the listener for system events.<br><br>Input arguments: <ul><li>**listener**: callback object for system events. </ul>Return value:<ul><li>**0**: Cancel registration is successful. </li><li>Other values: Cancel registration has failed.</li></ul>|
**Table 2** Description of ListenerRule HiSysEvent supports listening for events across processes. You can register a listener to listen for concerned events on a real-time basis. For example, you can enable the battery module to listen for power consumption events for power usage analysis.
| API| Description |
| -------- | --------- |
|ListenerRule(const std::string&amp; tag, RuleType ruleType = RuleType::WHOLE_WORD)|Constructor used to create a **ListenerRule** object based on the event tag.<br><br>Input arguments:<ul><li>**tag**: indicates the event tag for the **ListenerRule** object. The value is a string of 1 to 16 characters, including uppercase letters, lowercase letters, and digits. </li><li>**ruleType**: indicates the type of the **ListenerRule** object. The value is an enum defined by **RuleType**.</li></ul>|
|ListenerRule(const std::string&amp; domain, const std::string&amp; eventName, RuleType ruleType = RuleType::WHOLE_WORD)|Constructor used to create a **ListenerRule** object based on the event domain and event name.<br><br>Input arguments: <ul><li>**domain**: indicates the event domain for the **ListenerRule** object. The value is a string of 1 to 16 characters, including uppercase letters, digits, and underscores (&#95;). </li><li>**eventName**: indicates the event name for the **ListenerRule** object. The value is a string of 1 to 32 characters, including uppercase letters, digits, and underscores (&#95;). </li><li>**ruleType**: indicates the type of the **ListenerRule** object. The value is an enum defined by **RuleType**.</li></ul>|
|ListenerRule(const std::string&amp; domain, const std::string& eventName, const std::string&amp; tag, RuleType ruleType = RuleType::WHOLE_WORD)|Constructor used to create a **ListenerRule** object based on the event domain, event name, and event tag.<br><br>Input arguments:<ul><li>**tag**: indicates the event tag for the **ListenerRule** object. The value is a string of 1 to 16 characters, including uppercase letters, lowercase letters, and digits. </li><li>**domain**: indicates the event domain for the **ListenerRule** object. The value is a string of 1 to 16 characters, including uppercase letters, digits, and underscores (&#95;). </li><li>**eventName**: indicates the event name for the **ListenerRule** object. The value is a string of 1 to 32 characters, including uppercase letters, digits, and underscores (&#95;). </li><li>**ruleType**: indicates the type of the **ListenerRule** object. The value is an enum defined by **RuleType**.</li></ul>|
**Table 3** Description of RuleType
| Value | Description |
| ------------ | ------------- |
| WHOLE_WORD | Whole word matching |
| PREFIX | Prefix matching |
| REGULAR | Regular expression matching |
**Table 4** Description of HiSysEventSubscribeCallBack
| API| Description |
| -------- | --------- |
|void HiSysEventSubscribeCallBack::OnHandle(const std::string&amp; domain, const std::string&amp; eventName, const int eventType, const std::string&amp; eventDetail)|Provides the callback of system events.<br><br>Input arguments: <ul><li>**domain**: indicates the domain to which the event belongs. </li><li>**eventName**: indicates the event name. </li><li>**eventType**: indicates the event type. </li><li>**eventDetail**: indicates the event information, in JSON format. </li></ul>Return value:<br>None.|
### Development Example<a name="section123181432175110"></a>
C++ ### Constraints
1. Develop the source code. Before subscribing to system events, you need to configure HiSysEvent logging. For details, see [HiSysEvent Logging Configuration](../subsystems/subsys-dfx-hisysevent-logging-config.md).
Import the **DemoListener.h** header file, which contains the **DemoListener** class for implementing the custom event callback.
``` ## Development Guidelines
#ifndef DEMO_LISTENER_H
#define DEMO_LISTENER_H
#include "hisysevent_subscribe_callback.h"
#include <string> ### Available APIs
class DemoListener : public OHOS::HiviewDFX::HiSysEventSubscribeCallBack { **Table 1** Description of HiSysEventListener APIs
public:
explicit DemoListener() : HiSysEventSubscribeCallBack() {}
void OnHandle(const std::string& domain, const std::string& eventName, const int eventType,
const std::string& eventDetail);
virtual ~DemoListener() {}
void OnServiceDied();
};
#endif // DEMO_LISTENER_H | API| Description|
``` | -------- | -------- |
| int32_t HiSysEventManager::AddListener(std::shared_ptr&lt;HiSysEventListener&gt; listener, std::vector&lt;ListenerRule&gt;&amp; rules) | Registers a listener for system events. You can listen for certain events by specifying rules.<br>Input arguments:<br>- **listener**: callback object for system events.<br>- **rules**: rules for event listening.<br>Return value:<br>- **0**: Registration is successful.<br>- A negative value: Registration has failed.|
| int32_t HiSysEventManager::RemoveListener(std::shared_ptr&lt;HiSysEventListener&gt; listener) | Removes the listener for system events.<br>Input arguments:<br>- **listener**: callback object for system events.<br>Return value:<br>- **0**: Canceling registration is successful.<br>- A negative value: Canceling registration has failed. |
Create the **DemoListener.cpp** file, and add the implementation logic of the custom event callback API in the **DemoListener** class. **Table 2** Description of ListenerRule
``` | API| Description|
#include "demo_listener.h" | -------- | -------- |
| ListenerRule(const std::string&amp; tag, RuleType ruleType = RuleType::WHOLE_WORD) | Constructor used to create a **ListenerRule** object based on the event tag.<br>Input arguments:<br>- **tag**: event tag for the **ListenerRule** object. The value is a string of 1 to 16 characters, including uppercase letters, lowercase letters, and digits.<br>- **ruleType**: type of the **ListenerRule** object. The value is an enum defined by **RuleType**.|
| ListenerRule(const std::string&amp; domain, const std::string&amp; eventName, RuleType ruleType = RuleType::WHOLE_WORD) | Constructor used to create a **ListenerRule** object based on the event domain and event name.<br>Input arguments:<br>- **domain**: event domain for the **ListenerRule** object. The value is a string of 1 to 16 characters, including uppercase letters, digits, and underscores.<br>- **eventName**: event name for the **ListenerRule** object. The value is a string of 1 to 32 characters, including uppercase letters, digits, and underscores.<br>- **ruleType**: type of the **ListenerRule** object. The value is an enum defined by **RuleType**.|
| ListenerRule(const std::string&amp; domain, const std::string&amp; eventName, const std::string&amp; tag, RuleType ruleType = RuleType::WHOLE_WORD) | Constructor used to create a **ListenerRule** object based on the event domain, event name, and event tag.<br>Input arguments:<br>- **tag**: event tag for the **ListenerRule** object. The value is a string of 1 to 16 characters, including uppercase letters, lowercase letters, and digits.<br>- **domain**: event domain for the **ListenerRule** object. The value is a string of 1 to 16 characters, including uppercase letters, digits, and underscores.<br>- **eventName**: event name for the **ListenerRule** object. The value is a string of 1 to 32 characters, including uppercase letters, digits, and underscores.<br>- **ruleType**: type of the **ListenerRule** object. The value is an enum defined by **RuleType**.|
#include <iostream> **Table 3** Description of RuleType
void DemoListener::OnHandle(const std::string& domain, const std::string& eventName, | Value| Description|
const int eventType, const std::string& eventDetail) | -------- | -------- |
{ | WHOLE_WORD | Whole word matching|
std::cout << eventDetail << std::endl; | PREFIX | Prefix matching|
} | REGULAR | Regular expression matching|
void DemoListener::OnServiceDied() **Table 4** Description of HiSysEventListener
{
std::cout << std::string("service disconnect, exit") << std::endl;
exit(0);
}
```
Call the **AddEventListener** API of the **HiSysEventManager** class to add a listener for system events. | API| Description|
| -------- | -------- |
| void HiSysEventListener::OnEvent(std::shared_ptr&lt;HiSysEventRecord&gt; sysEvent) | Callback object for system events.<br>Input arguments:<br>- **sysEvent**: real-time system events.<br>Return value:<br>None.|
| void HiSysEventListener::OnServiceDied() | Callback object for service exceptions.<br>Input arguments:<br>None.<br>Return value:<br>None.|
``` **Table 5** Description of HiSysEventRecord
std::shared_ptr<DemoListener> demoListener = nullptr; | API| Description|
try { | -------- | -------- |
demoListener = std::make_shared<DemoListener>(); |std::string HiSysEventRecord::AsJson()|Obtains the content of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Content of the system event.|
} catch(...) { |std::string HiSysEventRecord::GetDomain()|Obtains the domain name of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Domain name of the system event.|
// Catch exception thrown by make_shared |std::string HiSysEventRecord::GetEventName()|Obtains the name of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Name of the system event.|
} |HiSysEvent::EventType HiSysEventRecord::GetEventType()|Obtains the type of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Type of the system event.|
if (demoListener != nullptr) { |std::string HiSysEventRecord::GetLevel()|Obtains the level of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Level of the system event.|
// Add a ListenerRule object based on the event tag, with RuleType left unspecified (in this case, ruleType is defaulted to WHOLE_WORD). |std::string HiSysEventRecord::GetTag()|Obtains the tag of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Tag of the system event.|
ListenerRule tagRule("dfx"); |std::string HiSysEventRecord::GetTimeZone()|Obtains the time zone of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Time zone, in the format of **+0800**.|
// Add a ListenerRule object based on the event tag, with RuleType set as REGULAR. |int HiSysEventRecord::GetTraceFlag()|Obtains the distributed call chain tracing flag of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Distributed call chain tracing flag.|
ListenerRule regRule("dfx.*", RuleType::REGULAR); |int64_t HiSysEventRecord::GetPid()|Obtains the ID of the process that flushes a system event to the disk.<br>Input arguments:<br>None.<br>Return value:<br>Process ID.|
// Add a ListenerRule object based on the event domain and event name, with RuleType set as PREFIX. |int64_t HiSysEventRecord::GetTid()|Obtains the thread ID of the process that flushes a system event to the disk.<br>Input arguments:<br>None.<br>Return value:<br>Thread ID.|
ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX); |int64_t HiSysEventRecord::GetUid()|Obtains the user ID of the process that flushes a system event to the disk.<br>Input arguments:<br>None.<br>Return value:<br>User ID.|
std::vector<ListenerRule> sysRules; |uint64_t HiSysEventRecord::GetPspanId()|Obtains the parent span ID of the distributed call chain tracing task.<br>Input arguments:<br>None.<br>Return value:<br>Parent span ID of the distributed call chain tracing task.|
sysRules.push_back(tagRule); |uint64_t HiSysEventRecord::GetSpandId()|Obtains the span ID of the distributed call chain tracing task.<br>Input arguments:<br>None.<br>Return value:<br>Span ID of the distributed call chain tracing task.|
sysRules.push_back(regRule); |uint64_t HiSysEventRecord::GetTime()|Obtains the timestamp of a system event.<br>Input arguments:<br>None.<br>Return value:<br>Timestamp of the system event.|
sysRules.push_back(domainNameRule); |uint64_t HiSysEventRecord::GetTraceId()|Obtains the ID of the distributed call chain tracing task.<br>Input arguments:<br>None.<br>Return value:<br>ID of the distributed call chain tracing task.|
HiSysEventManager::AddEventListener(demoListener, sysRules); |void HiSysEventRecord::GetParamNames(std::vector&lt;std::string&gt;&amp; params)|Obtains all key names of a system event.<br>Input arguments:<br>- **params**: key name array reference.<br>Return value:<br>None.|
} |int HiSysEventRecord::GetParamValue(const std::string&amp; param, int64_t&amp; value)|Parses the value of the **param** key in a system event into an int64\_t value.<br>Input arguments:<br>- **param**: key name.<br>- **value**: int64\_t reference.<br>Return value:<br>- **0**: Parsing is successful.<br>- **-1**: Parsing failed due to initialization error.<br>- **-2**: Parsing failed due to nonexistent key name.<br>- **-3**: Parsing failed due to type mismatch.|
``` |int HiSysEventRecord::GetParamValue(const std::string&amp; param, uint64_t&amp; value)|Parses the value of the **param** key in a system event into a uint64\_t value.<br>Input arguments:<br>- **param**: key name.<br>- **value**: uint64\_t reference.<br>Return value:<br>- **0**: Parsing is successful.<br>- **-1**: Parsing failed due to initialization error.<br>- **-2**: Parsing failed due to nonexistent key name.<br>- **-3**: Parsing failed due to type mismatch.|
|int HiSysEventRecord::GetParamValue(const std::string&amp; param, double&amp; value)|Parses the value of the **param** key in a system event into a double value.<br>Input arguments:<br>- **param**: key name.<br>- **value**: double reference.<br>Return value:<br>- **0**: Parsing is successful.<br>- **-1**: Parsing failed due to initialization error.<br>- **-2**: Parsing failed due to nonexistent key name.<br>- **-3**: Parsing failed due to type mismatch.|
2. Configure the **BUILD.gn** file. |int HiSysEventRecord::GetParamValue(const std::string&amp; param, std::string&amp; value)|Parses the value of the **param** key in a system event into a string value.<br>Input arguments:<br>- **param**: key name.<br>- **value**: std::string reference.<br>Return value:<br>- **0**: Parsing is successful.<br>- **-1**: Parsing failed due to initialization error.<br>- **-2**: Parsing failed due to nonexistent key name.<br>- **-3**: Parsing failed due to type mismatch.|
|int HiSysEventRecord::GetParamValue(const std::string&amp; param, std::vector&lt;int64_t&gt;&amp; value)|Parses the value of the **param** key in a system event into an int64\_t array.<br>Input arguments:<br>- **param**: key name.<br>- **value**: int64\_t array reference.<br>Return value:<br>- **0**: Parsing is successful.<br>- **-1**: Parsing failed due to initialization error.<br>- **-2**: Parsing failed due to nonexistent key name.<br>- **-3**: Parsing failed due to type mismatch.|
In the **BUILD.gn** file, add the **libhisyseventmanager** library that depends on the **hisysevent\_native** component. |int HiSysEventRecord::GetParamValue(const std::string&amp; param, std::vector&lt;uint64_t&gt;&amp; value)|Parses the value of the **param** key in a system event into a uint64\_t array.<br>Input arguments:<br>- **param**: key name.<br>- **value**: uint64\_t array reference.<br>Return value:<br>- **0**: Parsing is successful.<br>- **-1**: Parsing failed due to initialization error.<br>- **-2**: Parsing failed due to nonexistent key name.<br>- **-3**: Parsing failed due to type mismatch.|
|int HiSysEventRecord::GetParamValue(const std::string&amp; param, std::vector&lt;double&gt;&amp; value)|Parses the value of the **param** key in a system event into a double array.<br>Input arguments:<br>- **param**: key name.<br>- **value**: double array reference.<br>Return value:<br>- **0**: Parsing is successful.<br>- **-1**: Parsing failed due to initialization error.<br>- **-2**: Parsing failed due to nonexistent key name.<br>- **-3**: Parsing failed due to type mismatch.|
``` |int HiSysEventRecord::GetParamValue(const std::string&amp; param, std::vector&lt;std::string&gt;&amp; value)|Parses the value of the **param** key in a system event into a string array.<br>Input arguments:<br>- **param**: key name.<br>- **value**: std::string array reference.<br>Return value:<br>- **0**: Parsing is successful.<br>- **-1**: Parsing failed due to initialization error.<br>- **-2**: Parsing failed due to nonexistent key name.<br>- **-3**: Parsing failed due to type mismatch.|
external_deps = [ "hisysevent_native:libhisyseventmanager", ]
```
## How to Develop
### **C++**
The following provides an example of how to use C++ APIs of **HiSysEvent**.
1. Develop the source code.
Import the **DemoListener.h** header file, which contains the **DemoListener** class for implementing the custom event callback.
```
#ifndef DEMO_LISTENER_H
#define DEMO_LISTENER_H
#include "hisysevent_listener.h"
#include <string>
class DemoListener : public OHOS::HiviewDFX::HiSysEventListener {
public:
explicit DemoListener() : HiSysEventListener() {}
virtual ~DemoListener() {}
public:
void OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent);
void OnServiceDied();
};
#endif // DEMO_LISTENER_H
```
Create the **DemoListener.cpp** file, and add the implementation logic of the custom event callback API in the **DemoListener** class.
```
#include "demo_listener.h"
#include <iostream>
void DemoListener::OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)
{
if (sysEvent == nullptr) {
return;
}
std::cout << sysEvent.AsJson() << std::endl;
}
void DemoListener::OnServiceDied()
{
std::cout << std::string("service disconnect, exit") << std::endl;
exit(0);
}
```
Call the **AddEventListener** API of the **HiSysEventManager** class to add a listener for system events.
```
auto demoListener = std::make_shared<DemoListener>();
// Add a ListenerRule object based on the event tag, with RuleType left unspecified (in this case, ruleType is defaulted to WHOLE_WORD).
ListenerRule tagRule("dfx");
// Add a ListenerRule object based on the event tag, with RuleType set as REGULAR.
ListenerRule regRule("dfx.*", RuleType::REGULAR);
// Add a ListenerRule object based on the event domain and event name, with RuleType set as PREFIX.
ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX);
std::vector<ListenerRule> sysRules;
sysRules.push_back(tagRule);
sysRules.push_back(regRule);
sysRules.push_back(domainNameRule);
HiSysEventManager::AddEventListener(demoListener, sysRules);
```
2. Configure the **BUILD.gn** file.
In the **BUILD.gn** file, add the **libhisysevent** and **libhisyseventmanager** libraries that depend on the **hisysevent_native** component.
```
external_deps = [
"hisysevent_native:libhisysevent",
"hisysevent_native:libhisyseventmanager",
]
```
# HiSysEvent Query<a name="EN-US_TOPIC_0000001231455461"></a> # HiSysEvent Query
## Overview<a name="section279684125212"></a>
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. ## Overview
## Development Guidelines<a name="section315316761113"></a> HiSysEvent 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.
### Available APIs<a name="section03869128521"></a>
**Table 1** Description of the HiSysEvent query API ## Development Guidelines
| Name| Description |
| -------- | --------- |
| int32_t HiSysEventManager::QueryHiSysEvent(struct QueryArg&amp; queryArg, std::vector&lt;QueryRule&gt;&amp; queryRules, std::shared_ptr&lt;HiSysEventQueryCallBack&gt; 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>**0**: The query is successful. </li><li>Other values: The query has failed.</li></ul> |
### Available APIs
**Table 2** Description of QueryArg > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
>
> For details about the **HiSysEventRecord** argument in the **OnQuery()** method of **HiSysEventQueryCallback**, see Table 5 in [HiSysEvent Listening](subsys-dfx-hisysevent-listening.md).
| Attribute| Description | **Table 1** Description of the HiSysEvent query API
| -------- | --------- |
| 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&lt;std::string&gt;& 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&lt;std::string&gt;** format. By default, an empty string indicates that the event names on the list are successfully matched.</li></ul> |
**Table 4** Description of HiSysEventQueryCallBack
| API| Description |
| -------- | --------- |
| void HiSysEventQueryCallBack::OnQuery(const ::std::vector&lt;std::string&gt;&amp; sysEvent, const ::std::vector&lt;int64_t&gt;&amp; 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.|
| 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.|
### Development Example<a name="section14286111855212"></a>
C++
1. Develop the source code.
- Import the corresponding header file:
hisysevent\_manager.h | API| Description|
| -------- | -------- |
| int32_t HiSysEventManager::Query(struct QueryArg&amp; arg, std::vector&lt;QueryRule&gt;&amp; rules, std::shared_ptr&lt;HiSysEventQueryCallback&gt; callback) | Queries system events by specifying search criteria such as the time segment, event domain, and event name.<br>Input arguments:<br>- **arg**: event query parameter.<br>- **rules**: rules for event filtering.<br>- **callback**: callback object for event query.<br>Return value:<br>- **0**: Query is successful.<br>- A negative value: Query has failed.|
- Implement the callback API. **Table 2** Description of QueryArg
void HiSysEventQueryCallBack::OnQuery\(const ::std::vector&lt;std::string&gt;& sysEvent, const ::std::vector<int64\_t\>& seq\) | Attribute| Description|
| -------- | -------- |
void HiSysEventQueryCallBack::OnComplete\(int32\_t reason, int32\_t total\) | beginTime | Start time, in the **long long int** format.|
| endTime | End time, in the **long long int** format.|
- Invoke the query API in the corresponding service logic. | maxEvents | Maximum number of returned events, in the **int** format.|
HiSysEventManager::QueryHiSysEvent\(struct QueryArg& queryArg, std::vector<QueryRule\>& queryRules, std::shared\_ptr<HiSysEventQueryCallBack\> queryCallBack\)
```
// In this example, you'll query all system events.
#include "hisysevent_manager.h"
#include <iostream>
namespace OHOS {
namespace HiviewDFX {
// Implement the query callback API.
void HiSysEventToolQuery::OnQuery(const ::std::vector<std::string>& sysEvent,
const ::std::vector<int64_t>& 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.
std::shared_ptr<HiSysEventToolQuery> queryCallBack = nullptr;
try {
queryCallBack = std::make_shared<HiSysEventToolQuery>();
} catch(...) {
// Catch exception thrown by make_shared
}
if (queryCallBack != nullptr) {
struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents);
std::vector<QueryRule> rules;
HiSysEventManager::QueryHiSysEvent(args, rules, queryCallBack);
}
```
2. Modify the **BUILD.gn** file.
In the **BUILD.gn** file, add the **libhisyseventmanager** library that depends on the **hisysevent\_native** component.
``` **Table 3** Description of QueryRule
external_deps = [ "hisysevent_native:libhisyseventmanager", ]
``` | API| Description|
| -------- | -------- |
| QueryRule(const std::string&amp; domain, const std::vector&lt;std::string&gt;&amp; eventList) | 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.|
**Table 4** Description of HiSysEventQueryCallback
| API| Description|
| -------- | -------- |
| void HiSysEventQueryCallback::OnQuery(std::shared_ptr&lt;std::vector&lt;HiSysEventRecord&gt;&gt; sysEvents) | Callback object for event query.<br>Input arguments:<br>- **sysEvents**: event list.<br>Return value:<br>None.|
| void HiSysEventQueryCallback::OnComplete(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 default value is **0**.<br>- **total**: total number of events returned in this query.<br>Return value:<br>None.|
### How to Develop
**C++**
1. Develop the source code.
Import the corresponding header file:
```
#include "hisysevent_manager.h"
```
Implement the callback API.
```
void HiSysEventQueryCallback::OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents)
void HiSysEventQueryCallback::OnComplete(int32_t reason, int32_t total)
```
Invoke the query API in the corresponding service logic.
```
HiSysEventManager::Query(struct QueryArg& queryArg,
std::vector<QueryRule>& queryRules, std::shared_ptr<HiSysEventQueryCallback> queryCallBack)
```
In this example, you'll query all system events.
```
#include "hisysevent_manager.h"
#include <iostream>
namespace OHOS {
namespace HiviewDFX {
// Implement the query callback API.
void HiSysEventToolQuery::OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents)
{
if (sysEvents == nullptr) {
return;
}
for_each((*sysEvents).cbegin(), (*sysEvents).cend(), [](const HiSysEventRecord& event) {
std::cout << event.AsJson() << 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<HiSysEventToolQuery>();
struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents);
std::vector<QueryRule> rules;
HiSysEventManager::QueryHiSysEvent(args, rules, queryCallBack);
```
2. Modify the **BUILD.gn** file.
In the **BUILD.gn** file, add the **libhisysevent** and **libhisyseventmanager** libraries that depend on the **hisysevent_native** component.
```
external_deps = [
"hisysevent_native:libhisysevent",
"hisysevent_native:libhisyseventmanager",
]
```
...@@ -17,7 +17,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin** ...@@ -17,7 +17,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin**
| Option| Description| | Option| Description|
| -------- | -------- | | -------- | -------- |
| -r | Subscribes to real-time system events based on the default settings. When this option is specified, any real-time system event will be printed on the console.| | -r | Subscription to real-time system events based on the default settings. When this option is specified, any real-time system event will be printed on the console.|
- Command for enabling the debugging mode: - Command for enabling the debugging mode:
...@@ -29,7 +29,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin** ...@@ -29,7 +29,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin**
| Option| Description| | Option| Description|
| -------- | -------- | | -------- | -------- |
| -d | Subscribes to real-time system events in debugging mode.| | -d | Subscription to real-time system events in debugging mode.|
- Command for subscribing to real-time system events by event tag: - Command for subscribing to real-time system events by event tag:
...@@ -96,7 +96,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin** ...@@ -96,7 +96,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin**
| Option| Description| | Option| Description|
| -------- | -------- | | -------- | -------- |
| -l | Queries historical system events based on the default settings. A maximum of 1,000 latest system events will be returned.| | -l | Query of historical system events based on the default settings. A maximum of 10,000 system events will be returned.|
- Command for querying historical system events within the specified period of time: - Command for querying historical system events within the specified period of time:
...@@ -130,7 +130,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin** ...@@ -130,7 +130,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin**
| Option| Description| | Option| Description|
| -------- | -------- | | -------- | -------- |
| -m | Maximum number of historical system events that can be queried. The value ranges from **0** to **1000**. The number of returned system events is not more than the value of this parameter.| | -m | Maximum number of historical system events that can be queried. The number of returned system events is not more than the value of this parameter.|
Example: Example:
...@@ -151,7 +151,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin** ...@@ -151,7 +151,7 @@ The HiSysEvent tool is a command line tool preconfigured in the **/system/bin**
| Option| Description| | Option| Description|
| -------- | -------- | | -------- | -------- |
| -v | Used with the subscription command **-r** and query command **-l**. If system event validity check is enabled, invalid content contained in system events will be highlighted in red.| | -v | Used with the **-r** and **-l** commands. If system event validity check is enabled, invalid content contained in system events will be highlighted in red.|
Example: Example:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册