Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to console as per spec #3473

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions doc/api/console.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ is used on each argument. See [util.format()][] for more information.

Same as `console.log`.

### console.debug([data][, ...])

Same as `console.log`.

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

Same as `console.log` but prints to stderr.
Expand All @@ -56,6 +60,10 @@ Same as `console.log` but prints to stderr.

Same as `console.error`.

### console.exception([data][, ...])

Same as `console.error`

### console.dir(obj[, options])

Uses `util.inspect` on `obj` and prints resulting string to stdout. This function
Expand Down Expand Up @@ -105,6 +113,24 @@ to the current position.
Similar to [assert.ok()][], but the error message is formatted as
`util.format(message...)`.

### Compatibility functions

NOTE: The following functions are defined for compatibility purpose, and are not expected to do anything

* `console.group`
* `console.groupCollapsed`
* `console.groupEnd`
* `console.clear`
* `console.count`
* `console.dirxml`
* `console.isIndependentlyComposed`
* `console.table`
* `console.profile`
* `console.profileEnd`
* `console.timeline`
* `console.timelineEnd`
* `console.timeStamp`

## Class: Console

<!--type=class-->
Expand Down
48 changes: 48 additions & 0 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Console.prototype.log = function() {
Console.prototype.info = Console.prototype.log;


Console.prototype.debug = Console.prototype.log;


Console.prototype.warn = function() {
this._stderr.write(util.format.apply(this, arguments) + '\n');
};
Expand All @@ -48,6 +51,9 @@ Console.prototype.warn = function() {
Console.prototype.error = Console.prototype.warn;


Console.prototype.exception = Console.prototype.warn;


Console.prototype.dir = function(object, options) {
this._stdout.write(util.inspect(object, util._extend({
customInspect: false
Expand Down Expand Up @@ -90,5 +96,47 @@ Console.prototype.assert = function(expression) {
};


Console.prototype.group = noop;


Console.prototype.groupCollapsed = noop;


Console.prototype.groupEnd = noop;


Console.prototype.clear = noop;


Console.prototype.count = noop;


Console.prototype.dirxml = noop;


Console.prototype.isIndependentlyComposed = noop;


Console.prototype.table = noop;


Console.prototype.profile = noop;


Console.prototype.profileEnd = noop;


Console.prototype.timeline = noop;


Console.prototype.timelineEnd = noop;


Console.prototype.timeStamp = noop;


function noop() {}


module.exports = new Console(process.stdout, process.stderr);
module.exports.Console = Console;
30 changes: 30 additions & 0 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,36 @@ assert.doesNotThrow(function() {
console.timeEnd('label');
});

assert.doesNotThrow(function() {
console.group('test');
console.groupEnd();
});

assert.doesNotThrow(function() {
console.groupCollapsed('test');
console.groupEnd();
});

assert.doesNotThrow(console.clear);

assert.doesNotThrow(console.count);

assert.doesNotThrow(console.dirxml);

assert.doesNotThrow(console.exception);

assert.doesNotThrow(console.isIndependentlyComposed);

assert.doesNotThrow(console.profile);

assert.doesNotThrow(console.profileEnd);

assert.doesNotThrow(console.timeline);

assert.doesNotThrow(console.timelineEnd);

assert.doesNotThrow(console.timeStamp);

// an Object with a custom .inspect() function
var custom_inspect = { foo: 'bar', inspect: function() { return 'inspect'; } };

Expand Down