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