• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HiAppEvent FAQs
2
3<!--Kit: Performance Analysis Kit-->
4<!--Subsystem: HiviewDFX-->
5<!--Owner: @liujiaxing2024-->
6<!--Designer: @junjie_shi-->
7<!--Tester: @gcw_KuLfPSbe-->
8<!--Adviser: @foryourself-->
9
10## What should I do if the subscribed events cannot be found?
11
12**Symptom**
13
14In the development and debugging phase, when a fault such as crash or freeze occurs, the events subscribed through HiAppEvent cannot be obtained in the **HiLog** window of DevEco Studio.
15
16**Possible Causes and Solutions**
17
18The application exits after the crash or freeze event occurs.
19
20You can restart the application and view the event content.
21
22
23## What should I do if the external_log log file cannot be obtained?
24
25**Symptom**
26
27The following log is displayed in HiLog:
28
29- eventInfo.params.external_log=[]
30
31- HiAppEvent file does not exist
32
33**Possible Causes and Solutions**
34
35**Scenario 1**
36
37The directory containing the **external_log** file reaches its space limit.
38
39The **external_log** file is stored in the [application sandbox](../file-management/app-sandbox-directory.md), and the directory space is limited. You can use the **log_over_limit** field to determine whether the directory containing the external_log file reaches its space limit. If the value of **log_over_limit** is **true**, the directory reaches its space limit and the log file cannot be written.
40
41**external_log** is a string array. For example:
42
43**external_log=["/data/storage/el2/log/hiappevent/APP_CRASH_timestamp_xxxx.log"]**.
44
45**Solutions**
46
47Clear historical log files by referring to the solution in [What should I do if the external_log file cannot be deleted](#what-should-i-do-if-the-external_log-file-cannot-be-deleted).
48
49**Scenario 2**
50
51Some system events (such as time-consuming launch events) do not have **external_log** files.
52
53**Solutions**
54
55Check whether the event contains **external_log** by referring to [System Events](event-subscription-overview.md#system-events).
56
57**Scenario 3**
58
59The fault event occurs before the event subscription.
60
61System events are not subscribed to before the **addWatcher()** API is called. Therefore, the events before event subscription do not have the **external_log** file.
62
63**Solutions**
64
65Check whether the event occurs before the event subscription. The **external_log** file of an event can be obtained only when the event occurs after event subscription.
66
67**Scenario 4**
68
69The system event fails to be triggered.
70
71If no system event occurs, no **external_log** file is generated.
72
73**Solutions**
74
75Obtain other logs of the system event to check whether the system event is triggered successfully.
76
77**Scenario 5**
78
79The **external_log** log file is deleted after being generated.
80
81For example, the system event C is subscribed to by modules A and B in an application. After processing the callback of system event C, module A deletes the **external_log** file. When module B accesses the **external_log** file in the callback of system event C, a message is displayed, indicating that the log file does not exist.
82
83**Solutions**
84
85Check whether the **external_log** file is deleted by other modules.
86
87
88## What should I do if the external_log file cannot be deleted
89
90**Symptom**
91
92The directory containing the **external_log** files reaches its space limit, but the **external_log** files cannot be deleted.
93
94**Solution**
95
96- If you have the permission to access the **/data/app** directory of the device, you can manually delete the **external_log** files. File directory:
97  **/data/app/el2/100/log/application bundle name/hiappevent** (or **resourcelimit** or **watchdog**).
98
99- If you do not have the permission to access the **/data/app** directory of the device, you can delete the **external_log** file from the application code. The sample code is as follows: For details about the file deletion API, see [fs.unlink](../reference/apis-core-file-kit/js-apis-file-fs.md#fsunlink).
100
101**Code Example**
102
103```ts
104import { fileIo as fs } from '@kit.CoreFileKit';
105import { BusinessError } from '@kit.BasicServicesKit';
106
107if (eventInfo.params['external_log'] != undefined) {
108  for (let index = 0; index < eventInfo.params['external_log'].length; ++index) {
109    let externalLog: string = eventInfo.params['external_log'][index];
110    hilog.info(0x0000, 'testTag', `externalLog=${externalLog}`);
111    // Verify the access permission.
112    let res = fs.accessSync(externalLog);
113    if (res) {
114      hilog.info(0x0000, 'testTag', `HiAppEvent file exists`);
115    } else {
116      hilog.error(0x0000, 'testTag', `HiAppEvent file does not exist`);
117    }
118    // Verify the read and write permissions.
119    fs.open(externalLog, fs.OpenMode.READ_WRITE).then((file: fs.File) => {
120      hilog.info(0x0000, 'testTag', `HiAppEvent file=${externalLog} fd=${file.fd}`);
121      fs.closeSync(file);
122    }).catch((err: BusinessError) => {
123      hilog.info(0x0000, 'testTag',
124        `HiAppEvent open file=${externalLog} failed with error message=${err.message}, error code=${err.code}`);
125    });
126    // Delete the external_log file.
127    fs.unlink(externalLog).then(() => {
128      console.info("HiAppEvent remove file:" + externalLog + " succeed");
129    }).catch((err: BusinessError) => {
130      console.error("HiAppEvent remove file:" + externalLog + " failed with error message: " + err.message +
131        ", error code: " + err.code);
132    });
133  }
134}
135```
136
137Log about accessing and deleting the **external_log** file:
138
139```text
140externalLog=/data/storage/el2/log/hiappevent/APP_CRASH_1751081104816_35595.log
141HiAppEvent file exists
142HiAppEvent file=/data/storage/el2/log/hiappevent/APP_CRASH_1751081104816_35595.log fd=61
143HiAppEvent remove file:/data/storage/el2/log/hiappevent/APP_CRASH_1751081104816_35595.log succeed
144```
145
146
147> **NOTE**
148>
149> The path of **external_log** is the application sandbox directory, not the actual physical path. The application has the permission to access its own sandbox directory. The **external_log** space is limited. After processing log files, delete them in a timely manner.
150
151
152## Does a thread or process receive only its own event callbacks within the same application?
153
154No, the thread or process also receives event callbacks from other threads or processes within the same application. For example, if an application has two processes (process A and process B) and process A has called **addWatcher()** to subscribe to crash events, process A can receive the crash callback when process B crashes. As long as the application names of processes A and B are the same, the callback can be received.
155
156**API Reference**
157
158[hiAppEvent.addWatcher](../reference/apis-performance-analysis-kit/js-apis-hiviewdfx-hiappevent.md#hiappeventaddwatcher)
159