console: implement timeLog method

Refs: https://github.com/whatwg/console/pull/138

PR-URL: https://github.com/nodejs/node/pull/21312
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Michaël Zasso 2018-06-13 15:30:21 +02:00
parent 7951e6d26b
commit b016745e48
No known key found for this signature in database
GPG key ID: 770F7A9A5AE15600
3 changed files with 66 additions and 15 deletions

View file

@ -235,18 +235,34 @@ Console.prototype.time = function time(label = 'default') {
};
Console.prototype.timeEnd = function timeEnd(label = 'default') {
const hasWarned = timeLogImpl(this, 'timeEnd', label);
if (!hasWarned) {
this._times.delete(label);
}
};
Console.prototype.timeLog = function timeLog(label, ...data) {
timeLogImpl(this, 'timeLog', label, data);
};
// Returns true if label was not found
function timeLogImpl(self, name, label = 'default', data) {
// Coerces everything other than Symbol to a string
label = `${label}`;
const time = this._times.get(label);
const time = self._times.get(label);
if (!time) {
process.emitWarning(`No such label '${label}' for console.timeEnd()`);
return;
process.emitWarning(`No such label '${label}' for console.${name}()`);
return true;
}
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);
};
if (data === undefined) {
self.log('%s: %sms', label, ms.toFixed(3));
} else {
self.log('%s: %sms', label, ms.toFixed(3), ...data);
}
return false;
}
Console.prototype.trace = function trace(...args) {
const err = {