console

October 22, 2022

console

Stability: 2 - Stable

The Console module provides a console for debugging similar to the one used in web browsers. It is used to output some debugging information, intermediate results, etc. Some of the functions in the console module can also be used directly as global functions, such as log, print, etc.

console.show()

Show the console. This will display a hover window of the console (requires hover permission).

console.hide()

Hide the console hover window.

console.clear()

Clears the console.

console.log([data][, . .args])

  • data {any}
  • ... .args {any}

Print to the console with line breaks. Multiple arguments can be passed in, the first as the main message and the others as substitution values similar to those in printf(3)open in new window (the arguments are passed to util.format()).

const count = 5;
console.log('count: %d', count);
// print: count: 5 to stdout
console.log('count:', count);
// print: count: 5 to stdout

See util.format() for details.

This function can also be used as a global function.

console.verbose([data][, . .args])

  • data {any}
  • ... .args {any}

Similar to console.log, but the output is shown in gray font. The output has a lower priority than log and is used to output information of an observational nature.

console.info([data][, . .args])

  • data {any}
  • ... .args {any}

Similar to console.log, but the output is shown in green font. The output has higher priority than log, and is used to output important information.

console.warn([data][, . . args])

  • data {any}
  • ... .args {any}

Similar to console.log, but the output is shown in blue font. The output has higher priority than info, and is used to output warning messages.

console.error([data][, . . args])

  • data {any}
  • ... .args {any}

Similar to console.log, but the output is shown in red font. Output priority is higher than warn, and is used to output error messages.

console.assert(value, message)

  • value {any} The boolean value to be asserted.
  • message {string} The message to be output if value is false

Assert. If value is false, output the error message and stop the script.

var a = 1 + 1;
console.assert(a == 2, "Error in addition");

console.time([label])

[new in v4.1.0]

  • label {String} Timer label, can be omitted

Starts a timer to calculate the duration of an operation. The timer is identified by a unique label. When calling console.timeEnd(), the same label can be used to stop the timer and output the duration in milliseconds to the console. Repeatedly starting a timer with the same label will overwrite the previous timer that started the same label.

console.timeEnd(label)

[new in v4.1.0]

  • label {String} timer label

Stop the timer started before by calling console.time() and print the result to the console. The timer is deleted after calling console.timeEnd(). If the timer specified by the tag does not exist then NaNms will be printed.

console.time('summation');
var sum = 0;
for(let i = 0; i < 100000; i++){
    sum += i;
}
console.timeEnd('sum');
// print sum: xxx ms

console.trace([data][, . . args])

[new in v4.1.0]

  • data {any}
  • ... .args {any}

Similar to console.log, it also prints out information about the call stack where this function was called (i.e. the currently running file, number of lines, etc.).

console.trace('Show me');
// Print: (the stack trace will change depending on the location of the called trace)
// Show me
// at <test>:7

console.input(data[, . .args])

  • data {any}
  • ... .args {any}

Outputs the same message as console.log, and displays the input box on the console waiting for input. Pressing the console's confirm button will return the input string after calculating it with eval.

Some models may have a bug that the console does not show the input box.

For example.

var n = console.input("Please enter a number:"); 
// After entering 123.
toast(n + 1);
//display 124

console.rawInput(data[, . .args])

  • data {any}
  • ... .args {any}

Outputs the same message as console.log and displays the input box on the console waiting for input. Pressing the console's confirm button will return the input string directly.

Some models may have a bug that the console does not show the input box.

For example.

var n = console.rawInput("Please enter a number:"); 
// After entering 123.
toast(n + 1);
//display 1231

console.setSize(w, h)

  • w {number} width
  • ``h` {number} height

Set the size of the console in pixels.

console.show();
// Set the console size to a quarter of the screen
console.setSize(device.width / 2, device.height / 2);

console.setPosition(x, y)

  • x {number} horizontal coordinate
  • y {number} vertical coordinate

Sets the position of the console in pixels.

console.show();
console.setPosition(100, 100);

console.setGlobalLogConfig(config)

[new in v4.1.0]

  • config {Object} Log configuration, optional items are.
    • file {string} The path to the log file to which the logs will be written.
    • maxFileSize {number} the maximum file size in bytes, default is 512 * 1024 (512KB)
    • rootLevel {string} The log level to write to, default is "ALL" (all logs), can be "OFF", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", etc.
    • maxBackupSize {number} The maximum number of log backup files, default is 5
    • filePattern {string} The format of log writing, see PatternLayoutopen in new window

Set the path and configuration for log saving. For example, save the logs to "/sdcard/1.txt":

console.setGlobalLogConfig({
    "file": "/sdcard/1.txt"
});

Note that this function affects the logging of all scripts.

  • text {string} | {Object} The message to print to the console

Equivalent to log(text).

Last update:
Contributors: Bruce