Skip to content

Commit

Permalink
Default log is bare, must call withTimestamp to get timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
brentvatne committed Mar 31, 2017
1 parent c38e4b7 commit e80f488
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
39 changes: 20 additions & 19 deletions react-native-scripts/src/util/log.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import chalk from 'chalk';

function log() {
const args = [prefix].concat(Array.prototype.slice.call(arguments, 0));

respectProgressBars(() => {
console.log(...args);
});
};

log.withTimestamp = function() {
const prefix = chalk.dim(new Date().toLocaleTimeString()) + ':';
const args = [prefix].concat(Array.prototype.slice.call(arguments, 0));

respectProgressBars(() => {
console.log(...args);
});
}

let _bundleProgressBar;
log.setBundleProgressBar = function(bundleProgressBar) {
_bundleProgressBar = bundleProgressBar;
};

function respectProgressBars(commitLogs) {
if (_bundleProgressBar) {
Expand All @@ -13,23 +33,4 @@ function respectProgressBars(commitLogs) {
}
}

log.setBundleProgressBar = function setBundleProgressBar(bar) {
_bundleProgressBar = bar;
};

function log() {
const prefix = chalk.dim(new Date().toLocaleTimeString()) + ':';
var args = [prefix].concat(Array.prototype.slice.call(arguments, 0));

respectProgressBars(() => {
console.log(...args);
});
}

log.withoutPrefix = function(...args) {
respectProgressBars(() => {
console.log(...args);
});
};

export default log;
24 changes: 12 additions & 12 deletions react-native-scripts/src/util/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function installExitHooks(projectDir) {
}

process.on('SIGINT', () => {
log('\nStopping packager...');
log.withTimestamp('\nStopping packager...');
cleanUpPackager(projectDir).then(() => {
// TODO: this shows up after process exits, fix it
log(chalk.green('Packager stopped.'));
log.withTimestamp(chalk.green('Packager stopped.'));
process.exit();
});
});
Expand Down Expand Up @@ -66,7 +66,7 @@ function run(onReady: () => ?any, options: Object = {}) {
// this is set when we previously encountered an error
// TODO clearConsole();
}
log(`Running app on ${chunk.deviceName}\n`);
log.withTimestamp(`Running app on ${chunk.deviceName}\n`);
return;
}

Expand All @@ -82,20 +82,20 @@ function run(onReady: () => ?any, options: Object = {}) {
if (packagerReady) {
const message = `${chunk.msg.trim()}\n`;
if (chunk.level <= bunyan.INFO) {
log(message);
log.withTimestamp(message);
} else if (chunk.level === bunyan.WARN) {
log(chalk.yellow(message));
log.withTimestamp(chalk.yellow(message));
} else {
log(chalk.red(message));
log.withTimestamp(chalk.red(message));

// if you run into a syntax error then we should clear log output on reload
needsClear = message.indexOf('SyntaxError') >= 0;
}
} else {
if (chunk.level >= bunyan.ERROR) {
log.withoutPrefix(chalk.yellow('***ERROR STARTING PACKAGER***'));
log.withoutPrefix(logBuffer);
log.withoutPrefix(chalk.red(chunk.msg));
log(chalk.yellow('***ERROR STARTING PACKAGER***'));
log(logBuffer);
log(chalk.red(chunk.msg));
logBuffer = '';
} else {
logBuffer += chunk.msg + '\n';
Expand Down Expand Up @@ -129,7 +129,7 @@ function run(onReady: () => ?any, options: Object = {}) {
if (progressBar) {
log.setBundleProgressBar(null);
progressBar = null;
log(chalk.green('Finished building JavaScript bundle'));
log.withTimestamp(chalk.green('Finished building JavaScript bundle'));
}
},
updateLogs: updater => {
Expand All @@ -151,12 +151,12 @@ function run(onReady: () => ?any, options: Object = {}) {
});

installExitHooks(projectDir);
log('Starting packager...');
log.withTimestamp('Starting packager...');

Project.startAsync(projectDir, options).then(
() => {},
reason => {
log(chalk.red(`Error starting packager: ${reason.stack}`));
log.withTimestamp(chalk.red(`Error starting packager: ${reason.stack}`));
process.exit(1);
}
);
Expand Down

0 comments on commit e80f488

Please sign in to comment.