1# Console (控制台) 2<!--Kit: ArkTS--> 3<!--Subsystem: CommonLibrary--> 4<!--Owner: @yao_dashuai--> 5<!--Designer: @yao_dashuai--> 6<!--Tester: @kirl75; @zsw_zhushiwei--> 7<!--Adviser: @ge-yafang--> 8 9本模块提供了一个简单的调试控制台,类似于浏览器提供的JavaScript控制台机制。 10 11> **说明:** 12> 13> 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## console.debug 16 17debug(message: string, ...arguments: any[]): void 18 19以格式化输出方式打印调试信息。 20 21从API version 9开始,该接口支持在ArkTS卡片中使用。 22 23**系统能力:** SystemCapability.ArkUI.ArkUI.Full 24 25**参数:** 26 27| 参数名 | 类型 | 必填 | 说明 | 28| ------- | ------ | ---- | ----------- | 29| message | string | 是 | 表示要打印的文本信息。 | 30| arguments | any[] | 否 | 表示其余要打印的信息或message的替换值。 | 31 32**示例:** 33 34```js 35const number = 5; 36console.debug('count: %d', number); // 格式化输出替换message中的文本。 37// count: 5 38console.debug('count:', number); // 打印message以及其余信息 39// count: 5 40console.debug('count:'); // 仅打印message 41// count: 42``` 43 44## console.log 45 46log(message: string, ...arguments: any[]): void 47 48以格式化输出方式打印日志信息。 49 50从API version 9开始,该接口支持在ArkTS卡片中使用。 51 52**系统能力:** SystemCapability.ArkUI.ArkUI.Full 53 54**参数:** 55 56| 参数名 | 类型 | 必填 | 说明 | 57| ------- | ------ | ---- | ----------- | 58| message | string | 是 | 表示要打印的文本信息。 | 59| arguments | any[] | 否 |表示其余要打印的信息或message的替换值。 | 60 61**示例:** 62 63```js 64const number = 5; 65console.log('count: %d', number); // 格式化输出替换message中的文本。 66// count: 5 67console.log('count:', number); // 打印message以及其余信息 68// count: 5 69console.log('count:'); // 仅打印message 70// count: 71``` 72 73## console.info 74 75info(message: string, ...arguments: any[]): void 76 77以格式化输出方式打印日志信息。(console.log()的别名)。 78 79从API version 9开始,该接口支持在ArkTS卡片中使用。 80 81**系统能力:** SystemCapability.ArkUI.ArkUI.Full 82 83**参数:** 84 85| 参数名 | 类型 | 必填 | 说明 | 86| ------- | ------ | ---- | ----------- | 87| message | string | 是 | 表示要打印的文本信息。 | 88| arguments | any[] | 否 | 表示其余要打印的信息或message的替换值。 | 89 90**示例:** 91 92```js 93const number = 5; 94console.info('count: %d', number); // 格式化输出替换message中的文本。 95// count: 5 96console.info('count:', number); // 打印message以及其余信息 97// count: 5 98console.info('count:'); // 仅打印message 99// count: 100``` 101 102## console.warn 103 104warn(message: string, ...arguments: any[]): void 105 106以格式化输出方式打印警告信息。 107 108从API version 9开始,该接口支持在ArkTS卡片中使用。 109 110**系统能力:** SystemCapability.ArkUI.ArkUI.Full 111 112**参数:** 113 114| 参数名 | 类型 | 必填 | 说明 | 115| ------- | ------ | ---- | ----------- | 116| message | string | 是 | 表示要打印的警告信息。 | 117| arguments | any[] | 否 | 表示其余要打印的信息或message的替换值。 | 118 119**示例:** 120 121```js 122const str = "name should be string"; 123console.warn('warn: %d', str); // 格式化输出替换message中的文本。 124// warn: name should be string 125console.warn('warn:', str); // 打印message以及其余信息 126// warn: name should be string 127console.warn('warn:'); // 仅打印message 128// warn: 129``` 130 131## console.error 132 133error(message: string, ...arguments: any[]): void 134 135以格式化输出方式打印错误信息。 136 137从API version 9开始,该接口支持在ArkTS卡片中使用。 138 139**系统能力:** SystemCapability.ArkUI.ArkUI.Full 140 141**参数:** 142 143| 参数名 | 类型 | 必填 | 说明 | 144| ------- | ------ | ---- | ----------- | 145| message | string | 是 | 表示要打印的错误信息。 | 146| arguments | any[] | 否 | 表示其余要打印的信息或message的替换值。 | 147 148**示例:** 149 150```js 151const str = "value is not defined"; 152console.error('error: %d', str); // 格式化输出替换message中的文本。 153// error: value is not defined 154console.error('error:', str); // 打印message以及其余信息 155// error: value is not defined 156console.error('error:'); // 仅打印message 157// error: 158``` 159 160## console.assert<sup>10+</sup> 161 162assert(value?: Object, ...arguments: Object[]): void 163 164断言打印。 165 166**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 167 168**系统能力:** SystemCapability.Utils.Lang 169 170**参数:** 171 172| 参数名 | 类型 | 必填 | 说明 | 173| ------- | ------ | ---- | ----------- | 174| value | Object | 否 | 语句结果值。若value为假(false)或者省略,则输出以"Assertion failed"开头。如果value为真值(true),则无打印。| 175| arguments | Object | 否 | value为假(false)的后续错误消息打印。省略则不打印。| 176 177**错误码:** 178 179以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 180 181| 错误码ID | 错误信息 | 182| -------- | -------- | 183| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 184 185**示例:** 186 187```js 188console.assert(true, 'does nothing'); // 表达式结果值为true, 无打印。 189console.assert(2 % 1 == 0, 'does nothing'); // 表达式结果值为true, 无打印。 190 191console.assert(false, 'console %s work', 'didn\'t'); 192// Assertion failed: console didn't work 193 194console.assert(); 195// Assertion failed 196``` 197 198## console.count<sup>10+</sup> 199 200count(label?: string): void 201 202维护一个内部计数器,调用时,打印此标签名以及对应的计数次数。 203 204**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 205 206**系统能力:** SystemCapability.Utils.Lang 207 208**参数:** 209 210| 参数名 | 类型 | 必填 | 说明 | 211| ------- | ------ | ---- | ----------- | 212| label | string | 否 | 计数器标签名。默认值为'default'。| 213 214**错误码:** 215 216以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 217 218| 错误码ID | 错误信息 | 219| -------- | -------- | 220| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 221 222**示例:** 223 224```js 225console.count() 226// default: 1 227console.count('default') 228// default: 2 229console.count('abc') 230// abc: 1 231console.count('xyz') 232// xyz: 1 233console.count('abc') 234// abc: 2 235console.count() 236// default: 3 237``` 238 239## console.countReset<sup>10+</sup> 240 241countReset(label?: string): void 242 243清除指定标签名的计数。 244 245**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 246 247**系统能力:** SystemCapability.Utils.Lang 248 249**参数:** 250 251| 参数名 | 类型 | 必填 | 说明 | 252| ------- | ------ | ---- | ----------- | 253| label | string | 否 | 计数器标签名。默认值为'default'。| 254 255**错误码:** 256 257以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 258 259| 错误码ID | 错误信息 | 260| -------- | -------- | 261| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 262 263**示例:** 264 265```js 266console.count('abc'); 267// abc: 1 268console.countReset('abc'); 269console.count('abc'); 270// abc: 1 271``` 272 273## console.dir<sup>10+</sup> 274 275dir(dir?: Object): void 276 277打印对象内容。 278 279**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 280 281**系统能力:** SystemCapability.Utils.Lang 282 283**参数:** 284 285| 参数名 | 类型 | 必填 | 说明 | 286| ------- | ------ | ---- | ----------- | 287| dir | Object | 否 | 需要打印内容的对象。省略则无任何打印。| 288 289 290**示例:** 291 292```js 293class bar { 294 baz: boolean = true; 295} 296let b: bar = {baz: true} 297class foo{ 298 bar: bar = b; 299} 300let c: foo = {bar: b} 301class c1{ 302 foo: foo = c; 303} 304let a: c1 = {foo: c} 305console.dir(a); 306// Object: {"foo":{"bar":{"baz":true}}} 307 308console.dir(); // 无打印 309``` 310 311 312## console.dirxml<sup>10+</sup> 313 314dirxml(...arguments: Object[]): void 315 316此方法通过内部调用console.log()实现。此方法不会产生任何 XML 格式。使用方法与console.log()一致。 317 318**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 319 320**系统能力:** SystemCapability.Utils.Lang 321 322**参数:** 323 324| 参数名 | 类型 | 必填 | 说明 | 325| ------- | ------ | ---- | ----------- | 326| arguments | Object | 是 | 要打印的信息。 | 327 328**示例:** 329 330```js 331const number = 5; 332console.dirxml('count: %d', number); 333// count: 5 334console.dirxml('count:', number); 335// count: 5 336console.dirxml('count:'); 337// count: 338``` 339 340## console.group<sup>10+</sup> 341 342group(...arguments: Object[]): void 343 344默认将后续行的缩进增加两个空格。 345如果提供需要打印的信息,则首先打印信息,没有额外的缩进。 346 347**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 348 349**系统能力:** SystemCapability.Utils.Lang 350 351**参数:** 352 353| 参数名 | 类型 | 必填 | 说明 | 354| ------- | ------ | ---- | ----------- | 355| arguments | Object | 否 | 要打印的信息。 | 356 357**示例:** 358 359```js 360console.log("outter"); 361// outter 362console.group(); 363console.log("level 1"); 364// level 1 365console.group("in level1"); 366// in level1 367console.log("level 2"); 368// level 2 369``` 370 371 372## console.groupCollapsed<sup>10+</sup> 373 374groupCollapsed(...arguments: Object[]): void 375 376使用与功能同console.group()一致。 377 378**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 379 380**系统能力:** SystemCapability.Utils.Lang 381 382**参数:** 383 384| 参数名 | 类型 | 必填 | 说明 | 385| ------- | ------ | ---- | ----------- | 386| arguments | Object | 否 | 要打印的信息。| 387 388 389**示例:** 390 391```js 392console.groupCollapsed("outter"); 393// outter 394console.groupCollapsed(); 395console.log("level 1"); 396// level 1 397console.groupCollapsed("in level1"); 398// in level1 399console.log("level 2"); 400// level 2 401``` 402 403## console.groupEnd<sup>10+</sup> 404 405groupEnd(): void 406 407将后续行的缩进减少两个空格。 408 409**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 410 411**系统能力:** SystemCapability.Utils.Lang 412 413 414**示例:** 415 416```js 417console.log("outter"); 418// outter 419console.group(); 420console.log("level 1"); 421// level 1 422console.groupEnd(); 423console.log("outter"); 424// outter 425``` 426 427 428## console.table<sup>10+</sup> 429 430table(tableData?: Object): void 431 432以表格形式打印数据。 433 434**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 435 436**系统能力:** SystemCapability.Utils.Lang 437 438**参数:** 439 440| 参数名 | 类型 | 必填 | 说明 | 441| ------- | ------ | ---- | ----------- | 442| tableData | Object | 否 | 要打印为表格形式的对象。省略则无任何打印。 | 443 444**示例:** 445 446```js 447console.table([1, 2, 3]); 448// ┌─────────┬────────┐ 449// │ (index) │ Values │ 450// ├─────────┼────────┤ 451// │ 0 │ 1 │ 452// │ 1 │ 2 │ 453// │ 2 │ 3 │ 454// └─────────┴────────┘ 455 456console.table({ a: [1, 2, 3, 4, 5], b: 5, c: { e: 5 } }); 457 458// ┌─────────┬───┬───┬───┬───┬───┬───┬────────┐ 459// │ (index) │ 0 │ 1 │ 2 │ 3 │ 4 │ e │ Values │ 460// ├─────────┼───┼───┼───┼───┼───┼───┼────────┤ 461// │ a │ 1 │ 2 │ 3 │ 4 │ 5 │ │ │ 462// │ b │ │ │ │ │ │ │ 5 │ 463// │ c │ │ │ │ │ │ 5 │ │ 464// └─────────┴───┴───┴───┴───┴───┴───┴────────┘ 465``` 466 467## console.time<sup>10+</sup> 468 469time(label?: string): void 470 471启动可用于计算操作持续时间的计时器。可使用console.timeEnd()关闭计时器并打印经过的时间(单位:ms)。 472 473**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 474 475**系统能力:** SystemCapability.Utils.Lang 476 477**参数:** 478 479| 参数名 | 类型 | 必填 | 说明 | 480| ------- | ------ | ---- | ----------- | 481| label | string | 否 | 计时器标识。默认值为'default'。 | 482 483**错误码:** 484 485以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 486 487| 错误码ID | 错误信息 | 488| -------- | -------- | 489| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 490 491**示例:** 492 493```js 494console.time('abc'); 495``` 496 497## console.timeEnd<sup>10+</sup> 498 499timeEnd(label?: string): void 500 501停止之前通过调用 console.time() 启动的计时器并将打印经过的时间(单位:ms)。 502 503**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 504 505**系统能力:** SystemCapability.Utils.Lang 506 507**参数:** 508 509| 参数名 | 类型 | 必填 | 说明 | 510| ------- | ------ | ---- | ----------- | 511| label | string | 否 | 计时器标识。默认值为'default'。 | 512 513**错误码:** 514 515以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 516 517| 错误码ID | 错误信息 | 518| -------- | -------- | 519| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 520 521**示例:** 522 523```js 524console.time('abc'); 525console.timeEnd('abc'); 526// abc: 225.438ms 527``` 528 529## console.timeLog<sup>10+</sup> 530 531timeLog(label?: string, ...arguments: Object[]): void 532 533对于先前通过调用 console.time() 启动的计时器,打印经过时间和其他data参数。 534 535**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 536 537**系统能力:** SystemCapability.Utils.Lang 538 539**参数:** 540 541| 参数名 | 类型 | 必填 | 说明 | 542| ------- | ------ | ---- | ----------- | 543| label | string | 否 | 计时器标识。默认值为'default'。 | 544| arguments | Object | 否 | 需要打印的其他日志。 | 545 546**错误码:** 547 548以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 549 550| 错误码ID | 错误信息 | 551| -------- | -------- | 552| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.| 553 554**示例:** 555 556```js 557console.time('timer1'); 558console.timeLog('timer1', 17); 559// timer1: 365.227ms 17 560console.timeEnd('timer1'); 561// timer1: 513.22ms 562``` 563 564## console.trace<sup>10+</sup> 565 566static trace(...arguments: Object[]): void 567 568打印当前堆栈。 569 570**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 571 572**系统能力:** SystemCapability.Utils.Lang 573 574**参数:** 575 576| 参数名 | 类型 | 必填 | 说明 | 577| ------- | ------ | ---- | ----------- | 578| arguments | Object | 否 | 需要打印的其他日志。省略则仅打印堆栈信息。| 579 580**示例:** 581 582```js 583console.trace(); 584// Trace: 585// xxxxxxxxxx(当前堆栈信息) 586console.trace("Show the trace"); 587// Trace: Show the trace 588// xxxxxxxxxx(当前堆栈信息) 589``` 590 591## console.traceHybridStack<sup>12+</sup> 592 593static traceHybridStack(): void 594 595在主线程/worker线程中可打印当前线程混合堆栈信息。 596 597**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 598 599**系统能力:** SystemCapability.Utils.Lang 600 601**示例:** 602 603```ts 604console.traceHybridStack(); 605// TraceHybridStack: 606// xxxxxxxxxx(当前线程混合堆栈信息) 607```