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