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