1e41f4b71Sopenharmony_ci# HiSysEvent Logging
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Function Introduction
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciHiSysEvent 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.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci### Working Principles
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciBefore logging system events, you need to configure HiSysEvent logging. For details, see [HiSysEvent Logging Configuration](subsys-dfx-hisysevent-logging-config.md).
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## How to Develop
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci### Use Cases
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciUse HiSysEvent logging to flush logged event data to the event file.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci### Available APIs
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci#### C++ Event Logging API
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciHiSysEvent logging is implemented using the API provided by the **HiSysEvent** class. For details, see the [API Header Files](/base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include/).
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci> **NOTE**
26e41f4b71Sopenharmony_ci> 
27e41f4b71Sopenharmony_ci> 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.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**Table 1** Description of the C++ event logging API (deprecated)
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci| API                                                      | Description                  |
32e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | --------------------- |
33e41f4b71Sopenharmony_ci| 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 the event file.|
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**Table 2** Description of the C++ event logging API (in use)
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci| API                                                      | Description                  |
38e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | --------------------- |
39e41f4b71Sopenharmony_ci| HiSysEventWrite(domain, eventName, type, ...)                | Flushes logged event data to the event file.|
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci  **Table 3** Description of EventType enums
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci| Event Type  | Description       |
44e41f4b71Sopenharmony_ci| --------- | ----------- |
45e41f4b71Sopenharmony_ci| FAULT     | Fault event.|
46e41f4b71Sopenharmony_ci| STATISTIC | Statistical event.|
47e41f4b71Sopenharmony_ci| SECURITY  | Security event.|
48e41f4b71Sopenharmony_ci| BEHAVIOR  | Behavior event.|
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci#### C Event Logging API
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ciHiSysEvent logging is implemented using the API provided in the following table. For details, see the [API Header Files](/base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include/).
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci**Table 4** Description of the C event logging API
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci| API                                                      | Description                    |
57e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------------ |
58e41f4b71Sopenharmony_ci| int OH_HiSysEvent_Write(const char\* domain, const char\* name, HiSysEventEventType type, HiSysEventParam params[], size_t size); | Flushes logged event data to the event file.|
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci**Table 5** Description of HiSysEventEventType enums
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci| Event Type            | Description          |
63e41f4b71Sopenharmony_ci| -------------------- | -------------- |
64e41f4b71Sopenharmony_ci| HISYSEVENT_FAULT     | Fault event.|
65e41f4b71Sopenharmony_ci| HISYSEVENT_STATISTIC | Statistical event.|
66e41f4b71Sopenharmony_ci| HISYSEVENT_SECURITY  | Security event.|
67e41f4b71Sopenharmony_ci| HISYSEVENT_BEHAVIOR  | Behavior event.|
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci**Table 6** Description of the HiSysEventParam structure
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci| Attribute | Type            | Description                              |
72e41f4b71Sopenharmony_ci| --------- | -------------------- | ---------------------------------- |
73e41f4b71Sopenharmony_ci| name      | char name[]          | Event parameter name.                    |
74e41f4b71Sopenharmony_ci| t         | HiSysEventParamType  | Event parameter type.                    |
75e41f4b71Sopenharmony_ci| v         | HiSysEventParamValue | Event parameter value.                      |
76e41f4b71Sopenharmony_ci| arraySize | size_t               | Array length when the event parameter value is of the array type.|
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci**Table 7** Description of HiSysEventParamType enums
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci| Type               | Description                      |
81e41f4b71Sopenharmony_ci| ----------------------- | -------------------------- |
82e41f4b71Sopenharmony_ci| HISYSEVENT_INVALID      | Invalid event parameter.        |
83e41f4b71Sopenharmony_ci| HISYSEVENT_BOOL         | Event parameter of the bool type.        |
84e41f4b71Sopenharmony_ci| HISYSEVENT_INT8         | Event parameter of the int8_t type.      |
85e41f4b71Sopenharmony_ci| HISYSEVENT_UINT8        | Event parameter of the uint8_t type.     |
86e41f4b71Sopenharmony_ci| HISYSEVENT_INT16        | Event parameter of the int16_t type.     |
87e41f4b71Sopenharmony_ci| HISYSEVENT_UINT16       | Event parameter of the uint16_t type.    |
88e41f4b71Sopenharmony_ci| HISYSEVENT_INT32        | Event parameter of the int32_t type.     |
89e41f4b71Sopenharmony_ci| HISYSEVENT_UINT32       | Event parameter of the uint32_t type.    |
90e41f4b71Sopenharmony_ci| HISYSEVENT_INT64        | Event parameter of the int64_t type.     |
91e41f4b71Sopenharmony_ci| HISYSEVENT_UINT64       | Event parameter of the uint64_t type.    |
92e41f4b71Sopenharmony_ci| HISYSEVENT_FLOAT        | Event parameter of the float type.       |
93e41f4b71Sopenharmony_ci| HISYSEVENT_DOUBLE       | Event parameter of the double type.      |
94e41f4b71Sopenharmony_ci| HISYSEVENT_STRING       | Event parameter of the char* type.       |
95e41f4b71Sopenharmony_ci| HISYSEVENT_BOOL_ARRAY   | Event parameter of the bool array type.    |
96e41f4b71Sopenharmony_ci| HISYSEVENT_INT8_ARRAY   | Event parameter of the int8_t array type.  |
97e41f4b71Sopenharmony_ci| HISYSEVENT_UINT8_ARRAY  | Event parameter of the uint8_t array type. |
98e41f4b71Sopenharmony_ci| HISYSEVENT_INT16_ARRAY  | Event parameter of the int16_t array type. |
99e41f4b71Sopenharmony_ci| HISYSEVENT_UINT16_ARRAY | Event parameter of the uint16_t array type.|
100e41f4b71Sopenharmony_ci| HISYSEVENT_INT32_ARRAY  | Event parameter of the int32_t array type. |
101e41f4b71Sopenharmony_ci| HISYSEVENT_UINT32_ARRAY | Event parameter of the uint32_t array type.|
102e41f4b71Sopenharmony_ci| HISYSEVENT_INT64_ARRAY  | Event parameter of the int64_t array type. |
103e41f4b71Sopenharmony_ci| HISYSEVENT_UINT64_ARRAY | Event parameter of the uint64_t array type.|
104e41f4b71Sopenharmony_ci| HISYSEVENT_FLOAT_ARRAY  | Event parameter of the float array type.   |
105e41f4b71Sopenharmony_ci| HISYSEVENT_DOUBLE_ARRAY | Event parameter of the double array type.  |
106e41f4b71Sopenharmony_ci| HISYSEVENT_STRING_ARRAY | Event parameter of the char* array type.   |
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci**Table 8** Description of the HiSysEventParamValue union
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci| Attribute| Type| Description                    |
111e41f4b71Sopenharmony_ci| -------- | -------- | ------------------------ |
112e41f4b71Sopenharmony_ci| b        | bool     | Event parameter value of the bool type.    |
113e41f4b71Sopenharmony_ci| i8       | int8_t   | Event parameter value of the int8_t type.  |
114e41f4b71Sopenharmony_ci| ui8      | uint8_t  | Event parameter value of the uint8_t type. |
115e41f4b71Sopenharmony_ci| i16      | int16_t  | Event parameter value of the int16_t type. |
116e41f4b71Sopenharmony_ci| ui16     | uint16_t | Event parameter value of the uint16_t type.|
117e41f4b71Sopenharmony_ci| i32      | int32_t  | Event parameter value of the int32_t type. |
118e41f4b71Sopenharmony_ci| ui32     | uint32_t | Event parameter value of the uint32_t type.|
119e41f4b71Sopenharmony_ci| i64      | int64_t  | Event parameter value of the int64_t type. |
120e41f4b71Sopenharmony_ci| ui64     | uint64_t | Event parameter value of the uint64_t type.|
121e41f4b71Sopenharmony_ci| f        | float    | Event parameter value of the float type.   |
122e41f4b71Sopenharmony_ci| d        | double   | Event parameter value of the double type.  |
123e41f4b71Sopenharmony_ci| s        | char*    | Event parameter value of the char* type.   |
124e41f4b71Sopenharmony_ci| array    | void*    | Event parameter value of the array type.    |
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci#### Kernel Event Logging APIs
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ciKernel event logging is implemented using the APIs provided in the following table. For details, see the [API Header File](/kernel/linux/linux-5.10/include/dfx/hiview_hisysevent.h).
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci**Table 9** Description of kernel event logging APIs
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci| API                                                      | Description                                |
133e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ----------------------------------- |
134e41f4b71Sopenharmony_ci| struct hiview_hisysevent *hisysevent_create(const char *domain, const char *name, enum hisysevent_type type); | Creates a **hisysevent** object.   |
135e41f4b71Sopenharmony_ci| void hisysevent_destroy(struct hiview_hisysevent *event);    | Destroys a **hisysevent** object.                                                    |
136e41f4b71Sopenharmony_ci| 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.  |
137e41f4b71Sopenharmony_ci| 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.|
138e41f4b71Sopenharmony_ci| int hisysevent_write(struct hiview_hisysevent *event);       | Flushes **hisysevent** object data to the event file.                                               |
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**Table 10** Description of hisysevent_type enums
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci| Event Type  | Description       |
143e41f4b71Sopenharmony_ci| --------- | ----------- |
144e41f4b71Sopenharmony_ci| FAULT     | Fault event.|
145e41f4b71Sopenharmony_ci| STATISTIC | Statistical event.|
146e41f4b71Sopenharmony_ci| SECURITY  | Security event.|
147e41f4b71Sopenharmony_ci| BEHAVIOR  | Behavior event.|
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci### How to Develop
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci#### C++ Event Logging
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ciCall the event logging API wherever needed, with required event parameters passed to the API.
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci   ```c++
156e41f4b71Sopenharmony_ci   HiSysEventWrite(HiSysEvent::Domain::AAFWK, "START_APP", HiSysEvent::EventType::BEHAVIOR, "APP_NAME", "com.ohos.demo");
157e41f4b71Sopenharmony_ci   ```
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci#### C Event Logging
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci1. If you want to pass custom event parameters to the event logging API, create an event parameter object based on the event parameter type and add the object to the event parameter array.
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci   ```c
164e41f4b71Sopenharmony_ci   // Create an event parameter of the int32_t type.
165e41f4b71Sopenharmony_ci   HiSysEventParam param1 = {
166e41f4b71Sopenharmony_ci       .name = "KEY_INT32",
167e41f4b71Sopenharmony_ci       .t = HISYSEVENT_INT32,
168e41f4b71Sopenharmony_ci       .v = { .i32 = 1 },
169e41f4b71Sopenharmony_ci       .arraySize = 0,
170e41f4b71Sopenharmony_ci   };
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci   // Create an event parameter of the int32_t array type.
173e41f4b71Sopenharmony_ci   int32_t int32Arr[] = { 1, 2, 3 };
174e41f4b71Sopenharmony_ci   HiSysEventParam param2 = {
175e41f4b71Sopenharmony_ci       .name = "KEY_INT32_ARR",
176e41f4b71Sopenharmony_ci       .t = HISYSEVENT_INT32_ARRAY,
177e41f4b71Sopenharmony_ci       .v = { .array = int32Arr },
178e41f4b71Sopenharmony_ci       .arraySize = sizeof(int32Arr) / sizeof(int32Arr[0]),
179e41f4b71Sopenharmony_ci   };
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci   // Add the event parameter object to the created event parameter array.
182e41f4b71Sopenharmony_ci   HiSysEventParam params[] = { param1, param2 };
183e41f4b71Sopenharmony_ci   ```
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci2. Call the event logging API wherever needed, with required event parameters passed to the API.
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci   ```c
188e41f4b71Sopenharmony_ci   OH_HiSysEvent_Write("TEST_DOMAIN", "TEST_NAME", HISYSEVENT_BEHAVIOR, params, sizeof(params) / sizeof(params[0]));
189e41f4b71Sopenharmony_ci   ```
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci#### Kernel Event Logging
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci1. Create a **hisysevent** object based on the specified event domain, event name, and event type.
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci   ```c
196e41f4b71Sopenharmony_ci   struct hiview_hisysevent *event = hisysevent_create("KERNEL", "BOOT", BEHAVIOR);
197e41f4b71Sopenharmony_ci   ```
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci2. Pass the customized event parameters to the **hisysevent** object.
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci   ```c
202e41f4b71Sopenharmony_ci   // Add a parameter of the integer type.
203e41f4b71Sopenharmony_ci   hisysevent_put_integer(event, "BOOT_TIME", 100);
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci   // Add a parameter of the string type.
206e41f4b71Sopenharmony_ci   hisysevent_put_string(event, "MSG", "This is a test message");
207e41f4b71Sopenharmony_ci   ```
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci3. Trigger reporting of the **hisysevent** event.
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci   ```c
212e41f4b71Sopenharmony_ci   hisysevent_write(event);
213e41f4b71Sopenharmony_ci   ```
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ci4. Manually destroy the **hisysevent** object.
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci   ```c
218e41f4b71Sopenharmony_ci   hisysevent_destroy(&event);
219e41f4b71Sopenharmony_ci   ```
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci#### Shielding of Event Logging by Event Domain
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci1. 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:
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci- 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.
226e41f4b71Sopenharmony_ci   ```c++
227e41f4b71Sopenharmony_ci   #define DOMAIN_MASKS "DOMAIN_NAME_1|DOMAIN_NAME_2|...|DOMAIN_NAME_n"
228e41f4b71Sopenharmony_ci   #include "hisysevent.h"
229e41f4b71Sopenharmony_ci   ```
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci- Shielding event logging for event domains of the entire module: Define the **DOMAIN_MASKS** macro in the **BUILD.gn** file of the module.
232e41f4b71Sopenharmony_ci   ```gn
233e41f4b71Sopenharmony_ci   config("module_a"){
234e41f4b71Sopenharmony_ci     cflags_cc += ["-DDOMAIN_MASKS=\"DOMAIN_NAME_1|DOMAIN_NAME_2|...|DOMAIN_NAME_n\""]
235e41f4b71Sopenharmony_ci   }
236e41f4b71Sopenharmony_ci   ```
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci- Shielding event logging for event domains globally: Define the **DOMAIN_MASKS** macro in **/build/config/compiler/BUILD.gn**.
239e41f4b71Sopenharmony_ci   ```gn
240e41f4b71Sopenharmony_ci     cflags_cc += ["-DDOMAIN_MASKS=\"DOMAIN_NAME_1|DOMAIN_NAME_2|...|DOMAIN_NAME_n\""]
241e41f4b71Sopenharmony_ci   ```
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci2. Perform event logging by using the **HiSysEventWrite** API.
244e41f4b71Sopenharmony_ci   ```c++
245e41f4b71Sopenharmony_ci   constexpr char DOMAIN[] = "DOMAIN_NAME_1";
246e41f4b71Sopenharmony_ci   const std::string eventName = "EVENT_NAME1";
247e41f4b71Sopenharmony_ci   OHOS:HiviewDFX::HiSysEvent::EventType eventType = OHOS:HiviewDFX::HiSysEvent::EventType::FAULT;
248e41f4b71Sopenharmony_ci   HiSysEventWrite(domain, eventName, eventType); // Event logging is shielded for DOMAIN_NAME_1 because it has been defined in the DOMAIN_MASKS macro.
249e41f4b71Sopenharmony_ci   ```
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ci### Development Example
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ci#### C++ Event Logging
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ciAssume 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:
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ci1. Add the HiSysEvent component dependency to the **BUILD.gn** file of the service module.
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ci   ```c++
260e41f4b71Sopenharmony_ci   external_deps = [ "hisysevent:libhisysevent" ]
261e41f4b71Sopenharmony_ci   ```
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci2. In the application startup function **StartAbility()** of the service module, call the event logging API with event parameters passed in.
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci   ```c++
266e41f4b71Sopenharmony_ci   #include "hisysevent.h"
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci   int StartAbility()
269e41f4b71Sopenharmony_ci   {
270e41f4b71Sopenharmony_ci       ... // Other service logic
271e41f4b71Sopenharmony_ci       int ret = HiSysEventWrite(HiSysEvent::Domain::AAFWK, "START_APP", HiSysEvent::EventType::BEHAVIOR, "APP_NAME", "com.ohos.demo");
272e41f4b71Sopenharmony_ci       ... // Other service logic
273e41f4b71Sopenharmony_ci   }
274e41f4b71Sopenharmony_ci   ```
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci#### C Event Logging
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ciAssume 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:
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci1. Add the HiSysEvent component dependency to the **BUILD.gn** file of the service module.
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci   ```c++
283e41f4b71Sopenharmony_ci   external_deps = [ "hisysevent:libhisysevent" ]
284e41f4b71Sopenharmony_ci   ```
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci2. In the application startup function **StartAbility()** of the service module, call the event logging API with event parameters passed in.
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci   ```c
289e41f4b71Sopenharmony_ci   #include "hisysevent_c.h"
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci   int StartAbility()
292e41f4b71Sopenharmony_ci   {
293e41f4b71Sopenharmony_ci       ... // Other service logic
294e41f4b71Sopenharmony_ci       char packageName[] = "com.ohos.demo";
295e41f4b71Sopenharmony_ci       HiSysEventParam param = {
296e41f4b71Sopenharmony_ci           .name = "APP_NAME",
297e41f4b71Sopenharmony_ci           .t = HISYSEVENT_STRING,
298e41f4b71Sopenharmony_ci           .v = { .s = packageName },
299e41f4b71Sopenharmony_ci           .arraySize = 0,
300e41f4b71Sopenharmony_ci       };
301e41f4b71Sopenharmony_ci       HiSysEventParam params[] = { param };
302e41f4b71Sopenharmony_ci       int ret = OH_HiSysEvent_Write("AAFWK", "START_APP", HISYSEVENT_BEHAVIOR, params, sizeof(params) / sizeof(params[0]));
303e41f4b71Sopenharmony_ci       ... // Other service logic
304e41f4b71Sopenharmony_ci   }
305e41f4b71Sopenharmony_ci   ```
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_ci#### Kernel Event Logging
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ciAssume 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:
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ci1. In the device startup function **device_boot()**, construct a **hisysevent** object. After that, trigger event reporting, and then destroy the **hisysevent** object.
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci    ```c
314e41f4b71Sopenharmony_ci    #include <dfx/hiview_hisysevent.h>
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_ci    #include <linux/errno.h>
317e41f4b71Sopenharmony_ci    #include <linux/printk.h>
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci    int device_boot()
320e41f4b71Sopenharmony_ci    {
321e41f4b71Sopenharmony_ci        ... // Other service logic
322e41f4b71Sopenharmony_ci        struct hiview_hisysevent *event = NULL;
323e41f4b71Sopenharmony_ci        int ret = 0;
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ci        event = hisysevent_create("KERNEL", "BOOT", BEHAVIOR);
326e41f4b71Sopenharmony_ci        if (!event) {
327e41f4b71Sopenharmony_ci            pr_err("failed to create event");
328e41f4b71Sopenharmony_ci            return -EINVAL;
329e41f4b71Sopenharmony_ci        }
330e41f4b71Sopenharmony_ci        ret = hisysevent_put_string(event, "MSG", "This is a test message");
331e41f4b71Sopenharmony_ci        if (ret != 0) {
332e41f4b71Sopenharmony_ci            pr_err("failed to put sting to event, ret=%d", ret);
333e41f4b71Sopenharmony_ci            goto hisysevent_end;
334e41f4b71Sopenharmony_ci        }
335e41f4b71Sopenharmony_ci        ret = hisysevent_write(event);
336e41f4b71Sopenharmony_ci
337e41f4b71Sopenharmony_ci    hisysevent_end:
338e41f4b71Sopenharmony_ci        hisysevent_destroy(&event);
339e41f4b71Sopenharmony_ci        ... // Other service logic
340e41f4b71Sopenharmony_ci    }
341e41f4b71Sopenharmony_ci    ```
342e41f4b71Sopenharmony_ci
343e41f4b71Sopenharmony_ci#### Shielding of Event Logging by Event Domain
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci- 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.
346e41f4b71Sopenharmony_ci    ```c++
347e41f4b71Sopenharmony_ci    #define DOMAIN_MASKS "AAFWK|POWER"
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_ci    #include "hisysevent.h"
350e41f4b71Sopenharmony_ci    ... // Other service logic
351e41f4b71Sopenharmony_ci    HiSysEventWrite(OHOS:HiviewDFX::HiSysEvent::Domain::AAFWK, "JS_ERROR", OHOS:HiviewDFX::HiSysEvent::EventType::FAULT, "MODULE", "com.ohos.module"); // HiSysEvent logging is not performed.
352e41f4b71Sopenharmony_ci    ... // Other service logic
353e41f4b71Sopenharmony_ci    HiSysEventWrite(OHOS:HiviewDFX::HiSysEvent::Domain::POWER, "POWER_RUNNINGLOCK", OHOS:HiviewDFX::HiSysEvent::EventType::FAULT, "NAME", "com.ohos.module"); // HiSysEvent logging is not performed.
354e41f4b71Sopenharmony_ci    ```
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ci- 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.
357e41f4b71Sopenharmony_ci    ```gn
358e41f4b71Sopenharmony_ci    config("module_a") {
359e41f4b71Sopenharmony_ci        ... // Other configuration items
360e41f4b71Sopenharmony_ci        cflags_cc += ["-DDOMAIN_MASKS=\"AAFWK|POWER\""]
361e41f4b71Sopenharmony_ci    }
362e41f4b71Sopenharmony_ci    ```
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci- 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**.
365e41f4b71Sopenharmony_ci    ```gn
366e41f4b71Sopenharmony_ci    ... // Other configuration items
367e41f4b71Sopenharmony_ci    cflags_cc += ["-DDOMAIN_MASKS=\"AAFWK|POWER\""]
368e41f4b71Sopenharmony_ci    ```
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci# Reference
371e41f4b71Sopenharmony_ci
372e41f4b71Sopenharmony_ciThe 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).
373