Console : Standard output

更新时间:
2024-03-13
下载文档

Console : Standard output

console is a global object preset by JSRE. It uses the current process standard output print information and is mainly used to output various debugging information.

Example

console.log('Hello JavaScript!');

Support

The following shows console module APIs available for each permissions.

 User ModePrivilege Mode
console.depth
console.tagEnable
console.inspectEnable
console.timestampEnable
console.log
console.info
console.warn
console.error
console.tag
console.toString
console.clear
console.assert
console.trace
console.backtrace
console.drain
console.count
console.countReset
console.group
console.groupEnd
console.groupCollapsed
console.time
console.timeEnd
console.timeLog
console.tagFilterAdd
console.tagFilterDelete

Console Object

console.depth

  • {Integer}

Each function of console output supports recursion, and the recursion depth can be set through console.depth. The default output recursion depth is 3.

Example

console.depth = 3;

console.tagEnable

  • {Boolean}

Setting this property to true will print console.tag() information. The default is false.

console.inspectEnable

  • {Boolean}

Console output object, whether to print the attributes of the object recursively. Default: false.

Example

var obj = { a: 1, b: 2 };
console.log(obj); // will print obj.toString()

console.inspectEnable = true;
console.log(obj); // will print {a:1, b:2}

console.timestampEnable

  • {Boolean}

Setting this property to true will print timestamp before print information. The default is true.

This feature is available on EdgerOS 2.0.9 and above.

console.log(...args)

  • ...args {Any} Any number of any type of parameters that need to print.

This function can print any type of parameter information.

Example

console.log('Hello JSRE!');

var a = 10;
var b = 'string';
var c = { item1: 10, item2: 20 };

console.log('Hello JSRE!', a, b, c);

console.info(...args)

  • ...args {Any} Any number of any type of parameters that need to print.

This function can print any type of parameter information with 'Info:' prefix.

console.warn(...args)

  • ...args {Any} Any number of any type of parameters that need to print.

This function can print any type of parameter information with 'Warning:' prefix.

console.error(args)

  • ...args {Any} Any number of any type of parameters that need to print.

This function can print any type of parameter information with 'Error:' prefix.

console.tag(module, ...args)

  • module {Object} Must be current module object.
  • ...args {Any} Any number of any type of parameters that need to print.

This function is used for debugging output in the module. The current task can enable the console.tag() debug output with the following code:

console.tagEnable = true;

Example

console.tag(module, 'This is a module debug output.');

console.toString(...args)

  • ...args {Any} Any number of any type of parameters that need to print.

This function can make any type of parameter information to a string.

Example

console.log('Hello JSRE!');

var a = 10;
var b = 'string';
var c = { item1: 10, item2: 20 };

var str = console.toString('Hello JSRE!', a, b, c);

console.clear()

If the output is a standard tty terminal, this function will clear the current screen display.

console.assert(cond, message)

  • cond {Boolean} Assertion condition.
  • message {String} When the condition is not met, the error that needs to print and the exception information thrown.

This method determines whether the cond condition is true. If it is true, continue execute. If cond is false, this method prints an error message and throws an exception.

Example

var a = 1;

// true
console.assert(a === 1, 'Assert Error!');

// false
console.assert(a !== 1, 'Assert Error!');

console.trace()

Print the current call stack.

Example

function aaa() {
  console.trace();
}

function bbb() {
  aaa();
}

bbb();

console.backtrace()

  • Returns: {Array} Backtrace array.

Get the current call stack.

Example

var array = console.backtrace();
for (var line of array) {
  console.log(line);
}

console.drain()

If the console output is a standard terminal device or file, this function waits for the internal sending buffer to be sent completely.

console.count([label])

  • label {String} The display label for the counter. default: 'default'.

Maintains an internal counter specific to label and outputs the number of times console.count() has been called with the given label.

Example

console.count(); // default: 1
console.count('default'); // default: 2
console.count('abc'); // abc: 1
console.count('xyz'); // xyz: 1
console.count('abc'); // abc: 2
console.count(); // default: 3

console.countReset([label])

  • label {String} The display label for the counter. default: 'default'.

Resets the internal counter specific to label.

Example

console.count('abc'); // abc: 1
console.countReset('abc');
console.count('abc'); // abc: 1

console.group()

Generate a print group.

Example

console.log('Outter1');
console.group();
console.log('Inner1');
console.log('Inner2');
console.groupEnd();
console.log('Outter2');

console.groupEnd()

End a print group.

Example

console.log('Outter1');
console.group();
console.log('Inner1');
console.group();
console.log('Inner2');
console.groupEnd();
console.log('Inner3');
console.groupEnd();
console.log('Outter2');

console.groupCollapsed()

Generate a print group.

Same as console.group().

console.time([label])

  • label {String} Timer label. default: 'default'.

Starts a timer that can be used to compute the duration of an operation. Timers are identified by a unique label. Use the same label when calling console.timeEnd() to stop the timer and output the elapsed time in milliseconds. Timer durations are accurate to the millisecond.

console.timeEnd([label])

  • label {String} Timer label. default: 'default'.

Stops a timer that was previously started by calling console.time() and prints the result.

Example

console.time('100000-elements');
for (var i = 0; i < 100000; i++) {}
console.timeEnd('100000-elements');
// prints 100000-elements: 10 ms

console.timeLog([label][, ...data])

  • label {String} Timer label. default: 'default'.
  • ...data {Any} Any number of any type of parameters that need to print.

For a timer that was previously started by calling console.time(), prints the elapsed time and other data arguments.

Example

console.time('100000-elements');
for (var i = 0; i < 10; i++) {
  for (var j = 0; j < 100000; j++) {}
  console.timeLog('100000-elements', 'times:', i);
}
console.timeEnd('100000-elements');

console.tagFilterAdd(id[, deny])

  • id {String} | {Array} Module ID or module ID array.
  • deny {Boolean} Whether to add to blacklist. default: false, means whitelist.

Add a specified module to the tag output filter. JSRE provides a filtering function for console.tag(), which is divided into whitelist and blacklist. When both lists are empty, when console.tagEnable is true, all tag prints will be output. When a whitelist exists, Only the module tag output allowed by the whitelist is printed. If blacklist exists, the module tag output in the blacklist will be prohibited, and the whitelist has higher priority than the blacklist.

Example

// Only print iosched module
console.tagFilterAdd('iosched');

// Print iosched and webapp module
console.tagFilterAdd(['iosched', 'webapp']);

// Deny coap module tag print
console.tagFilterAdd('coap', true);

console.tagFilterDelete(id)

  • id {String} | {Array} Module ID or module ID array.

Remove the previously added module from the tag filter.

Example

console.tagFilterDelete('iosched');
文档内容是否对您有所帮助?
有帮助
没帮助