js-apis-faultLogger.md 4.6 KB
Newer Older
W
wusongqing 已提交
1
# Fault Logger
W
wusongqing 已提交
2
> **NOTE**<br/>
W
wusongqing 已提交
3 4 5 6
> 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.

## Modules to Import

W
wusongqing 已提交
7
```js
W
wusongqing 已提交
8 9 10 11
import faultLogger from '@ohos.faultLogger'
```


W
wusongqing 已提交
12
## FaultType
W
wusongqing 已提交
13 14 15

Enumerates the fault types.

W
wusongqing 已提交
16 17
**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger

W
wusongqing 已提交
18
| Name| Default Value| Description|
W
wusongqing 已提交
19
| -------- | -------- | -------- |
W
wusongqing 已提交
20 21 22 23
| NO_SPECIFIC | 0 | No specific fault type.|
| CPP_CRASH | 2 | C++ program crash.|
| JS_CRASH | 3 | JS program crash.|
| APP_FREEZE | 4 | Application freezing.|
W
wusongqing 已提交
24

W
wusongqing 已提交
25
## FaultLogInfo
W
wusongqing 已提交
26 27 28

Defines the data structure of the fault log information.

W
wusongqing 已提交
29 30
**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger

W
wusongqing 已提交
31 32
| Name| Type| Description|
| -------- | -------- | -------- |
W
wusongqing 已提交
33 34 35 36 37 38 39 40
| pid | number | Process ID of the faulty process.|
| uid | number | User ID of the faulty process.|
| type | [FaultType](#faulttype) | Fault type.|
| timestamp | number | Second-level timestamp when the log was generated.|
| reason | string | Reason for the fault.|
| module | string | Module on which the fault occurred.|
| summary | string | Summary of the fault.|
| fullLog | string | Full log text.|
W
wusongqing 已提交
41 42 43 44 45

## faultLogger.querySelfFaultLog

querySelfFaultLog(faultType: FaultType, callback: AsyncCallback&lt;Array&lt;FaultLogInfo&gt;&gt;) : void

46
Obtains 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.
W
wusongqing 已提交
47 48

**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
W
wusongqing 已提交
49

W
wusongqing 已提交
50 51 52 53
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
54
| faultType | [FaultType](#faulttype) | Yes| Fault type.|
W
wusongqing 已提交
55
| 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. |
W
wusongqing 已提交
56
**Example**
W
wusongqing 已提交
57

W
wusongqing 已提交
58
```js
W
wusongqing 已提交
59 60 61 62 63 64 65 66 67 68 69
function queryFaultLogCallback(error, value) {
    if (error) {
        console.info('error is ' + error);
    } else {
        console.info("value length is " + value.length);
        let len = value.length;
        for (let i = 0; i < len; i++) {
            console.info("log: " + i);
            console.info("Log pid: " + value[i].pid);
            console.info("Log uid: " + value[i].uid);
            console.info("Log type: " + value[i].type);
W
wusongqing 已提交
70
            console.info("Log timestamp: " + value[i].timestamp);
W
wusongqing 已提交
71 72 73 74 75 76 77
            console.info("Log reason: " + value[i].reason);
            console.info("Log module: " + value[i].module);
            console.info("Log summary: " + value[i].summary);
            console.info("Log text: " + value[i].fullLog);
        }
    }
}
W
wusongqing 已提交
78
faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH, queryFaultLogCallback);
W
wusongqing 已提交
79 80 81 82
```

## faultLogger.querySelfFaultLog

W
wusongqing 已提交
83 84 85
querySelfFaultLog(faultType: FaultType) : Promise&lt;Array&lt;FaultLogInfo&gt;&gt;

Obtains 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.
W
wusongqing 已提交
86

W
wusongqing 已提交
87
**System capability**: SystemCapability.HiviewDFX.Hiview.FaultLogger
W
wusongqing 已提交
88

W
wusongqing 已提交
89 90 91 92
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
93
| faultType | [FaultType](#faulttype) | Yes| Fault type.|
W
wusongqing 已提交
94 95 96 97 98

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
99
| 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.|
W
wusongqing 已提交
100

W
wusongqing 已提交
101
**Example**
W
wusongqing 已提交
102

W
wusongqing 已提交
103
```js
W
wusongqing 已提交
104 105 106 107 108 109 110 111 112 113
async function getLog() {
    let value = await faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH);
    if (value) {
        console.info("value length is " + value.length);
	let len = value.length;
	for (let i = 0; i < len; i++) {
	    console.info("log: " + i);
	    console.info("Log pid: " + value[i].pid);
	    console.info("Log uid: " + value[i].uid);
	    console.info("Log type: " + value[i].type);
W
wusongqing 已提交
114
	    console.info("Log timestamp: " + value[i].timestamp);
W
wusongqing 已提交
115 116 117 118 119
	    console.info("Log reason: " + value[i].reason);
	    console.info("Log module: " + value[i].module);
	    console.info("Log summary: " + value[i].summary);
	    console.info("Log text: " + value[i].fullLog);
	}
W
wusongqing 已提交
120 121
    }
}
W
wusongqing 已提交
122
```