From 9cc7b529d6431d3df35cc35498d84e1f7e8ea90f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 6 Jul 2016 17:35:52 -0700 Subject: [PATCH] logger. --- lib/bcoin/chainentry.js | 2 +- lib/bcoin/logger.js | 81 ++++++++++++++++++++++++++--------------- lib/bcoin/node.js | 1 + 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/lib/bcoin/chainentry.js b/lib/bcoin/chainentry.js index ac0543d2..9a8193b0 100644 --- a/lib/bcoin/chainentry.js +++ b/lib/bcoin/chainentry.js @@ -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)); diff --git a/lib/bcoin/logger.js b/lib/bcoin/logger.js index 6873ac58..fa6c62d1 100644 --- a/lib/bcoin/logger.js +++ b/lib/bcoin/logger.js @@ -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'); }; /** diff --git a/lib/bcoin/node.js b/lib/bcoin/node.js index b2a0ebd6..c46b17de 100644 --- a/lib/bcoin/node.js +++ b/lib/bcoin/node.js @@ -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 }); }