Skip to content

Commit cc3cca4

Browse files
x1ddospaulirish
authored andcommitted
cli: use logger to print status messages (#530)
When cli is run with the output directed to stdout, it may become quite inconvenient for the progress status messages to pop up on the stdout as they are being mixed with the audit formatted results. This change makes both `status` and `statusEnd` events be emitted on `log.events`, and replaces `console.time` and `console.timeEnd` with outputting the timers using lighthouse logger, so that all timer messages go to the same place, usually stderr. In any event, this change makes it easier to control and redirect status messages in the same way all other logging is done.
1 parent 60a06c5 commit cc3cca4

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

lighthouse-cli/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const yargs = require('yargs');
2222
const semver = require('semver');
2323
const Printer = require('./printer');
2424
const lighthouse = require('../lighthouse-core');
25+
const log = require('../lighthouse-core/lib/log');
2526

2627
// node 5.x required due to use of ES2015 features, like spread operator
2728
if (semver.lt(process.version, '5.0.0')) {
@@ -146,6 +147,25 @@ if (!flags.auditWhitelist || flags.auditWhitelist === 'all') {
146147
flags.auditWhitelist = new Set(flags.auditWhitelist.split(',').map(a => a.toLowerCase()));
147148
}
148149

150+
// Listen on progress events, record their start timestamps
151+
// and print result using the logger.
152+
let timers = {};
153+
log.events.on('status', function(event) {
154+
let msg = event[1];
155+
if (!msg) {
156+
return;
157+
}
158+
timers[msg] = Date.now();
159+
});
160+
log.events.on('statusEnd', function(event) {
161+
let msg = event[1];
162+
if (!msg) {
163+
return;
164+
}
165+
let t = Date.now() - timers[msg];
166+
log.log('Timer', `${msg} ${t}ms`);
167+
});
168+
149169
// kick off a lighthouse run
150170
lighthouse(url, flags, config)
151171
.then(results => Printer.write(results, outputMode, outputPath))

lighthouse-core/lib/log.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ const events = new Emitter();
4444
module.exports = {
4545
setLevel,
4646
events,
47-
log(title, msg) {
48-
if (title === 'status') {
49-
console.time(msg);
50-
events.emit('status', arguments);
51-
}
52-
if (title === 'statusEnd') {
53-
console.timeEnd(msg);
47+
log(title) {
48+
if (title === 'status' || title === 'statusEnd') {
49+
events.emit(title, arguments);
5450
}
5551
return _log(title, arguments);
5652
},

0 commit comments

Comments
 (0)