1# @ohos.hichecker (HiChecker)
2
3The 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.
4
5> **NOTE**
6>
7> 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.
8
9## Modules to Import
10
11```ts
12import { hichecker } from '@kit.PerformanceAnalysisKit';
13```
14
15
16## Constants
17
18Provides the constants of all rule types.
19
20**System capability**: SystemCapability.HiviewDFX.HiChecker
21
22| Name                                            | Type     | Value        | Description                                                  |
23| ------------------------------------------------ | -------- | -----------| ------------------------------------------------------ |
24| RULE_CAUTION_PRINT_LOG                           | bigint   | 1ULL << 63 | Alarm rule, which is programmed to print a log when an alarm is generated.                           |
25| RULE_CAUTION_TRIGGER_CRASH                       | bigint   | 1ULL << 62 | Alarm rule, which is programmed to force the application to exit when an alarm is generated.                         |
26| RULE_THREAD_CHECK_SLOW_PROCESS                   | bigint   | 1ULL       | Caution rule, which is programmed to detect whether any time-consuming function is invoked.                     |
27| RULE_CHECK_ABILITY_CONNECTION_LEAK               | bigint   | 1ULL << 33 | Caution rule, which is programmed to detect whether ability leakage has occurred.                     |
28| RULE_CHECK_ARKUI_PERFORMANCE<sup>11+</sup>       | bigint   | 1ULL << 34 | Caution rule, which is programmed to detect the ArkUI performance.                              |
29
30## hichecker.addCheckRule<sup>9+</sup>
31
32addCheckRule(rule: bigint): void
33
34Adds one or more check rules. HiChecker detects unexpected operations or gives feedback based on the added rules. You can use **grep HiChecker** to check for the application running information in the hilog.
35
36**System capability**: SystemCapability.HiviewDFX.HiChecker
37
38**Parameters**
39
40| Name| Type  | Mandatory| Description            |
41| ------ | ------ | ---- | ---------------- |
42| rule   | bigint | Yes  | Rule to be added.|
43
44**Error codes**
45
46| ID| Error Message|
47| ------- | ----------------------------------------------------------------- |
48| 401 | the parameter check failed, only one bigint type parameter is needed  |
49
50**Example**
51
52```ts
53import { BusinessError } from '@kit.BasicServicesKit';
54
55try {
56    // Add a rule.
57    hichecker.addCheckRule(hichecker.RULE_CAUTION_PRINT_LOG);
58    // Add multiple rules.
59    // hichecker.addCheckRule(
60    //     hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
61} catch (err) {
62    console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`);
63}
64```
65
66## hichecker.removeCheckRule<sup>9+</sup>
67
68removeCheckRule(rule: bigint): void
69
70Removes one or more rules. The removed rules will become ineffective.
71
72**System capability**: SystemCapability.HiviewDFX.HiChecker
73
74**Parameters**
75
76| Name| Type  | Mandatory| Description            |
77| ------ | ------ | ---- | ---------------- |
78| rule   | bigint | Yes  | Rule to be removed.|
79
80**Error codes**
81
82| ID| Error Message|
83| ------- | ----------------------------------------------------------------- |
84| 401 | the parameter check failed, only one bigint type parameter is needed  |
85
86**Example**
87
88```ts
89import { BusinessError } from '@kit.BasicServicesKit';
90
91try {
92    // Remove a rule.
93    hichecker.removeCheckRule(hichecker.RULE_CAUTION_PRINT_LOG);
94    // Remove multiple rules.
95    // hichecker.removeCheckRule(
96    //     hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
97} catch (err) {
98    console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`);
99}
100```
101
102## hichecker.containsCheckRule<sup>9+</sup>
103
104containsCheckRule(rule: bigint): boolean
105
106Checks 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.
107
108**System capability**: SystemCapability.HiviewDFX.HiChecker
109
110**Parameters**
111
112| Name| Type  | Mandatory| Description            |
113| ------ | ------ | ---- | ---------------- |
114| rule   | bigint | Yes  | Rule to be checked.|
115
116**Return value**
117
118| Type   | Description                                                      |
119| ------- | ---------------------------------------------------------- |
120| boolean | Returns **true** if the rule exists in the collection of added rules; returns **false** otherwise.|
121
122**Error codes**
123
124| ID| Error Message|
125| ------- | ----------------------------------------------------------------- |
126| 401 | the parameter check failed, only one bigint type parameter is needed  |
127
128**Example**
129
130```ts
131import { BusinessError } from '@kit.BasicServicesKit';
132
133try {
134    // Add a rule.
135    hichecker.addCheckRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);
136
137    // Check whether the added rule exists in the collection of added rules.
138    hichecker.containsCheckRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS); // return true;
139    hichecker.containsCheckRule(hichecker.RULE_CAUTION_PRINT_LOG); // return false;
140} catch (err) {
141    console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`);
142}
143```
144
145## hichecker.addRule<sup>(deprecated)</sup>
146
147addRule(rule: bigint): void
148
149> **NOTE**
150>
151> This API is deprecated since API version 9. You are advised to use [hichecker.addCheckRule](#hicheckeraddcheckrule9).
152
153Adds one or more rules. HiChecker detects unexpected operations or gives feedback based on the added rules.
154
155**System capability**: SystemCapability.HiviewDFX.HiChecker
156
157**Parameters**
158
159| Name| Type  | Mandatory| Description            |
160| ------ | ------ | ---- | ---------------- |
161| rule   | bigint | Yes  | Rule to be added.|
162
163**Example**
164
165```ts
166// Add a rule.
167hichecker.addRule(hichecker.RULE_CAUTION_PRINT_LOG);
168
169// Add multiple rules.
170hichecker.addRule(
171          hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
172```
173
174## hichecker.removeRule<sup>(deprecated)</sup>
175
176removeRule(rule: bigint): void
177
178> **NOTE**
179>
180> This API is deprecated since API version 9. You are advised to use [hichecker.removeCheckRule](#hicheckerremovecheckrule9).
181
182Removes one or more rules. The removed rules will become ineffective.
183
184**System capability**: SystemCapability.HiviewDFX.HiChecker
185
186**Parameters**
187
188| Name| Type  | Mandatory| Description            |
189| ------ | ------ | ---- | ---------------- |
190| rule   | bigint | Yes  | Rule to be removed.|
191
192**Example**
193
194```ts
195// Remove a rule.
196hichecker.removeRule(hichecker.RULE_CAUTION_PRINT_LOG);
197
198// Remove multiple rules.
199hichecker.removeRule(
200          hichecker.RULE_CAUTION_PRINT_LOG | hichecker.RULE_CAUTION_TRIGGER_CRASH);
201```
202
203## hichecker.getRule
204
205getRule(): bigint
206
207Obtains a collection of thread, process, and alarm rules that have been added.
208
209**System capability**: SystemCapability.HiviewDFX.HiChecker
210
211**Return value**
212
213| Type  | Description                  |
214| ------ | ---------------------- |
215| bigint | Collection of added rules.|
216
217**Example**
218
219```ts
220// Add a rule.
221hichecker.addRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);
222
223// Obtain the collection of added rules.
224hichecker.getRule();   // return 1n;
225```
226
227## hichecker.contains<sup>(deprecated)</sup>
228
229contains(rule: bigint): boolean
230
231> **NOTE**
232>
233> This API is deprecated since API version 9. You are advised to use [hichecker.containsCheckRule](#hicheckercontainscheckrule9).
234
235Checks 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.
236
237**System capability**: SystemCapability.HiviewDFX.HiChecker
238
239**Parameters**
240
241| Name| Type  | Mandatory| Description            |
242| ------ | ------ | ---- | ---------------- |
243| rule   | bigint | Yes  | Rule to be checked.|
244
245**Return value**
246
247| Type   | Description                                                      |
248| ------- | ---------------------------------------------------------- |
249| boolean | Returns **true** if the rule exists in the collection of added rules; returns **false** otherwise.|
250
251**Example**
252
253```ts
254// Add a rule.
255hichecker.addRule(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS);
256
257// Check whether the added rule exists in the collection of added rules.
258hichecker.contains(hichecker.RULE_THREAD_CHECK_SLOW_PROCESS); // return true;
259hichecker.contains(hichecker.RULE_CAUTION_PRINT_LOG); // return false;
260```
261