js-apis-hichecker.md 4.3 KB
Newer Older
S
shawn_he 已提交
1 2
# HiChecker

S
shawn_he 已提交
3 4 5
HiChecker 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.

> **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 30 31


## hichecker.addRule

S
shawn_he 已提交
32
addRule(rule: bigint): void
S
shawn_he 已提交
33 34 35

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

S
shawn_he 已提交
36 37
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
38 39
**Parameters**

S
shawn_he 已提交
40
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
41
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
42
| rule   | bigint | Yes  | Rule to be added.|
S
shawn_he 已提交
43 44 45

**Example**

S
shawn_he 已提交
46
```js
S
shawn_he 已提交
47 48 49 50 51 52 53 54 55 56
// 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);
```

## hichecker.removeRule

S
shawn_he 已提交
57
removeRule(rule: bigint): void
S
shawn_he 已提交
58 59 60

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

S
shawn_he 已提交
61 62
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
63 64
**Parameters**

S
shawn_he 已提交
65
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
66
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
67
| rule   | bigint | Yes  | Rule to be removed.|
S
shawn_he 已提交
68 69 70

**Example**

S
shawn_he 已提交
71
```js
S
shawn_he 已提交
72 73 74 75 76 77 78 79 80 81
// 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 已提交
82
getRule(): bigint 
S
shawn_he 已提交
83 84 85

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

S
shawn_he 已提交
86 87
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
88 89
**Return value**

S
shawn_he 已提交
90
| Type  | Description                  |
S
shawn_he 已提交
91
| ------ | ---------------------- |
S
shawn_he 已提交
92
| bigint | Collection of added rules.|
S
shawn_he 已提交
93 94 95

**Example**

S
shawn_he 已提交
96
```js
S
shawn_he 已提交
97 98 99 100
// Add a rule.
hichecker.addRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);

// Obtain the collection of added rules.
S
shawn_he 已提交
101
hichecker.getRule();   // return 1n;
S
shawn_he 已提交
102 103 104 105
```

## hichecker.contains

S
shawn_he 已提交
106
contains(rule: bigint): boolean
S
shawn_he 已提交
107 108 109

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 已提交
110 111
**System capability**: SystemCapability.HiviewDFX.HiChecker

S
shawn_he 已提交
112 113
**Parameters**

S
shawn_he 已提交
114
| Name| Type  | Mandatory| Description            |
S
shawn_he 已提交
115
| ------ | ------ | ---- | ---------------- |
S
shawn_he 已提交
116
| rule   | bigint | Yes  | Rule to be checked.|
S
shawn_he 已提交
117 118 119

**Return value**

S
shawn_he 已提交
120
| Type   | Description                                                      |
S
shawn_he 已提交
121
| ------- | ---------------------------------------------------------- |
S
shawn_he 已提交
122
| boolean | Returns **true** if the rule exists in the collection of added rules; returns **false** otherwise.|
S
shawn_he 已提交
123 124 125

**Example**

S
shawn_he 已提交
126
```js
S
shawn_he 已提交
127 128 129 130
// 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 已提交
131 132
hichecker.contains(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS); // return true;
hichecker.contains(hichecker.RULE_CAUTION_PRINT_LOG); // return false;
S
shawn_he 已提交
133
```