js-apis-hichecker.md 7.5 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.hichecker (HiChecker)
S
shawn_he 已提交
2

S
shawn_he 已提交
3
The HiChecker module allows you to check issues that may be easily ignored during development of applications (including system-built and third-party applications). Such issues include calling of time-consuming functions by key application threads, event distribution and execution timeout in application processes, and ability resource leakage in application processes. The issues are recorded in logs or lead to process crashes explicitly so that you can find and rectify them.
S
shawn_he 已提交
4

S
shawn_he 已提交
5 6
> **NOTE**
>
S
shawn_he 已提交
7 8 9 10 11
> 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

S
shawn_he 已提交
12
```js
S
shawn_he 已提交
13 14 15 16
import hichecker from '@ohos.hichecker';
```


S
shawn_he 已提交
17
## Constants
S
shawn_he 已提交
18 19 20

Provides the constants of all rule types.

S
shawn_he 已提交
21 22 23
**System capability**: SystemCapability.HiviewDFX.HiChecker

| Name                              | Type| Description                                                  |
S
shawn_he 已提交
24
| ---------------------------------- | -------- | ------------------------------------------------------ |
S
shawn_he 已提交
25 26 27 28
| RULE_CAUTION_PRINT_LOG             | bigint   | Alarm rule, which is programmed to print a log when an alarm is generated.                        |
| RULE_CAUTION_TRIGGER_CRASH         | bigint   | Alarm rule, which is programmed to force the application to exit when an alarm is generated.                      |
| RULE_THREAD_CHECK_SLOW_PROCESS     | bigint   | Caution rule, which is programmed to detect whether any time-consuming function is invoked.                  |
| RULE_CHECK_ABILITY_CONNECTION_LEAK | bigint   | Caution rule, which is programmed to detect whether ability leakage has occurred.                   |
S
shawn_he 已提交
29

S
shawn_he 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
## hichecker.addCheckRule<sup>9+</sup>

addCheckRule(rule: bigint): void

Adds one or more rules. HiChecker detects unexpected operations or gives feedback based on the added rules.

**System capability**: SystemCapability.HiviewDFX.HiChecker

**Parameters**

| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| rule   | bigint | Yes  | Rule to be added.|

**Example**

```js
try {
    // Add a rule.
S
shawn_he 已提交
49
    hichecker.addCheckRule(hichecker.RULE_CAUTION_PRINT_LOG);
S
shawn_he 已提交
50
    // Add multiple rules.
S
shawn_he 已提交
51 52 53
    // hichecker.addCheckRule(
    //     hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
} catch (err) {
S
shawn_he 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    console.error(`code: ${err.code}, message: ${err.message}`);
}
```

## hichecker.removeCheckRule<sup>9+</sup>

removeCheckRule(rule: bigint): void

Removes one or more rules. The removed rules will become ineffective.

**System capability**: SystemCapability.HiviewDFX.HiChecker

**Parameters**

| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| rule   | bigint | Yes  | Rule to be removed.|

**Example**

```js
try {
    // Remove a rule.
    hichecker.removeCheckRule(hichecker.RULE_CAUTION_PRINT_LOG);
    // Remove multiple rules.
S
shawn_he 已提交
79 80 81
    // hichecker.removeCheckRule(
    //     hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
} catch (err) {
S
shawn_he 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    console.error(`code: ${err.code}, message: ${err.message}`);
}
```

## hichecker.containsCheckRule<sup>9+</sup>

containsCheckRule(rule: bigint): boolean

Checks whether the specified rule exists in the collection of added rules. If the rule is of the thread level, this operation is performed only on the current thread.

**System capability**: SystemCapability.HiviewDFX.HiChecker

**Parameters**

| Name| Type  | Mandatory| Description            |
| ------ | ------ | ---- | ---------------- |
| rule   | bigint | Yes  | Rule to be checked.|

**Return value**

| Type   | Description                                                      |
| ------- | ---------------------------------------------------------- |
| boolean | Returns **true** if the rule exists in the collection of added rules; returns **false** otherwise.|

**Example**

```js
try {
    // Add a rule.
    hichecker.addCheckRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);

    // Check whether the added rule exists in the collection of added rules.
    hichecker.containsCheckRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS); // return true;
    hichecker.containsCheckRule(hichecker.RULE_CAUTION_PRINT_LOG); // return false;
S
shawn_he 已提交
116
} catch (err) {
S
shawn_he 已提交
117 118 119 120 121 122
    console.error(`code: ${err.code}, message: ${err.message}`);
}
```

