-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
console: using class #9068
Closed
Closed
console: using class #9068
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,100 +2,89 @@ | |
|
||
const util = require('util'); | ||
|
||
function Console(stdout, stderr) { | ||
if (!(this instanceof Console)) { | ||
return new Console(stdout, stderr); | ||
} | ||
if (!stdout || typeof stdout.write !== 'function') { | ||
throw new TypeError('Console expects a writable stream instance'); | ||
} | ||
if (!stderr) { | ||
stderr = stdout; | ||
} else if (typeof stderr.write !== 'function') { | ||
throw new TypeError('Console expects writable stream instances'); | ||
class Console { | ||
constructor(stdout, stderr) { | ||
if (!(this instanceof Console)) | ||
return new Console(stdout, stderr); | ||
|
||
if (!stdout || typeof stdout.write !== 'function') | ||
throw new TypeError('Console expects a writable stream instance'); | ||
|
||
if (!stderr) | ||
stderr = stdout; | ||
else if (typeof stderr.write !== 'function') | ||
throw new TypeError('Console expects writable stream instances'); | ||
|
||
this._stdout = stdout; | ||
this._stderr = stderr; | ||
this._times = new Map(); | ||
|
||
const methods = [ | ||
'log', 'info', 'warn', | ||
'error', 'dir', 'time', | ||
'timeEnd', 'trace', 'assert' | ||
]; | ||
|
||
methods.forEach((method) => { | ||
this[method] = this[method].bind(this); | ||
}); | ||
} | ||
|
||
var prop = { | ||
writable: true, | ||
enumerable: false, | ||
configurable: true | ||
}; | ||
prop.value = stdout; | ||
Object.defineProperty(this, '_stdout', prop); | ||
prop.value = stderr; | ||
Object.defineProperty(this, '_stderr', prop); | ||
prop.value = new Map(); | ||
Object.defineProperty(this, '_times', prop); | ||
|
||
// bind the prototype functions to this Console instance | ||
var keys = Object.keys(Console.prototype); | ||
for (var v = 0; v < keys.length; v++) { | ||
var k = keys[v]; | ||
this[k] = this[k].bind(this); | ||
// As of v8 5.0.71.32, the combination of rest param, template string | ||
// and .apply(null, args) benchmarks consistently faster than using | ||
// the spread operator when calling util.format. | ||
log(...args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is still slow, even with V8 5.4 currently in master. Ditto for the other rest args uses. |
||
this._stdout.write(`${util.format.apply(null, args)}\n`); | ||
} | ||
} | ||
|
||
|
||
// As of v8 5.0.71.32, the combination of rest param, template string | ||
// and .apply(null, args) benchmarks consistently faster than using | ||
// the spread operator when calling util.format. | ||
Console.prototype.log = function(...args) { | ||
this._stdout.write(`${util.format.apply(null, args)}\n`); | ||
}; | ||
|
||
|
||
Console.prototype.info = Console.prototype.log; | ||
|
||
|
||
Console.prototype.warn = function(...args) { | ||
this._stderr.write(`${util.format.apply(null, args)}\n`); | ||
}; | ||
|
||
|
||
Console.prototype.error = Console.prototype.warn; | ||
|
||
info(...args) { | ||
this.log.apply(this, args); | ||
} | ||
|
||
Console.prototype.dir = function(object, options) { | ||
options = Object.assign({customInspect: false}, options); | ||
this._stdout.write(`${util.inspect(object, options)}\n`); | ||
}; | ||
warn(...args) { | ||
this._stderr.write(`${util.format.apply(null, args)}\n`); | ||
} | ||
|
||
error(...args) { | ||
this.warn.apply(this, args); | ||
} | ||
|
||
Console.prototype.time = function(label) { | ||
this._times.set(label, process.hrtime()); | ||
}; | ||
dir(object, options) { | ||
options = Object.assign({customInspect: false}, options); | ||
this._stdout.write(`${util.inspect(object, options)}\n`); | ||
} | ||
|
||
time(label) { | ||
this._times.set(label, process.hrtime()); | ||
} | ||
|
||
Console.prototype.timeEnd = function(label) { | ||
const time = this._times.get(label); | ||
if (!time) { | ||
process.emitWarning(`No such label '${label}' for console.timeEnd()`); | ||
return; | ||
timeEnd(label) { | ||
const time = this._times.get(label); | ||
if (!time) { | ||
process.emitWarning(`No such label '${label}' for console.timeEnd()`); | ||
return; | ||
} | ||
const duration = process.hrtime(time); | ||
const ms = duration[0] * 1000 + duration[1] / 1e6; | ||
this.log('%s: %sms', label, ms.toFixed(3)); | ||
this._times.delete(label); | ||
} | ||
const duration = process.hrtime(time); | ||
const ms = duration[0] * 1000 + duration[1] / 1e6; | ||
this.log('%s: %sms', label, ms.toFixed(3)); | ||
this._times.delete(label); | ||
}; | ||
|
||
|
||
Console.prototype.trace = function trace(...args) { | ||
// TODO probably can to do this better with V8's debug object once that is | ||
// exposed. | ||
var err = new Error(); | ||
err.name = 'Trace'; | ||
err.message = util.format.apply(null, args); | ||
Error.captureStackTrace(err, trace); | ||
this.error(err.stack); | ||
}; | ||
|
||
|
||
Console.prototype.assert = function(expression, ...args) { | ||
if (!expression) { | ||
require('assert').ok(false, util.format.apply(null, args)); | ||
|
||
trace(...args) { | ||
// TODO probably can to do this better with V8's debug object once that is | ||
// exposed. | ||
var err = new Error(); | ||
err.name = 'Trace'; | ||
err.message = util.format.apply(null, args); | ||
Error.captureStackTrace(err, this.trace); | ||
this.error(err.stack); | ||
} | ||
}; | ||
|
||
assert(expression, ...args) { | ||
if (!expression) | ||
require('assert').ok(false, util.format.apply(null, args)); | ||
} | ||
} | ||
|
||
module.exports = new Console(process.stdout, process.stderr); | ||
module.exports.Console = Console; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is not going to work, so you can leave it out from this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean 7, 8 lines?