1# @ohos.hiSysEvent (系统事件打点)(系统接口)
2
3本模块提供了系统事件打点能力,包括系统事件的埋点、落盘系统事件的订阅及已落盘的系统事件的查询能力。
4
5> **说明:**
6>
7> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> - 本模块为系统接口。
9
10## 导入模块
11
12```ts
13import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
14```
15
16## EventType
17
18系统事件类型枚举。
19
20**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
21
22| 名称 | 值 | 说明 |
23| -------- | -------- | -------- |
24| FAULT | 1 | 错误事件类型。 |
25| STATISTIC | 2 | 统计事件类型。 |
26| SECURITY | 3 | 安全事件类型。 |
27| BEHAVIOR | 4 | 用户行为事件类型。 |
28
29## SysEventInfo
30
31系统事件信息对象接口。
32
33**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
34
35| 名称 | 类型 | 必填 | 说明 |
36| -------- | -------- | -------- | -------- |
37| domain | string | 是 | 事件领域。 |
38| name | string | 是 | 事件名称。 |
39| eventType | [EventType](#eventtype) | 是 | 事件类型。 |
40| params | object | 否 | 事件参数。 |
41
42
43## hiSysEvent.write
44
45write(info: SysEventInfo, callback: AsyncCallback<void>): void
46
47系统事件打点方法,接收[SysEventInfo](#syseventinfo)类型的对象作为事件参数,使用callback方式作为异步回调。
48
49**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
50
51**参数:**
52
53| 参数名    | 类型                      | 必填 | 说明                                                         |
54| --------- | ------------------------- | ---- | ------------------------------------------------------------ |
55| info | [SysEventInfo](#syseventinfo) | 是 | 系统事件。 |
56| callback  | AsyncCallback&lt;void&gt; | 是 | 回调函数,可以在回调函数中处理接口返回值。<br/>- 0表示事件校验成功,事件正常异步写入事件文件;<br/>- 正值表示事件打点存在异常,但可以正常写入;<br/>- 负值表示事件打点失败。 |
57
58**错误码:**
59
60以下错误码的详细介绍请参见[系统事件错误码](errorcode-hisysevent-sys.md)。
61
62| 错误码ID | 错误信息 |
63| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
64| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
65| 11200001 | Invalid event domain.                                                                                                                           |
66| 11200002 | Invalid event name.                                                                                                                             |
67| 11200003 | Abnormal environment.                                                                                                                           |
68| 11200004 | The event length exceeds the limit.                                                                                                             |
69| 11200051 | Invalid event parameter.                                                                                                                        |
70| 11200052 | The size of the event parameter of the string type exceeds the limit.                                                                           |
71| 11200053 | The number of event parameters exceeds the limit.                                                                                               |
72| 11200054 | The number of event parameters of the array type exceeds the limit.                                                                             |
73
74**示例:**
75
76```ts
77import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
78import { BusinessError } from '@kit.BasicServicesKit';
79
80try {
81  let customizedParams: Record<string, string | number> = {
82    'PID': 487,
83    'UID': 103,
84    'PACKAGE_NAME': "com.ohos.hisysevent.test",
85    'PROCESS_NAME': "syseventservice",
86    'MSG': "no msg."
87  };
88  let eventInfo: hiSysEvent.SysEventInfo = {
89    domain: "RELIABILITY",
90    name: "STACK",
91    eventType: hiSysEvent.EventType.FAULT,
92    params: customizedParams
93  };
94  hiSysEvent.write(eventInfo, (err: BusinessError) => {
95    // do something here.
96  });
97} catch (err) {
98  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
99}
100```
101
102
103## hiSysEvent.write
104
105write(info: SysEventInfo): Promise&lt;void&gt;
106
107系统事件打点方法,接收[SysEventInfo](#syseventinfo)类型的对象作为事件参数,使用promise方式作为异步回调。
108
109**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
110
111**参数:**
112
113| 参数名    | 类型                    | 必填 | 说明 |
114| --------- | ----------------------- | ---- | --------------- |
115| info | [SysEventInfo](#syseventinfo) | 是   | 系统事件。 |
116
117**返回值:**
118
119| 类型                | 说明                                                         |
120| ------------------- | ------------------------------------------------------------ |
121| Promise&lt;void&gt; | Promise实例,可以在其then()、catch()方法中分别对系统事件写入成功、写入异常的回调进行处理。 |
122
123**错误码:**
124
125以下错误码的详细介绍请参见[系统事件错误码](errorcode-hisysevent-sys.md)。
126
127| 错误码ID | 错误信息 |
128| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
129| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
130| 11200001 | Invalid event domain.                                                                                                                           |
131| 11200002 | Invalid event name.                                                                                                                             |
132| 11200003 | Abnormal environment.                                                                                                                           |
133| 11200004 | The event length exceeds the limit.                                                                                                             |
134| 11200051 | Invalid event parameter.                                                                                                                        |
135| 11200052 | The size of the event parameter of the string type exceeds the limit.                                                                           |
136| 11200053 | The number of event parameters exceeds the limit.                                                                                               |
137| 11200054 | The number of event parameters of the array type exceeds the limit.                                                                             |
138
139**示例:**
140
141```ts
142import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
143import { BusinessError } from '@kit.BasicServicesKit';
144
145try {
146  let customizedParams: Record<string, string | number> = {
147    'PID': 487,
148    'UID': 103,
149    'PACKAGE_NAME': "com.ohos.hisysevent.test",
150    'PROCESS_NAME': "syseventservice",
151    'MSG': "no msg."
152  };
153  let eventInfo: hiSysEvent.SysEventInfo = {
154    domain: "RELIABILITY",
155    name: "STACK",
156    eventType: hiSysEvent.EventType.FAULT,
157    params: customizedParams
158  };
159  hiSysEvent.write(eventInfo).then(
160    () => {
161      // do something here.
162    }
163  ).catch(
164    (err: BusinessError) => {
165      console.error(`error code: ${err.code}, error msg: ${err.message}`);
166    }
167  );
168} catch (err) {
169  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
170}
171```
172
173## RuleType
174
175匹配规则类型枚举。
176
177**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
178
179| 名称 | 值 | 说明 |
180| -------- | -------- | -------- |
181| WHOLE_WORD | 1 | 全词匹配类型。 |
182| PREFIX | 2 | 前缀匹配类型。 |
183| REGULAR | 3 | 正则匹配类型。 |
184
185## WatchRule
186
187系统事件订阅规则对象接口。
188
189**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
190
191| 名称 | 类型 | 必填 | 说明 |
192| -------- | -------- | -------- | -------- |
193| domain | string | 是 | 事件领域。 |
194| name | string | 是 | 事件名称。 |
195| tag | string | 否 | 事件标签。 |
196| ruleType | [RuleType](#ruletype) | 是 | 匹配规则类型。 |
197
198## Watcher
199
200系统事件订阅者对象接口。
201
202**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
203
204| 名称 | 类型 | 必填 | 说明 |
205| -------- | -------- | -------- | -------- |
206| rules | [WatchRule](#watchrule)[] | 是 | 订阅对象数组,每个订阅者对象包含多个订阅规则。 |
207| onEvent | function | 是 | 订阅事件的回调方法(info: [SysEventInfo](#syseventinfo)) => void。 |
208| onServiceDied | function | 是 | 系统事件服务关闭的回调方法() => void。 |
209
210## hiSysEvent.addWatcher
211
212addWatcher(watcher: Watcher): void
213
214订阅系统事件,接收[Watcher](#watcher)类型的对象作为事件参数。
215
216**需要权限:** ohos.permission.READ_DFX_SYSEVENT
217
218**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
219
220**参数:**
221
222| 参数名 | 类型 | 必填 | 说明 |
223| ------ | ----------------------------- | ---- | ------------------------ |
224| watcher | [Watcher](#watcher) | 是 | 系统事件订阅者对象。 |
225
226**错误码:**
227
228以下错误码的详细介绍请参见[系统事件错误码](errorcode-hisysevent-sys.md)。
229
230| 错误码ID | 错误信息 |
231| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
232| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
233| 202      | System API is not allowed called by Non-system application.                                                                                     |
234| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
235| 11200101 | The number of watchers exceeds the limit.                                                                                                       |
236| 11200102 | The number of watch rules exceeds the limit.                                                                                                    |
237
238**示例:**
239
240```ts
241import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
242import { BusinessError } from '@kit.BasicServicesKit';
243
244let watchRules: hiSysEvent.WatchRule[] = [{
245    domain: "RELIABILITY",
246    name: "STACK",
247    tag: "STABILITY",
248    ruleType: hiSysEvent.RuleType.WHOLE_WORD,
249  } as hiSysEvent.WatchRule];
250let watcher: hiSysEvent.Watcher = {
251  rules: watchRules,
252  onEvent: (info: hiSysEvent.SysEventInfo) => {
253    // do something here.
254  },
255  onServiceDied: () => {
256    // do something here.
257  }
258};
259try {
260  hiSysEvent.addWatcher(watcher);
261} catch (err) {
262  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
263}
264```
265
266## hiSysEvent.removeWatcher
267
268removeWatcher(watcher: Watcher): void
269
270取消订阅系统事件,接收[Watcher](#watcher)类型的对象作为事件参数。
271
272**需要权限:** ohos.permission.READ_DFX_SYSEVENT
273
274**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
275
276**参数:**
277
278| 参数名 | 类型  | 必填 | 说明  |
279| ------ | ------------- | ---- | ------------------------- |
280| watcher | [Watcher](#watcher) | 是 | 系统事件订阅者对象。 |
281
282**错误码:**
283
284以下错误码的详细介绍请参见[系统事件错误码](errorcode-hisysevent-sys.md)。
285
286| 错误码ID | 错误信息 |
287| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
288| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
289| 202      | System API is not allowed called by Non-system application.                                                                                     |
290| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
291| 11200201 | The watcher does not exist.                                                                                                                     |
292
293**示例:**
294
295```ts
296import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
297import { BusinessError } from '@kit.BasicServicesKit';
298
299let watchRules: hiSysEvent.WatchRule[] = [{
300    domain: "RELIABILITY",
301    name: "STACK",
302    tag: "STABILITY",
303    ruleType: hiSysEvent.RuleType.WHOLE_WORD,
304  } as hiSysEvent.WatchRule ];
305let watcher: hiSysEvent.Watcher = {
306  rules: watchRules,
307  onEvent: (info: hiSysEvent.SysEventInfo) => {
308    // do something here.
309  },
310  onServiceDied: () => {
311    // do something here.
312  }
313};
314try {
315  hiSysEvent.addWatcher(watcher);
316  hiSysEvent.removeWatcher(watcher);
317} catch (err) {
318  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
319}
320```
321
322## QueryArg
323
324系统事件查询参数对象接口。
325
326**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
327
328| 名称 | 类型 | 必填 | 说明 |
329| -------- | -------- | -------- | -------- |
330| beginTime | number | 是 | 查询的系统事件起始时间(13位时间戳)。 |
331| endTime | number | 是 | 查询的系统事件结束时间(13位时间戳)。 |
332| maxEvents | number | 是 | 查询的系统事件最多条数。 |
333| fromSeq<sup>10+</sup> | number | 否   | 查询的系统事件起始序列号,默认值为-1。 |
334| toSeq<sup>10+</sup> | number | 否   | 查询的系统事件结束序列号,默认值为-1。 |
335
336## QueryRule
337
338系统事件查询规则对象接口。
339
340**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
341
342| 名称 | 类型 | 必填 | 说明 |
343| -------- | -------- | -------- | -------- |
344| domain | string | 是 | 查询包含的事件领域。 |
345| names | string[] | 是 | 查询所包含的多个事件名称,每个查询规则对象包含多个系统事件名称。 |
346| condition<sup>10+</sup> | string | 否 | 事件的额外参数条件,格式:{"version":"V1","condition":{"and":[{"param":"参数","op":"操作符","value":"比较值"}]}} |
347
348## Querier
349
350系统事件查询者对象接口。
351
352**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
353
354| 名称 | 类型 | 必填 | 说明 |
355| -------- | -------- | -------- | -------- |
356| onQuery | function | 是 | 返回查询到的系统事件的回调方法(infos: [SysEventInfo](#syseventinfo)[]) => void。 |
357| onComplete | function | 是 | 查询结果统计的回调方法(reason: number, total: number) => void。 |
358
359## hiSysEvent.query
360
361query(queryArg: QueryArg, rules: QueryRule[], querier: Querier): void
362
363查询系统事件。
364
365**需要权限:** ohos.permission.READ_DFX_SYSEVENT
366
367**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
368
369**参数:**
370
371| 参数名 | 类型 | 必填 | 说明 |
372| ------ | ----------------------------- | ---- | ------------------------ |
373| queryArg | [QueryArg](#queryarg) | 是   | 查询需要配置的查询参数。 |
374| rules | [QueryRule](#queryrule)[] | 是   | 查询规则数组,每次查询可配置多个查询规则。 |
375| querier | [Querier](#querier) | 是   | 查询者对象,包含查询结果及结束的相关回调。 |
376
377**错误码:**
378
379以下错误码的详细介绍请参见[系统事件错误码](errorcode-hisysevent-sys.md)。
380
381| 错误码ID | 错误信息 |
382| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
383| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
384| 202      | System API is not allowed called by Non-system application.                                                                                     |
385| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
386| 11200301 | The number of query rules exceeds the limit.                                                                                                    |
387| 11200302 | Invalid query rule.                                                                                                                             |
388| 11200303 | The number of concurrent queriers exceeds the limit.                                                                                            |
389| 11200304 | The query frequency exceeds the limit.                                                                                                          |
390
391**示例:**
392
393```ts
394import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
395import { BusinessError } from '@kit.BasicServicesKit';
396
397try {
398  let customizedParams: Record<string, string | number> = {
399    'PID': 487,
400    'UID': 103,
401    'PACKAGE_NAME': "com.ohos.hisysevent.test",
402    'PROCESS_NAME': "syseventservice",
403    'MSG': "no msg."
404  };
405  let eventInfo: hiSysEvent.SysEventInfo = {
406    domain: "RELIABILITY",
407    name: "STACK",
408    eventType: hiSysEvent.EventType.FAULT,
409    params: customizedParams
410  };
411  hiSysEvent.write(eventInfo, (err: BusinessError) => {
412    // do something here.
413  });
414
415  let queryArg: hiSysEvent.QueryArg = {
416    beginTime: -1,
417    endTime: -1,
418    maxEvents: 5,
419  };
420  let queryRules: hiSysEvent.QueryRule[] = [{
421    domain: "RELIABILITY",
422    names: ["STACK"],
423  } as hiSysEvent.QueryRule];
424  let querier: hiSysEvent.Querier = {
425    onQuery: (infos: hiSysEvent.SysEventInfo[]) => {
426      // do something here.
427    },
428    onComplete: (reason: number, total: number) => {
429      // do something here.
430    }
431  };
432  hiSysEvent.query(queryArg, queryRules, querier);
433} catch (err) {
434  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
435}
436```
437
438## hiSysEvent.exportSysEvents<sup>10+</sup>
439
440exportSysEvents(queryArg: QueryArg, rules: QueryRule[]): number
441
442批量导出系统事件,以文件格式写入应用沙箱固定目录(/data/storage/el2/base/cache/hiview/event/)。
443
444**需要权限:** ohos.permission.READ_DFX_SYSEVENT
445
446**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
447
448**参数:**
449
450| 参数名   | 类型                      | 必填 | 说明                                       |
451| -------- | ------------------------- | ---- | ------------------------------------------ |
452| queryArg | [QueryArg](#queryarg)     | 是   | 导出需要配置的查询参数。                   |
453| rules    | [QueryRule](#queryrule)[] | 是   | 查询规则数组,每次导出可配置多个查询规则。 |
454
455**返回值:**
456
457| 类型   | 说明             |
458| ------ | ---------------- |
459| number | 接口调用时间戳。 |
460
461**错误码:**
462
463以下错误码的详细介绍请参见[系统事件错误码](errorcode-hisysevent-sys.md)。
464
465| 错误码ID | 错误信息 |
466| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
467| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
468| 202      | System API is not allowed called by Non-system application.                                                                                     |
469| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
470| 11200301 | The number of query rules exceeds the limit.                                                                                                    |
471| 11200302 | Invalid query rule.                                                                                                                             |
472| 11200304 | The query frequency exceeds the limit.                                                                                                          |
473
474**示例:**
475
476```ts
477import { fileIo } from '@kit.CoreFileKit';
478import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
479import { BusinessError } from '@kit.BasicServicesKit';
480
481try {
482  let customizedParams: Record<string, string | number> = {
483    'PID': 487,
484    'UID': 103,
485    'PACKAGE_NAME': "com.ohos.hisysevent.test",
486    'PROCESS_NAME': "syseventservice",
487    'MSG': "no msg."
488  };
489  let eventInfo: hiSysEvent.SysEventInfo = {
490    domain: "RELIABILITY",
491    name: "STACK",
492    eventType: hiSysEvent.EventType.FAULT,
493    params: customizedParams
494  };
495  hiSysEvent.write(eventInfo, (err: BusinessError) => {
496    // do something here.
497  });
498
499  let queryArg: hiSysEvent.QueryArg = {
500    beginTime: -1,
501    endTime: -1,
502    maxEvents: 1,
503  };
504  let queryRules: hiSysEvent.QueryRule[] = [{
505    domain: "RELIABILITY",
506    names: ["STACK"],
507  } as hiSysEvent.QueryRule];
508  let time = hiSysEvent.exportSysEvents(queryArg, queryRules);
509  console.log(`receive export task time is : ${time}`);
510
511  // 延迟读取本次导出的事件
512  setTimeout(() => {
513    let eventDir = '/data/storage/el2/base/cache/hiview/event';
514    let filenames = fileIo.listFileSync(eventDir);
515    for (let i = 0; i < filenames.length; i++) {
516      if (filenames[i].indexOf(time.toString()) != -1) {
517        let res = fileIo.readTextSync(eventDir + '/' + filenames[i]);
518        let events: string = JSON.parse('[' + res.slice(0, res.length - 1) + ']');
519        console.log("read file end, events is :" + JSON.stringify(events));
520      }
521    }
522  }, 10000);
523} catch (err) {
524  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
525}
526```
527
528## hiSysEvent.subscribe<sup>10+</sup>
529
530subscribe(rules: QueryRule[]): number
531
532订阅实时系统事件(事件需满足低频率或偶发性的约束条件),事件发生时立即以文件格式写入应用沙箱固定目录(/data/storage/el2/base/cache/hiview/event/)。
533
534**需要权限:** ohos.permission.READ_DFX_SYSEVENT
535
536**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
537
538**参数:**
539
540| 参数名 | 类型                      | 必填 | 说明                                       |
541| ------ | ------------------------- | ---- | ------------------------------------------ |
542| rules  | [QueryRule](#queryrule)[] | 是   | 查询规则数组,每次订阅可配置多个查询规则。 |
543
544**返回值:**
545
546| 类型   | 说明             |
547| ------ | ---------------- |
548| number | 接口调用时间戳。 |
549
550**错误码:**
551
552以下错误码的详细介绍请参见[系统事件错误码](errorcode-hisysevent-sys.md)。
553
554| 错误码ID | 错误信息 |
555| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
556| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
557| 202      | System API is not allowed called by Non-system application.                                                                                     |
558| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
559| 11200301 | The number of query rules exceeds the limit.                                                                                                    |
560| 11200302 | Invalid query rule.                                                                                                                             |
561
562**示例:**
563
564```ts
565import { fileIo } from '@kit.CoreFileKit';
566import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
567import { BusinessError } from '@kit.BasicServicesKit';
568
569try {
570  let rules: hiSysEvent.QueryRule[] = [{
571    domain: "RELIABILITY",
572    names: ["STACK"],
573  } as hiSysEvent.QueryRule,
574  {
575    domain: "BUNDLE_MANAGER",
576    names: ["BUNDLE_UNINSTALL"],
577  } as hiSysEvent.QueryRule];
578  hiSysEvent.subscribe(rules);
579
580  let customizedParams: Record<string, string | number> = {
581    'PID': 487,
582    'UID': 103,
583    'PACKAGE_NAME': "com.ohos.hisysevent.test",
584    'PROCESS_NAME': "syseventservice",
585    'MSG': "no msg."
586  };
587  let eventInfo: hiSysEvent.SysEventInfo = {
588    domain: "RELIABILITY",
589    name: "STACK",
590    eventType: hiSysEvent.EventType.FAULT,
591    params: customizedParams
592  };
593  hiSysEvent.write(eventInfo, (err: BusinessError) => {
594    // do something here.
595  });
596
597  // 延迟读取订阅的事件
598  setTimeout(() => {
599    let eventDir = '/data/storage/el2/base/cache/hiview/event';
600    let filenames = fileIo.listFileSync(eventDir);
601    for (let i = 0; i < filenames.length; i++) {
602      let res = fileIo.readTextSync(eventDir + '/' + filenames[i]);
603      let events: string = JSON.parse('[' + res.slice(0, res.length - 1) + ']');
604      console.log("read file end, events is :" + JSON.stringify(events));
605    }
606  }, 10000);
607} catch (err) {
608  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
609}
610```
611
612## hiSysEvent.unsubscribe<sup>10+</sup>
613
614unsubscribe(): void
615
616取消订阅系统事件。
617
618**需要权限:** ohos.permission.READ_DFX_SYSEVENT
619
620**系统能力:** SystemCapability.HiviewDFX.HiSysEvent
621
622**错误码:**
623
624以下错误码的详细介绍请参见[系统事件错误码](errorcode-hisysevent-sys.md)。
625
626| 错误码ID | 错误信息 |
627| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
628| 201      | Permission denied. An attempt was made to read system event forbidden by permission: ohos.permission.READ_DFX_SYSEVENT.                         |
629| 202      | System API is not allowed called by Non-system application.                                                                                     |
630| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
631| 11200305 | Failed to unsubscribe.                                                                                                                          |
632
633**示例:**
634
635```ts
636import { hiSysEvent } from '@kit.PerformanceAnalysisKit';
637import { BusinessError } from '@kit.BasicServicesKit';
638
639try {
640  let rules: hiSysEvent.QueryRule[] = [{
641    domain: "RELIABILITY",
642    names: ["STACK"],
643  } as hiSysEvent.QueryRule,
644  {
645    domain: "BUNDLE_MANAGER",
646    names: ["BUNDLE_UNINSTALL"],
647  } as hiSysEvent.QueryRule];
648  hiSysEvent.subscribe(rules);
649  hiSysEvent.unsubscribe();
650} catch (err) {
651  console.error(`error code: ${(err as BusinessError).code}, error msg: ${(err as BusinessError).message}`);
652}
653```
654
655