• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.faultLogger (FaultLogger)
2
3> **NOTE**
4>
5> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
6> This module is deprecated since API version 18. In later versions, you are advised to use HiAppEvent to subscribe to the **APP_CRASH** and **APP_FREEZE** events.
7
8## Modules to Import
9
10```ts
11import { FaultLogger } from '@kit.PerformanceAnalysisKit';
12```
13
14## FaultType
15
16Enumerates the fault types.
17
18**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
19
20| Name| Value| Description|
21| -------- | -------- | -------- |
22| NO_SPECIFIC | 0 | No specific fault type.|
23| CPP_CRASH | 2 | C++ program crash.|
24| JS_CRASH | 3 | JS program crash.|
25| APP_FREEZE | 4 | Application freezing.|
26
27## FaultLogInfo
28
29Defines the data structure of the fault log information.
30
31**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
32
33| Name| Type| Mandatory| Description|
34| -------- | -------- | -------- | -------- |
35| pid | number | Yes| Process ID of the faulty process.|
36| uid | number | Yes| User ID of the faulty process.|
37| type | [FaultType](#faulttype) | Yes| Fault type.|
38| timestamp | number | Yes| Millisecond-level timestamp when the log was generated.|
39| reason | string | Yes| Reason for the fault.|
40| module | string | Yes| Module on which the fault occurred.|
41| summary | string | Yes| Summary of the fault.|
42| fullLog | string | Yes| Full log text.|
43
44## FaultLogger.query<sup>9+</sup>
45
46query(faultType: FaultType, callback: AsyncCallback&lt;Array&lt;FaultLogInfo&gt;&gt;) : void
47
48Obtains the fault information about the current process. This API uses an asynchronous callback to return the fault information array obtained, which contains a maximum of 10 pieces of fault information.
49
50**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
51
52**Parameters**
53
54| Name| Type| Mandatory| Description|
55| -------- | -------- | -------- | -------- |
56| faultType | [FaultType](#faulttype) | Yes| Fault type.|
57| callback | AsyncCallback&lt;Array&lt;[FaultLogInfo](#faultloginfo)&gt;&gt; | Yes| Callback used to return the fault information array.<br>**value** is the fault information array obtained. If **value** is **undefined**, an exception occurs during the information retrieval. In this case, an error string will be returned.
58
59**Error codes**
60
61For details about the error codes, see [FaultLogger Error Codes](errorcode-faultlogger.md).
62
63| ID| Error Message|
64| --- | --- |
65| 401 | The parameter check failed, Parameter type error |
66| 801 | The specified SystemCapability name was not found |
67| 10600001 | The service is not started or is faulty |
68
69**Example**
70
71```ts
72import { FaultLogger } from '@kit.PerformanceAnalysisKit';
73import { BusinessError } from '@kit.BasicServicesKit';
74
75function queryFaultLogCallback(error: BusinessError, value: Array<FaultLogger.FaultLogInfo>) {
76    if (error) {
77        console.info('error is ' + error);
78    } else {
79        console.info("value length is " + value.length);
80        let len: number = value.length;
81        for (let i = 0; i < len; i++) {
82            console.info("log: " + i);
83            console.info("Log pid: " + value[i].pid);
84            console.info("Log uid: " + value[i].uid);
85            console.info("Log type: " + value[i].type);
86            console.info("Log timestamp: " + value[i].timestamp);
87            console.info("Log reason: " + value[i].reason);
88            console.info("Log module: " + value[i].module);
89            console.info("Log summary: " + value[i].summary);
90            console.info("Log text: " + value[i].fullLog);
91        }
92    }
93}
94try {
95    FaultLogger.query(FaultLogger.FaultType.JS_CRASH, queryFaultLogCallback);
96} catch (err) {
97    console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`);
98}
99```
100
101## FaultLogger.query<sup>9+</sup>
102
103query(faultType: FaultType) : Promise&lt;Array&lt;FaultLogInfo&gt;&gt;
104
105Obtains the fault information about the current process. This API uses a promise to return the fault information array obtained, which contains a maximum of 10 pieces of fault information.
106
107**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
108
109**Parameters**
110
111| Name| Type| Mandatory| Description|
112| -------- | -------- | -------- | -------- |
113| faultType | [FaultType](#faulttype) | Yes| Fault type.|
114
115**Return value**
116
117| Type| Description|
118| -------- | -------- |
119| Promise&lt;Array&lt;[FaultLogInfo](#faultloginfo)&gt;&gt; | Promise used to return the fault information array. You can obtain the fault information instance in its **then()** method or use **await**.<br>**value** is the fault information array obtained. If **value** is **undefined**, an exception occurs during the information retrieval.|
120
121**Error codes**
122
123For details about the error codes, see [FaultLogger Error Codes](errorcode-faultlogger.md).
124
125| ID| Error Message|
126| --- | --- |
127| 401 | The parameter check failed, Parameter type error |
128| 801 | The specified SystemCapability name was not found |
129| 10600001 | The service is not started or is faulty |
130
131**Example**
132
133```ts
134import { FaultLogger } from '@kit.PerformanceAnalysisKit';
135import { BusinessError } from '@kit.BasicServicesKit';
136
137async function getLog() {
138  try {
139    let value: Array<FaultLogger.FaultLogInfo> = await FaultLogger.query(FaultLogger.FaultType.JS_CRASH);
140    if (value) {
141      console.info("value length is " + value.length);
142      let len: number = value.length;
143      for (let i = 0; i < len; i++) {
144        console.info("log: " + i);
145        console.info("Log pid: " + value[i].pid);
146        console.info("Log uid: " + value[i].uid);
147        console.info("Log type: " + value[i].type);
148        console.info("Log timestamp: " + value[i].timestamp);
149        console.info("Log reason: " + value[i].reason);
150        console.info("Log module: " + value[i].module);
151        console.info("Log summary: " + value[i].summary);
152        console.info("Log text: " + value[i].fullLog);
153      }
154    }
155  } catch (err) {
156    console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`);
157  }
158}
159```
160
161## FaultLogger.querySelfFaultLog<sup>(deprecated)</sup>
162
163querySelfFaultLog(faultType: FaultType, callback: AsyncCallback&lt;Array&lt;FaultLogInfo&gt;&gt;) : void
164
165> **NOTE**
166>
167> This API is deprecated since API version 9. You are advised to use [FaultLogger.query](#faultloggerquery9).
168
169Obtains the fault information about the current process. This API uses an asynchronous callback to return the fault information array obtained, which contains a maximum of 10 pieces of fault information.
170
171**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
172
173**Parameters**
174
175| Name| Type| Mandatory| Description|
176| -------- | -------- | -------- | -------- |
177| faultType | [FaultType](#faulttype) | Yes| Fault type.|
178| callback | AsyncCallback&lt;Array&lt;[FaultLogInfo](#faultloginfo)&gt;&gt; | Yes| Callback used to return the fault information array.<br>**value** is the fault information array obtained. If **value** is **undefined**, an exception occurs during the information retrieval. In this case, an error string will be returned.
179
180**Example**
181
182```ts
183import { FaultLogger } from '@kit.PerformanceAnalysisKit';
184import { BusinessError } from '@kit.BasicServicesKit';
185
186function queryFaultLogCallback(error: BusinessError, value: Array<FaultLogger.FaultLogInfo>) {
187  if (error) {
188    console.info('error is ' + error);
189  } else {
190    console.info("value length is " + value.length);
191    let len: number = value.length;
192    for (let i = 0; i < len; i++) {
193      console.info("log: " + i);
194      console.info("Log pid: " + value[i].pid);
195      console.info("Log uid: " + value[i].uid);
196      console.info("Log type: " + value[i].type);
197      console.info("Log timestamp: " + value[i].timestamp);
198      console.info("Log reason: " + value[i].reason);
199      console.info("Log module: " + value[i].module);
200      console.info("Log summary: " + value[i].summary);
201      console.info("Log text: " + value[i].fullLog);
202    }
203  }
204}
205FaultLogger.querySelfFaultLog(FaultLogger.FaultType.JS_CRASH, queryFaultLogCallback);
206```
207
208## FaultLogger.querySelfFaultLog<sup>(deprecated)</sup>
209
210querySelfFaultLog(faultType: FaultType) : Promise&lt;Array&lt;FaultLogInfo&gt;&gt;
211
212> **NOTE**
213>
214> This API is deprecated since API version 9. You are advised to use [FaultLogger.query](#faultloggerquery9-1).
215
216Obtains the fault information about the current process. This API uses a promise to return the fault information array obtained, which contains a maximum of 10 pieces of fault information.
217
218**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
219
220**Parameters**
221
222| Name| Type| Mandatory| Description|
223| -------- | -------- | -------- | -------- |
224| faultType | [FaultType](#faulttype) | Yes| Fault type.|
225
226**Return value**
227
228| Type| Description|
229| -------- | -------- |
230| Promise&lt;Array&lt;[FaultLogInfo](#faultloginfo)&gt;&gt; | Promise used to return the fault information array. You can obtain the fault information instance in its **then()** method or use **await**.<br>**value** is the fault information array obtained. If **value** is **undefined**, an exception occurs during the information retrieval.|
231
232**Example**
233
234```ts
235import { FaultLogger } from '@kit.PerformanceAnalysisKit';
236
237async function getLog() {
238  let value: Array<FaultLogger.FaultLogInfo> = await FaultLogger.querySelfFaultLog(FaultLogger.FaultType.JS_CRASH);
239  if (value) {
240    console.info("value length is " + value.length);
241    let len: number = value.length;
242    for (let i = 0; i < len; i++) {
243      console.info("log: " + i);
244      console.info("Log pid: " + value[i].pid);
245      console.info("Log uid: " + value[i].uid);
246      console.info("Log type: " + value[i].type);
247      console.info("Log timestamp: " + value[i].timestamp);
248      console.info("Log reason: " + value[i].reason);
249      console.info("Log module: " + value[i].module);
250      console.info("Log summary: " + value[i].summary);
251      console.info("Log text: " + value[i].fullLog);
252    }
253  }
254}
255```
256