This commit is contained in:
Christopher Jeffrey 2016-07-06 17:35:52 -07:00
parent c27466a7c5
commit 9cc7b529d6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 54 additions and 30 deletions

View File

@ -182,7 +182,7 @@ ChainEntry.prototype.getAncestors = function getAncestors(max, callback) {
var cached; var cached;
if (max === 0) if (max === 0)
return callback(null, []); return callback(null, ancestors);
assert(utils.isNumber(max)); assert(utils.isNumber(max));

View File

@ -34,10 +34,14 @@ function Logger(options) {
this.level = Logger.levels.warning; this.level = Logger.levels.warning;
this.colors = options.colors !== false; this.colors = options.colors !== false;
this.console = options.console !== false;
this.file = options.file; this.file = options.file;
this.stream = null; this.stream = options.stream;
this.closed = false; this.closed = false;
assert(!this.file || typeof this.file === 'string', 'Bad file.');
assert(!this.stream || typeof this.stream.write === 'function', 'Bad stream.');
if (!process.stdout || !process.stdout.isTTY) if (!process.stdout || !process.stdout.isTTY)
this.colors = false; this.colors = false;
@ -76,6 +80,8 @@ Logger.colors = {
Logger.prototype.open = function open() { Logger.prototype.open = function open() {
this.closed = false; this.closed = false;
if (this.stream)
this.stream.open();
}; };
/** /**
@ -87,13 +93,12 @@ Logger.prototype.close = function close() {
return; return;
try { try {
this.stream.destroy(); this.stream.close();
} catch (e) { } catch (e) {
; ;
} }
this.closed = true; this.closed = true;
this.stream = null;
}; };
/** /**
@ -205,6 +210,22 @@ Logger.prototype.log = function log(level, args) {
assert(Logger.levels[level] != null, 'Invalid log level.'); assert(Logger.levels[level] != null, 'Invalid log level.');
this.writeConsole(level, args);
this.writeStream(level, args);
};
/**
* Write log to the console.
* @param {String} level
* @param {Object[]} args
*/
Logger.prototype.writeConsole = function writeConsole(level, args) {
var prefix, msg, color;
if (!this.console)
return;
prefix = '[' + level + '] '; prefix = '[' + level + '] ';
if (utils.isBrowser) { if (utils.isBrowser) {
@ -214,12 +235,9 @@ Logger.prototype.log = function log(level, args) {
msg = prefix + msg; msg = prefix + msg;
if (level === 'error') return level === 'error'
console.error(msg); ? console.error(msg)
else : console.log(msg);
console.log(msg);
return;
} }
if (this.colors) { if (this.colors) {
@ -229,37 +247,41 @@ Logger.prototype.log = function log(level, args) {
msg = prefix + utils.format(args, this.colors); msg = prefix + utils.format(args, this.colors);
if (level === 'error') return level === 'error'
process.stderr.write(msg + '\n'); ? process.stderr.write(msg + '\n')
else : process.stdout.write(msg + '\n');
process.stdout.write(msg + '\n');
if (this.file) {
if (this.colors)
msg = prefix + utils.format(args, false);
this.write(msg);
}
}; };
/** /**
* Write a string to the output stream (usually a file). * Write a string to the output stream (usually a file).
* @param {String} msg * @param {String} level
* @param {Object[]} args
*/ */
Logger.prototype.write = function write(msg) { Logger.prototype.writeStream = function writeStream(level, args) {
if (!fs) var prefix, msg;
return;
if (this.closed) if (this.closed)
return; return;
if (!this.stream) { if (!this.stream) {
if (!this.file)
return;
if (utils.isBrowser)
return;
utils.mkdir(this.file, true); utils.mkdir(this.file, true);
this.stream = fs.createWriteStream(this.file, { flags: 'a' }); this.stream = fs.createWriteStream(this.file, { flags: 'a' });
this.stream.on('error', function() {}); this.stream.on('error', function() {});
} }
this.stream.write(process.pid + ' (' + utils.date() + '): ' + msg + '\n'); prefix = '[' + level + '] ';
msg = prefix + utils.format(args, false);
msg = '(' + utils.date() + '): ' + msg + '\n';
if (!utils.isBrowser)
msg = process.pid + ' ' + msg;
this.stream.write(msg);
}; };
/** /**
@ -272,17 +294,18 @@ Logger.prototype.write = function write(msg) {
Logger.prototype._error = function error(err) { Logger.prototype._error = function error(err) {
var msg; var msg;
if (utils.isBrowser) { if (this.closed)
console.error(err);
return; return;
}
if (utils.isBrowser && this.console)
console.error(err);
msg = (err.message + '').replace(/^ *Error: */, ''); msg = (err.message + '').replace(/^ *Error: */, '');
this.log('error', [msg]); this.log('error', [msg]);
if (this.file) if (this.stream)
this.write(err.stack + ''); this.stream.write(err.stack + '\n');
}; };
/** /**

View File

@ -47,6 +47,7 @@ function Node(options) {
if (!this.logger) { if (!this.logger) {
this.logger = new bcoin.logger({ this.logger = new bcoin.logger({
level: options.logLevel || 'none', level: options.logLevel || 'none',
console: options.logConsole,
file: options.logFile file: options.logFile
}); });
} }