## hichecker.addRule<sup>(deprecated)</sup>

S
shawn_he 已提交
123
addRule(rule: bigint): void
S
shawn_he 已提交
124

S
shawn_he 已提交
125 126
> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hichecker.addCheckRule](#hicheckeraddcheckrule9) instead.

S
shawn_he 已提交
127 128
Adds one or more rules. HiChecker detects unexpected operations or gives feedback based on the added rules.

S
shawn_he 已提交
129 130
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
131 132
**Parameters**

S
shawn_he 已提交
133
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
134
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
135
| rule   | bigint | Yes  | Rule to be added.|
S
shawn_he 已提交
136 137 138

**Example**

S
shawn_he 已提交
139
```js
S
shawn_he 已提交
140 141 142 143 144 145 146 147
// Add a rule.
hichecker.addRule(hichecker.RULE_CAUTION_PRINT_LOG);

// Add multiple rules.
hichecker.addRule(
          hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
```

S
shawn_he 已提交
148
## hichecker.removeRule<sup>(deprecated)</sup>
S
shawn_he 已提交
149

S
shawn_he 已提交
150
removeRule(rule: bigint): void
S
shawn_he 已提交
151

S
shawn_he 已提交
152 153
> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hichecker.removeCheckRule](#hicheckerremovecheckrule9) instead.

S
shawn_he 已提交
154 155
Removes one or more rules. The removed rules will become ineffective.

S
shawn_he 已提交
156 157
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
158 159
**Parameters**

S
shawn_he 已提交
160
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
161
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
162
| rule   | bigint | Yes  | Rule to be removed.|
S
shawn_he 已提交
163 164 165

**Example**

S
shawn_he 已提交
166
```js
S
shawn_he 已提交
167 168 169 170 171 172 173 174 175 176
// Remove a rule.
hichecker.removeRule(hichecker.RULE_CAUTION_PRINT_LOG);

// Remove multiple rules.
hichecker.removeRule(
          hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
```

## hichecker.getRule

S
shawn_he 已提交
177
getRule(): bigint 
S
shawn_he 已提交
178 179 180

Obtains a collection of thread, process, and alarm rules that have been added.

S
shawn_he 已提交
181 182
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
183 184
**Return value**

S
shawn_he 已提交
185
| Type  | Description                  |
S
shawn_he 已提交
186
| ------ | ---------------------- |
S
shawn_he 已提交
187
| bigint | Collection of added rules.|
S
shawn_he 已提交
188 189 190

**Example**

S
shawn_he 已提交
191
```js
S
shawn_he 已提交
192 193 194 195
// Add a rule.
hichecker.addRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);

// Obtain the collection of added rules.
S
shawn_he 已提交
196
hichecker.getRule();   // return 1n;
S
shawn_he 已提交
197 198
```

S
shawn_he 已提交
199
## hichecker.contains<sup>(deprecated)</sup>
S
shawn_he 已提交
200

S
shawn_he 已提交
201
contains(rule: bigint): boolean
S
shawn_he 已提交
202

S
shawn_he 已提交
203 204
> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hichecker.containsCheckRule](#hicheckercontainscheckrule9) instead.

S
shawn_he 已提交
205 206
Checks whether the specified rule exists in the collection of added rules. If the rule is of the thread level, this operation is performed only on the current thread.

S
shawn_he 已提交
207 208
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
209 210
**Parameters**

S
shawn_he 已提交
211
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
212
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
213
| rule   | bigint | Yes  | Rule to be checked.|
S
shawn_he 已提交
214 215 216

**Return value**

S
shawn_he 已提交
217
| Type   | Description                                                      |
S
shawn_he 已提交
218
| ------- | ---------------------------------------------------------- |
S
shawn_he 已提交
219
| boolean | Returns **true** if the rule exists in the collection of added rules; returns **false** otherwise.|
S
shawn_he 已提交
220 221 222

**Example**

S
shawn_he 已提交
223
```js
S
shawn_he 已提交
224 225 226 227
// Add a rule.
hichecker.addRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);

// Check whether the added rule exists in the collection of added rules.
S
shawn_he 已提交
228 229
hichecker.contains(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS); // return true;
hichecker.contains(hichecker.RULE_CAUTION_PRINT_LOG); // return false;
S
shawn_he 已提交
230
```