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

S
shawn_he 已提交
3
The **hichecker** module is provided for you to check issues that may be easily ignored during development of OpenHarmony 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 5

> **NOTE**<br>
S
shawn_he 已提交
6 7 8 9 10
> 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 已提交
11
```js
S
shawn_he 已提交
12 13 14 15
import hichecker from '@ohos.hichecker';
```


S
shawn_he 已提交
16
## Constant
S
shawn_he 已提交
17 18 19

Provides the constants of all rule types.

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

| Name                              | Type| Description                                                  |
S
shawn_he 已提交
23
| ---------------------------------- | -------- | ------------------------------------------------------ |
S
shawn_he 已提交
24 25 26 27
| 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 已提交
28 29


S
shawn_he 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 79 80 81 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 116 117 118 119 120 121 122 123
## hichecker.addCheckRule<sup>9+</sup>

## hichecker.addRule
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.
    hichecker.addCheckRule(hichecker.RULE_CAUTION_PRINT_LOG);}
    // Add multiple rules.
    hichecker.addCheckRule(
        hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
catch (err) {
    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.
    hichecker.removeCheckRule(
        hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
catch (err) {
    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;
catch (err) {
    console.error(`code: ${err.code}, message: ${err.message}`);
}
```

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

S
shawn_he 已提交
124 125
## hichecker.addRule

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

S
shawn_he 已提交
128
addRule(rule: bigint): void
S
shawn_he 已提交
129 130 131

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

S
shawn_he 已提交
132 133
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
134 135
**Parameters**

S
shawn_he 已提交
136
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
137
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
138
| rule   | bigint | Yes  | Rule to be added.|
S
shawn_he 已提交
139 140 141

**Example**

S
shawn_he 已提交
142
```js
S
shawn_he 已提交
143 144 145 146 147 148 149 150
// 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 已提交
151
## hichecker.removeRule<sup>(deprecated)</sup>
S
shawn_he 已提交
152

S
shawn_he 已提交
153
removeRule(rule: bigint): void
S
shawn_he 已提交
154

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

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

S
shawn_he 已提交
159 160
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
161 162
**Parameters**

S
shawn_he 已提交
163
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
164
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
165
| rule   | bigint | Yes  | Rule to be removed.|
S
shawn_he 已提交
166 167 168

**Example**

S
shawn_he 已提交
169
```js
S
shawn_he 已提交
170 171 172 173 174 175 176 177 178 179
// 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 已提交
180
getRule(): bigint 
S
shawn_he 已提交
181 182 183

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

S
shawn_he 已提交
184 185
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
186 187
**Return value**

S
shawn_he 已提交
188
| Type  | Description                  |
S
shawn_he 已提交
189
| ------ | ---------------------- |
S
shawn_he 已提交
190
| bigint | Collection of added rules.|
S
shawn_he 已提交
191 192 193

**Example**

S
shawn_he 已提交
194
```js
S
shawn_he 已提交
195 196 197 198
// Add a rule.
hichecker.addRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);

// Obtain the collection of added rules.
S
shawn_he 已提交
199
hichecker.getRule();   // return 1n;
S
shawn_he 已提交
200 201
```

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

S
shawn_he 已提交
204
contains(rule: bigint): boolean
S
shawn_he 已提交
205

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

S
shawn_he 已提交
208 209
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 已提交
210 211
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
212 213
**Parameters**

S
shawn_he 已提交
214
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
215
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
216
| rule   | bigint | Yes  | Rule to be checked.|
S
shawn_he 已提交
217 218 219

**Return value**

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

**Example**

S
shawn_he 已提交
226
```js
S
shawn_he 已提交
227 228 229 230
// 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 已提交
231 232
hichecker.contains(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS); // return true;
hichecker.contains(hichecker.RULE_CAUTION_PRINT_LOG); // return false;
S
shawn_he 已提交
233
```