• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Fault Logger
2
3> **NOTE**<br>
4> 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.
5
6## Modules to Import
7
8```js
9import faultLogger from '@ohos.faultLogger'
10```
11
12
13## FaultType
14
15Enumerates the fault types.
16
17**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
18
19| Name| Default Value| Description|
20| -------- | -------- | -------- |
21| NO_SPECIFIC | 0 | No specific fault type.|
22| CPP_CRASH | 2 | C++ program crash.|
23| JS_CRASH | 3 | JS program crash.|
24| APP_FREEZE | 4 | Application freezing.|
25
26## FaultLogInfo
27
28Defines the data structure of the fault log information.
29
30**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
31
32| Name| Type| Description|
33| -------- | -------- | -------- |
34| pid | number | Process ID of the faulty process.|
35| uid | number | User ID of the faulty process.|
36| type | [FaultType](#faulttype) | Fault type.|
37| timestamp | number | Second-level timestamp when the log was generated.|
38| reason | string | Reason for the fault.|
39| module | string | Module on which the fault occurred.|
40| summary | string | Summary of the fault.|
41| fullLog | string | Full log text.|
42
43## faultLogger.querySelfFaultLog
44
45querySelfFaultLog(faultType: FaultType, callback: AsyncCallback&lt;Array&lt;FaultLogInfo&gt;&gt;) : void
46
47Obtains 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.
48
49**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
50
51**Parameters**
52
53| Name| Type| Mandatory| Description|
54| -------- | -------- | -------- | -------- |
55| faultType | [FaultType](#faulttype) | Yes| Fault type.|
56| callback | AsyncCallbackArray&lt;Array&lt;[FaultLogInfo](#faultloginfo)>> | Yes | Callback used to return the fault information array.<br/>The value is the fault information array obtained. If the value is **undefined**, an exception occurs during the information retrieval. In this case, an error string will be returned. |
57**Example**
58
59```js
60function queryFaultLogCallback(error, value) {
61    if (error) {
62        console.info('error is ' + error);
63    } else {
64        console.info("value length is " + value.length);
65        let len = value.length;
66        for (let i = 0; i < len; i++) {
67            console.info("log: " + i);
68            console.info("Log pid: " + value[i].pid);
69            console.info("Log uid: " + value[i].uid);
70            console.info("Log type: " + value[i].type);
71            console.info("Log timestamp: " + value[i].timestamp);
72            console.info("Log reason: " + value[i].reason);
73            console.info("Log module: " + value[i].module);
74            console.info("Log summary: " + value[i].summary);
75            console.info("Log text: " + value[i].fullLog);
76        }
77    }
78}
79faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH, queryFaultLogCallback);
80```
81
82## faultLogger.querySelfFaultLog
83
84querySelfFaultLog(faultType: FaultType) : Promise&lt;Array&lt;FaultLogInfo&gt;&gt;
85
86Obtains 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.
87
88**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
89
90**Parameters**
91
92| Name| Type| Mandatory| Description|
93| -------- | -------- | -------- | -------- |
94| faultType | [FaultType](#faulttype) | Yes| Fault type.|
95
96**Return value**
97
98| Type| Description|
99| -------- | -------- |
100| 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>The value is the fault information array obtained. If the value is **undefined**, an exception occurs during the information retrieval.|
101
102**Example**
103
104```js
105async function getLog() {
106    let value = await faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH);
107    if (value) {
108        console.info("value length is " + value.length);
109	let len = value.length;
110	for (let i = 0; i < len; i++) {
111	    console.info("log: " + i);
112	    console.info("Log pid: " + value[i].pid);
113	    console.info("Log uid: " + value[i].uid);
114	    console.info("Log type: " + value[i].type);
115	    console.info("Log timestamp: " + value[i].timestamp);
116	    console.info("Log reason: " + value[i].reason);
117	    console.info("Log module: " + value[i].module);
118	    console.info("Log summary: " + value[i].summary);
119	    console.info("Log text: " + value[i].fullLog);
120	}
121    }
122}
123```
124