提交 0eb69bad 编写于 作者: S shawn_he

update doc

Signed-off-by: Nshawn_he <shawn.he@huawei.com>
上级 849c8114
# HiSysEvent Logging<a name="EN-US_TOPIC_0000001231373947"></a> # HiSysEvent Logging
## Overview<a name="section77571101789"></a> ## Overview
### Introduction<a name="section123133332175224"></a> ### Introduction
HiSysEvent provides event logging APIs for OpenHarmony to record important information of key processes during system running, helping you locate faults. In addition, you can upload the log data to the cloud for big data analytics. HiSysEvent provides event logging APIs for OpenHarmony to record important information of key processes during system running. Besides, it supports shielding of event logging by event domain, helping you to evaluate the impact of event logging.
### Constraints<a name="section123181432175224"></a> ### Working Principles
Before logging system events, you need to configure HiSysEvent logging. For details, see [HiSysEvent Logging Configuration](subsys-dfx-hisysevent-logging-config.md). Before logging system events, you need to complete HiSysEvent logging configuration. For details, see [HiSysEvent Logging Configuration](subsys-dfx-hisysevent-logging-config.md).
## Development Guidelines<a name="section314416685113"></a> ## Development Guidelines
### Available APIs<a name="section13480315886"></a> ### When to Use
The following table lists the C++ APIs provided by the HiSysEvent class. Use HiSysEvent logging to flush logged event data to disks.
For details about the HiSysEvent class, see the API reference. ### Available APIs
**Table 1** C++ APIs provided by HiSysEvent #### C++ Event Logging APIs
| API| Description| HiSysEvent logging is implemented using the API provided by the **HiSysEvent** class. For details, see the API Reference.
| -------- | --------- |
| template&lt;typename... Types&gt; static int Write(const std::string &amp;domain, const std::string &amp;eventName, EventType type, Types... keyValues) | Logs system events. <br><br>Input arguments: <ul><li>**domain**: Indicates the domain related to the event. You can use a preconfigured domain or customize a domain as needed. The name of a custom domain can contain a maximum of 16 characters, including digits (0-9) and uppercase letters (A-Z). It must start with a letter. </li><li>**eventName**: Indicates the event name. The value contains a maximum of 32 characters, including digits (0 to 9), letters (A-Z), and underscores (&#95;). It must start with a letter and cannot end with an underscore. </li><li>**type**: Indicates the event type. For details, see EventType. </li><li>**keyValues**: Indicates the key-value pairs of event parameters. It can be in the format of the basic data type, std::string, std::vector&lt;basic data type&gt;, or std:vector&lt;std::string&gt;. The value contains a maximum of 48 characters, including digits (0 to 9), letters (A-Z), and underscores (&#95;). It must start with a letter and cannot end with an underscore. The number of parameter names cannot exceed 32. </li></ul>Return value: <ul><li>**0**: The logging is successful. </li><li>Negative value: The logging has failed.</li></ul> | > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> In OpenHarmony-3.2-Beta3, HiSysEvent logging is open for restricted use to avoid event storms. The **HiSysEvent::Write** API in Table 1 is replaced by the **HiSysEventWrite** API in Table 2. The **HiSysEvent::Write** API has been deprecated. Use the **HiSysEventWrite** API instead for HiSysEvent logging.
**Table 2** Description of HiSysEvent::Domain APIs
**Table 1** C++ event logging API (deprecated)
| API| Description|
| -------- | --------- | | API | Description |
| static const std::string AAFWK | Atomic ability subsystem| | ------------------------------------------------------------ | ---------------------- |
| static const std::string APPEXECFWK | User program framework subsystem| | template&lt;typename...&nbsp;Types&gt;&nbsp;<br>static&nbsp;int&nbsp;Write(const&nbsp;std::string&nbsp;&amp;domain,&nbsp;const&nbsp;std::string&nbsp;&amp;eventName,&nbsp;EventType&nbsp;type,&nbsp;Types...&nbsp;keyValues) | Flushes logged event data to disks.|
| static const std::string ACCOUNT | Account subsystem|
| static const std::string ARKUI | ARKUI subsystem| **Table 2** C++ event logging API (in use)
| static const std::string AI | AI subsystem| | API | Description |
| static const std::string BARRIER_FREE | Accessibility subsystem| | ------------------------------------------------------------ | ---------------------- |
| static const std::string BIOMETRICS | Biometric recognition subsystem| | HiSysEventWrite(domain, eventName, type, ...) | Flushes logged event data to disks.|
| static const std::string CCRUNTIME |C/C++ operating environment subsystem|
| static const std::string COMMUNICATION | Public communication subsystem| **Table 3** Event types
| static const std::string DEVELOPTOOLS | Development toolchain subsystem|
| static const std::string DISTRIBUTED_DATAMGR | Distributed data management subsystem| | Event | Description |
| static const std::string DISTRIBUTED_SCHEDULE | Distributed Scheduler subsystem| | --------- | ------------ |
| static const std::string GLOBAL | Globalization subsystem| | FAULT | Fault event|
| static const std::string GRAPHIC | Graphics subsystem| | STATISTIC | Statistical event|
| static const std::string HIVIEWDFX | DFX subsystem| | SECURITY | Security event|
| static const std::string IAWARE | Scheduling and resource management subsystem| | BEHAVIOR | Behavior event|
| static const std::string INTELLI_ACCESSORIES | Smart accessory subsystem|
| static const std::string INTELLI_TV | Smart TV subsystem| #### Kernel Event Logging APIs
| static const std::string IVI_HARDWARE | IVI-dedicated hardware subsystem|
| static const std::string LOCATION | LBS subsystem| The following table describes the kernel event logging APIs.
| static const std::string MSDP | MSDP subsystem|
| static const std::string MULTI_MEDIA | Media subsystem| **Table 4** Description of kernel event logging APIs
| static const std::string MULTI_MODAL_INPUT | Multimode input subsystem|
| static const std::string NOTIFICATION | Common event and notification subsystem| | API | Description |
| static const std::string POWERMGR | Power management subsystem| | ------------------------------------------------------------ | ------------------------------------ |
| static const std::string ROUTER | Router subsystem| | struct hiview_hisysevent *hisysevent_create(const char *domain, const char *name, enum hisysevent_type type); | Creates a **hisysevent** object. |
| static const std::string SECURITY | Security subsystem| | void hisysevent_destroy(struct hiview_hisysevent *event); | Destroys a **hisysevent** object. |
| static const std::string SENSORS | Pan-sensor subsystem| | int hisysevent_put_integer(struct hiview_hisysevent *event, const char *key, long long value); | Adds event parameters of the integer type to a **hisysevent** object. |
| static const std::string SOURCE_CODE_TRANSFORMER | Application porting subsystem| | int hisysevent_put_string(struct hiview_hisysevent *event, const char *key, const char *value); | Adds event parameters of the string type to a **hisysevent** object.|
| static const std::string STARTUP | Startup subsystem| | int hisysevent_write(struct hiview_hisysevent *event); | Flushes **hisysevent** object data to disks. |
| static const std::string TELEPHONY | Telephony subsystem|
| static const std::string UPDATE | Update subsystem| Table 5 Kernel event types
| static const std::string USB | USB subsystem|
| static const std::string WEARABLE_HARDWARE | Wearable-dedicated hardware subsystem| | Event | Description |
| static const std::string WEARABLE_HARDWARE | Wearable-dedicated service subsystem| | --------- | ------------ |
| static const std::string OTHERS | Others| | FAULT | Fault event|
**Table 3** Description of HiSysEvent::EventType
| Name| Description|
| -------- | --------- |
| FAULT | Fault event|
| STATISTIC | Statistical event| | STATISTIC | Statistical event|
| SECURITY | Security event| | SECURITY | Security event|
| BEHAVIOR | System behavior event| | BEHAVIOR | Behavior event|
### How to Develop
#### C++ Event Logging
1. Call the event logging API wherever needed, with required event parameters passed to the API.
```c++
HiSysEventWrite(HiSysEvent::Domain::AAFWK, "START_APP", HiSysEvent::EventType::BEHAVIOR, "APP_NAME", "com.ohos.demo");
```
#### Kernel Event Logging
1. Create a **hisysevent** object based on the specified event domain, event name, and event type.
```c
struct hiview_hisysevent *event = hisysevent_create("KERNEL", "BOOT", BEHAVIOR);
```
2. Pass the customized event parameters to the **hisysevent** object.
### Development Example<a name="section112771171317"></a> ```c
// Add a parameter of the integer type.
hisysevent_put_integer(event, "BOOT_TIME", 100);
C++ // Add a parameter of the string type.
hisysevent_put_string(event, "MSG", "This is a test message");
```
1. Develop the source code. 3. Trigger reporting of the **hisysevent** event.
Include the HiSysEvent header file in the class definition header file or class implementation source file. For example: ```c
hisysevent_write(event);
```
4. Manually destroy the **hisysevent** object.
```c
hisysevent_destroy(&event);
```
#### Shielding of Event Logging by Event Domain
1. In the corresponding file, define the **DOMAIN_MASKS** macro with content similar to DOMAIN_NAME_1|DOMAIN_NAME_2|...|DOMAIN_NAME_n. There are three scenarios:
- Shielding only event logging for the event domains configured in the current source code file: Define the **DOMAIN_MASKS** macro before importing the **.cpp** file to the **hisysevent.h** file.
```c++
#define DOMAIN_MASKS "DOMAIN_NAME_1|DOMAIN_NAME_2|...|DOMAIN_NAME_n"
#include "hisysevent.h"
```
- Shielding event logging for event domains of the entire module: Define the **DOMAIN_MASKS** macro in the **BUILD.gn** file of the module.
```gn
config("module_a"){
cflags_cc += ["-DDOMAIN_MASKS=\"DOMAIN_NAME_1|DOMAIN_NAME_2|...|DOMAIN_NAME_n\""]
}
```
- Shielding event logging for event domains globally: Define the **DOMAIN_MASKS** macro in **/build/config/compiler/BUILD.gn**.
```gn
cflags_cc += ["-DDOMAIN_MASKS=\"DOMAIN_NAME_1|DOMAIN_NAME_2|...|DOMAIN_NAME_n\""]
```
2. Perform event logging by using the **HiSysEventWrite** API.
```c++
constexpr char DOMAIN[] = "DOMAIN_NAME_1";
const std::string eventName = "EVENT_NAME1";
OHOS:HiviewDFX::HiSysEvent::EventType eventType = OHOS:HiviewDFX::HiSysEvent::EventType::FAULT;
HiSysEventWrite(domain, eventName, eventType); // Event logging is shielded for DOMAIN_NAME_1 because it has been defined in the DOMAIN_MASKS macro.
```
### Development Examples
#### C++ Event Logging
Assume that a service module needs to trigger event logging during application startup to record the application startup event and application bundle name. The following is the complete sample code:
1. Add the HiSysEvent component dependency to the **BUILD.gn** file of the service module.
```c++
external_deps = [ "hisysevent_native:libhisysevent" ]
```
2. In the application startup function **StartAbility()** of the service module, call the event logging API with the event parameters passed in.
```c++
#include "hisysevent.h"
int StartAbility()
{
... // Other service logic
int ret = HiSysEventWrite(HiSysEvent::Domain::AAFWK, "START_APP", HiSysEvent::EventType::BEHAVIOR, "APP_NAME", "com.ohos.demo");
... // Other service logic
}
```
#### Kernel Event Logging
Assume that the kernel service module needs to trigger event logging during device startup to record the device startup event. The following is the complete sample code:
1. In the device startup function **device_boot()**, construct a **hisysevent** object. After that, trigger event reporting, and then destroy the **hisysevent** object.
```c
#include <dfx/hiview_hisysevent.h>
#include <linux/errno.h>
#include <linux/printk.h>
int device_boot()
{
... // Other service logic
struct hiview_hisysevent *event = NULL;
int ret = 0;
event = hisysevent_create("KERNEL", "BOOT", BEHAVIOR);
if (!event) {
pr_err("failed to create event");
return -EINVAL;
}
ret = hisysevent_put_string(event, "MSG", "This is a test message");
if (ret != 0) {
pr_err("failed to put sting to event, ret=%d", ret);
goto hisysevent_end;
}
ret = hisysevent_write(event);
hisysevent_end:
hisysevent_destroy(&event);
... // Other service logic
}
``` ```
#### Shielding of Event Logging by Event Domain
- If you want to shield event logging for the **AAFWK** and **POWER** domains in a **.cpp** file, define the **DOMAIN_MASKS** macro before including the **hisysevent.h** header file to the **.cpp** file.
```c++
#define DOMAIN_MASKS "AAFWK|POWER"
#include "hisysevent.h" #include "hisysevent.h"
``` ... // Other service logic
HiSysEventWrite(OHOS:HiviewDFX::HiSysEvent::Domain::AAFWK, "JS_ERROR", OHOS:HiviewDFX::HiSysEvent::EventType::FAULT, "MODULE", "com.ohos.module"); // HiSysEvent logging is not performed.
... // Other service logic
HiSysEventWrite(OHOS:HiviewDFX::HiSysEvent::Domain::POWER, "POWER_RUNNINGLOCK", OHOS:HiviewDFX::HiSysEvent::EventType::FAULT, "NAME", "com.ohos.module"); // HiSysEvent logging is not performed.
Add the event logging code. For example, if you want to log events specific to the app start time (start\_app), then add the following code to the service implementation source file: ```
- If you want to shield event logging for the **AAFWK** and **POWER** domains of the entire service module, define the **DOMAIN_MASKS** macro as follows in the **BUILG.gn** file of the service module.
```gn
config("module_a") {
... // Other configuration items
cflags_cc += ["-DDOMAIN_MASKS=\"AAFWK|POWER\""]
}
``` ```
HiSysEvent::Write(HiSysEvent::Domain::AAFWK, "start_app", HiSysEvent::EventType::FAULT, "app_name", "com.demo");
- If you want to shield event logging for the **AAFWK** and **POWER** domains globally, define the **DOMAIN_MASKS** macro as follows in **/build/config/compiler/BUILD.gn**.
```gn
... // Other configuration items
cflags_cc += ["-DDOMAIN_MASKS=\"AAFWK|POWER\""]
``` ```
2. Configure compilation information. Specifically, add the subsystem SDK dependency to **BUILD.gn**. # Reference
``` The HiSysEvent module writes the logged event data to the node file, and the Hiview module parses and processes the event data in a unified manner. For details, see the [Hiview Development Guide](subsys-dfx-hiview.md).
external_deps = [ "hisysevent_native:libhisysevent" ]
```
# HiSysEvent Overview
## Introduction
HiSysEvent provides event logging APIs for OpenHarmony to record important information of key processes during system running, helping you locate faults. In addition, you can upload the log data to the cloud for big data analytics.
The key modules of HiSysEvent are described as follows:
- Event configuration: enables you to define HiSysEvent events in YAML files.
- Trace point configuration: provides trace point APIs and supports flushing of HiSysEvent events to disks.
- Event subscription: provides APIs for you to subscribe to HiSysEvent events by event domain and event name.
- Event query: provides APIs for you to query HiSysEvent events by event domain and event name.
- Event debugging tool: allows you to subscribe to real-time HiSysEvent events and query historical HiSysEvent events.
## Reference
For more information about the source code and usage of HiSysEvent, access the HiSysEvent code repository(https://gitee.com/openharmony/hiviewdfx_hisysevent).
# HiSysEvent Development<a name="EN-US_TOPIC_0000001195021448"></a> # HiSysEvent Development<a name="EN-US_TOPIC_0000001195021448"></a>
- **[HiSysEvent Overview](subsys-dfx-hisysevent-overview.md)**
- **[HiSysEvent Logging Configuration](subsys-dfx-hisysevent-logging-config.md)** - **[HiSysEvent Logging Configuration](subsys-dfx-hisysevent-logging-config.md)**
- **[HiSysEvent Logging](subsys-dfx-hisysevent-logging.md)** - **[HiSysEvent Logging](subsys-dfx-hisysevent-logging.md)**
......
# Hiview Development Guide
## Introduction
### Function Overview
Hiview is a module that provides toolkits for device maintenance across different platforms. It consists of the plugin management platform and the service plugins running on the platform. Hiview works in event-driven mode. The core of Hiview is a collection of HiSysEvent stubs distributed in the system. Formatted events are reported to Hiview through the HiSysEvent API for processing. The following figure shows the data interaction process.
**Figure 1** Data interaction between Hiview modules
![Hiview_module_data_interaction](figure/Hiview_module_data_interaction.png)
1. The service process calls the event logging API to report logged event information and writes the information to the node file.
2. SysEventSource of the Hiview process asynchronously reads event information from the node file and distributes the event to SysEventPipeline for processing.
- The SysEventService plugin verifies events and flushes them to disks.
- The Faultlogger plugin processes fault-related events.
- The EventLogger plugin collects event-related log information.
3. On completion of event processing, SysEventPipeline sends the events to the event subscription queue and then dispatches the events to the subscription plugin for processing.
- FreezeDetectorPlugin processes screen freezing events.
- HiCollieCollector processes suspension events.
### Basic Concepts
Before you get started, familiarize yourself with the following concepts:
- Plug-in
An independent module running in the Hiview process. A plugin is delivered with the Hiview binary file to implement maintenance and fault management functions independently. Plug-ins can be disassembled independently during compilation, and can be hosted on the platform during running and dynamically configured.
- Pipeline
An ordered set of event processing plugins. Events entering the pipeline are processed by plugins on the pipeline in sequence.
- Event source
A special plugin that can produce events. Different from common plugins, a special plugin can be bound to a pipeline, and can produce events and distribute them to the pipeline.
- Pipeline group
A group of pipelines configured on the same event source.
### Working Principles
Hiview supports plugin development on the plugin management platform and provides the required plugin development capabilities. You can add plugins to the Hiview platform to implement HiSysEvent event processing. Before you get started, you're expected to have a basic understanding of plugin working principles.
#### Plug-in Registration
A plugin can be registered in any of the following modes.
| Mode | Description |
| ------------------ | ------------------------------------------------------------ |
| Static registration | Use the **REGISTER(xxx);** macro to register the plugin. Such a plugin cannot be unloaded.|
| Proxy registration | Use the **REGISTER_PROXY(xxx);** macro to register the plugin. Such a plugin is not loaded upon startup and can be loaded and unloaded dynamically during system running.|
| Proxy registration and loading upon startup| Use the **REGISTER_PROXY_WITH_LOADED(xxx);** macro to register the plugin. Such a plugin is loaded upon startup and can be unloaded and loaded dynamically during system running.|
#### Plug-in Event-Driven Modes
There are two event-driven modes available for plugins: pipeline-driven and subscription-driven. The differences are as follows:
- Pipeline-driven plugins need to be configured on the pipeline. After an event is distributed from an event source to the pipeline, the event traverses the plugins configured on the pipeline in sequence for processing.
- Subscription-driven plugins do not need to be configured on the pipeline. However, a listener needs to be registered with the Hiview platform upon plugin startup, and the plugin needs to implement the event listener function.
#### Plug-in Loading
Depending on your service demand, you can compile all or some plugins into the Hiview binary file.
Multiple plugins can be built into an independent plugin package and preset in the system as an independent **.so** file. One **.so** file corresponds to one **plugin_config** file. For example, **libxxx.z.so** corresponds to the** xxx_plugin_config** file. When the Hiview process starts, it scans for the plugin package (**.so** file) and the corresponding configuration file and loads the plugins in the plugin package.
The plugin package is described as follows:
1. The plugin package runs on the plugin management platform as an independent entity. The plugins, pipelines, or event sources in it provide the same functions as the plugins in the Hiview binary.
2. Plug-ins in the plugin package can be inserted into the Hiview binary pipeline.
3. Subscribers, wherever they are located, can receive events sent by the platform based on the subscription rules.
## Plug-in Development Guide
### When to Use
You can deploy a plugin on the Hiview platform if you want to perform specific service processing on the HiSysEvent events distributed from the event source. The following table describes the APIs used for plugin development.
### Available APIs
The following table lists the APIs related to plugin development. For details about the APIs, see the API Reference.
Table 1 Description of Plugin APIs
| API | Description |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| virtual void OnLoad() | Loads plugins. After plugins are loaded, you can call this API to initialize data.|
| virtual void OnUnload() | Unloads plugins. Before plugins are unloaded, you can call this API to reclaim data. |
| virtual bool ReadyToLoad() | Checks whether the current plugin can be loaded when the Hiview starts plugin loading. |
| virtual bool OnEvent(std::shared_ptr\<Event\>& event) | Implements event processing. You can call this API to receive events distributed by the pipeline or platform perform service processing.|
| virtual bool CanProcessEvent(std::shared_ptr\<Event\> event) | Checks whether an event can traverse backward throughout the entire pipeline. This function takes effect only when the plugin is the first one in the pipeline.|
| HiviewContext* GetHiviewContext() | Obtains the context of the Hiview plugin management platform. |
**Table 2** Description of Event APIs
| API | Description |
| ---------------------- | ---------------------------- |
| domain_ | Event domain. |
| eventName_ | Event name. |
| happenTime_ | Time when an event occurs. |
| jsonExtraInfo_ | Event data in JSON format. |
| bool IsPipelineEvent() | Whether an event is a pipeline event. |
| bool HasFinish() | Whether an event can continue to traverse backward.|
### How to Develop
1. Define a service plugin class, namely, **PluginExample**, which inherits from the **Plugin** class.
```c++
#include "event.h"
#include "plugin.h"
class PluginExample : public Plugin {
public:
bool OnEvent(std::shared_ptr<Event>& event) override;
void OnLoad() override;
void OnUnload() override;
};
```
2. In the plugin class implementation code, register the plugin and overwrite the corresponding functions based on the service requirements.
```c++
#include "plugin_factory.h"
// Register the plugin in static registration mode.
REGISTER(PluginExample);
void PluginExample::OnLoad()
{
... // Initialize plugin resources while the plugin is loaded.
printf("PluginExample OnLoad \n");
}
void PluginExample::OnUnload()
{
... // Release plugin resources while the plugin is unloaded.
printf("PluginExample OnUnload \n");
}
bool PluginExample::OnEvent(std::shared_ptr<Event>& event)
{
... // Perform specific service processing on the event using the event processing function.
printf("PluginExample OnEvent \n");
// If the plugin focuses only on events of a certain domain, log only the events of this domain.
if (event->domain_ == "TEST_DOMAIN") {
printf("The event data received is %s \n", event->jsonExtraInfo_);
return true;
}
return false;
}
```
3. Configure the plugin in the **plugin_build.json** file and compile the plugin with the Hiview binary file.
```json
{
"plugins": {
"PluginExample": {
"path": "plugins/PluginExample",
"name": "PluginExample"
}
},
"rules": [
{
"info": {
"loadorder": {
"PluginExample": {
"loadtime": 0
}
},
"pipelines": {
"SysEventPipeline": [
PluginExample
]
}
}
}
]
}
```
## Reference
For more information about the source code and usage of HiSysEvent, access the Hiview code repository(https://gitee.com/openharmony/hiviewdfx_hiview).
...@@ -12,6 +12,7 @@ The DFX subsystem provides the following functions: ...@@ -12,6 +12,7 @@ The DFX subsystem provides the following functions:
- HiChecker: implements defect scanning. It is applicable to standard-system devices \(reference memory ≥ 128 MiB\). - HiChecker: implements defect scanning. It is applicable to standard-system devices \(reference memory ≥ 128 MiB\).
- HiDumper: exports system information. It is applicable to standard-system devices \(reference memory ≥ 128 MiB\). - HiDumper: exports system information. It is applicable to standard-system devices \(reference memory ≥ 128 MiB\).
- FaultLogger: implements crash detection. It is applicable to standard-system devices \(reference memory ≥ 128 MiB\). - FaultLogger: implements crash detection. It is applicable to standard-system devices \(reference memory ≥ 128 MiB\).
- Hiview: implements device maintenance across different platforms. It is applicable to standard-system devices \(reference memory ≥ 128 MiB\).
## Basic Concepts<a name="section5635178134811"></a> ## Basic Concepts<a name="section5635178134811"></a>
......
...@@ -8,3 +8,4 @@ ...@@ -8,3 +8,4 @@
- **[HiSysEvent Development](subsys-dfx-hisysevent.md)** - **[HiSysEvent Development](subsys-dfx-hisysevent.md)**
- **[HiDumper Development](subsys-dfx-hidumper.md)** - **[HiDumper Development](subsys-dfx-hidumper.md)**
- **[HiChecker Development](subsys-dfx-hichecker.md)** - **[HiChecker Development](subsys-dfx-hichecker.md)**
- **[Hiview Development](subsys-dfx-hiview.md)**
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册