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