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;
if (max === 0)
return callback(null, []);
return callback(null, ancestors);
assert(utils.isNumber(max));

View File

@ -34,10 +34,14 @@ function Logger(options) {
this.level = Logger.levels.warning;
this.colors = options.colors !== false;
this.console = options.console !== false;
this.file = options.file;
this.stream = null;
this.stream = options.stream;
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)
this.colors = false;
@ -76,6 +80,8 @@ Logger.colors = {
Logger.prototype.open = function open() {
this.closed = false;
if (this.stream)
this.stream.open();
};
/**
@ -87,13 +93,12 @@ Logger.prototype.close = function close() {
return;
try {
this.stream.destroy();
this.stream.close();
} catch (e) {
;
}
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.');
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 + '] ';
if (utils.isBrowser) {
@ -214,12 +235,9 @@ Logger.prototype.log = function log(level, args) {
msg = prefix + msg;
if (level === 'error')
console.error(msg);
else
console.log(msg);
return;
return level === 'error'
? console.error(msg)
: console.log(msg);
}
if (this.colors) {
@ -229,37 +247,41 @@ Logger.prototype.log = function log(level, args) {
msg = prefix + utils.format(args, this.colors);
if (level === 'error')
process.stderr.write(msg + '\n');
else
process.stdout.write(msg + '\n');
if (this.file) {
if (this.colors)
msg = prefix + utils.format(args, false);
this.write(msg);
}
return level === 'error'
? process.stderr.write(msg + '\n')
: process.stdout.write(msg + '\n');
};
/**
* 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) {
if (!fs)
return;
Logger.prototype.writeStream = function writeStream(level, args) {
var prefix, msg;
if (this.closed)
return;
if (!this.stream) {
if (!this.file)
return;
if (utils.isBrowser)
return;
utils.mkdir(this.file, true);
this.stream = fs.createWriteStream(this.file, { flags: 'a' });
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) {
var msg;
if (utils.isBrowser) {
console.error(err);
if (this.closed)
return;
}
if (utils.isBrowser && this.console)
console.error(err);
msg = (err.message + '').replace(/^ *Error: */, '');
this.log('error', [msg]);
if (this.file)
this.write(err.stack + '');
if (this.stream)
this.stream.write(err.stack + '\n');
};
/**

View File

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