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