From f1ced58587afbdf17410aaea0e8ca7466b4ac328 Mon Sep 17 00:00:00 2001 From: cubedro Date: Thu, 25 Jun 2015 01:59:12 +0300 Subject: [PATCH] added verbosity --- lib/node.js | 61 +++++++++++++++++++++++---------------------- lib/utils/logger.js | 39 ++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 31 deletions(-) diff --git a/lib/node.js b/lib/node.js index 1bce826..5067452 100644 --- a/lib/node.js +++ b/lib/node.js @@ -48,14 +48,15 @@ Socket = Primus.createSocket({ if(process.env.NODE_ENV === 'production' && INSTANCE_NAME === "") { - INSTANCE_NAME = shelljs.exec('ec2metadata --instance-id 2>/dev/null', {silent: true}).output; + console.error("No instance name specified!"); + process.exit(1); } -console.log(''); -console.info('NET STATS CLIENT'); -console.success('v' + pjson.version); -console.log(''); -console.log(''); +console.info(' '); +console.info(' ', 'NET STATS CLIENT'); +console.success(' ', 'v' + pjson.version); +console.info(' '); +console.info(' '); function Node () { @@ -145,11 +146,11 @@ Node.prototype.checkWeb3Connection = function() if (!this._web3) { try { - var tmp = web3.version.client; + var eth_version = web3.version.client; - if( !_.isUndefined(tmp) ) + if( !_.isUndefined(eth_version) ) { - console.log(' ', tmp); + console.success(eth_version); console.success('Web3 connection established'); this._web3 = true; @@ -222,7 +223,7 @@ Node.prototype.setupSockets = function() socket.on('open', function open() { console.info('wsc', 'The socket connection has been opened.'); - console.log(' ', 'Trying to login'); + console.info(' ', 'Trying to login'); socket.emit('hello', { id: self.id, @@ -241,11 +242,11 @@ Node.prototype.setupSockets = function() }) .on('data', function incoming(data) { - console.info('Socket received some data', data); + console.stats('Socket received some data', data); }) .on('history', function (data) { - console.info('his', 'Got history request'); + console.stats('his', 'Got history request'); self.getHistory( data ); }) @@ -325,7 +326,7 @@ Node.prototype.emit = function(message, payload) { try { socket.emit(message, payload); - console.success('wsc', 'Socket emited message:', chalk.reset.cyan(message)); + console.sstats('wsc', 'Socket emited message:', chalk.reset.cyan(message)); // console.success('wsc', payload); } catch (err) { @@ -442,12 +443,12 @@ Node.prototype.validateLatestBlock = function (error, result, timeString) if( _.isEqual(JSON.stringify(this.stats.block), JSON.stringify(block)) ) return false; - console.info(this.stats.block); - console.info(block); + console.stats(this.stats.block); + console.stats(block); console.warn("Blocks are different... updating block"); } - console.success("==>", "Got block:", chalk.reset.red(block.number)); + console.sstats("==>", "Got block:", chalk.reset.red(block.number)); this.stats.block = block; this.sendBlockUpdate(); @@ -478,9 +479,9 @@ Node.prototype.getStats = function(forced) if (this._web3 && (lastFetchAgo >= UPDATE_INTERVAL || forced === true)) { - console.info('==>', 'Getting stats') - console.log(' ', 'last update:', chalk.reset.cyan(lastFetchAgo)); - console.log(' ', 'forced:', chalk.reset.cyan(forced === true)); + console.stats('==>', 'Getting stats') + console.stats(' ', 'last update:', chalk.reset.cyan(lastFetchAgo)); + console.stats(' ', 'forced:', chalk.reset.cyan(forced === true)); async.parallel({ peers: function (callback) @@ -515,7 +516,7 @@ Node.prototype.getStats = function(forced) results.end = _.now(); results.diff = results.end - self._lastFetch; - console.success('==>', 'Got getStats results in', chalk.reset.cyan(results.diff, 'ms')); + console.sstats('==>', 'Got getStats results in', chalk.reset.cyan(results.diff, 'ms')); if(results.peers !== null) { @@ -543,7 +544,7 @@ Node.prototype.getPending = function() if (this._web3) { - console.info('==>', 'Getting Pending') + console.stats('==>', 'Getting Pending') web3.eth.getBlockTransactionCount('pending', function (err, pending) { @@ -556,7 +557,7 @@ Node.prototype.getPending = function() results.end = _.now(); results.diff = results.end - now; - console.success('==>', 'Got', chalk.reset.red(pending) , chalk.reset.bold.green('pending tx'+ (pending === 1 ? '' : 's') + ' in'), chalk.reset.cyan(results.diff, 'ms')); + console.sstats('==>', 'Got', chalk.reset.red(pending) , chalk.reset.bold.green('pending tx'+ (pending === 1 ? '' : 's') + ' in'), chalk.reset.cyan(results.diff, 'ms')); self.stats.pending = pending; @@ -583,7 +584,7 @@ Node.prototype.getHistory = function (range) if (!_.isUndefined(range.list)) interv = range.list; - console.info('his', 'Getting history from', chalk.reset.cyan(interv[0]), 'to', chalk.reset.cyan(interv[interv.length - 1])); + console.stats('his', 'Getting history from', chalk.reset.cyan(interv[0]), 'to', chalk.reset.cyan(interv[interv.length - 1])); async.mapSeries(interv, function (number, callback) { @@ -656,20 +657,20 @@ Node.prototype.prepareStats = function () Node.prototype.sendBlockUpdate = function() { this._lastBlockSentAt = _.now(); - console.info("wsc", "Sending", chalk.reset.red("block"), chalk.bold.white("update")); + console.stats("wsc", "Sending", chalk.reset.red("block"), chalk.bold.white("update")); this.emit('block', this.prepareBlock()); } Node.prototype.sendPendingUpdate = function() { - console.info("wsc", "Sending pending update"); + console.stats("wsc", "Sending pending update"); this.emit('pending', this.preparePending()); } Node.prototype.sendStatsUpdate = function (force) { if( this.changed() || force ) { - console.info("wsc", "Sending", chalk.reset.blue((force ? "forced" : "changed")), chalk.bold.white("update")); + console.stats("wsc", "Sending", chalk.reset.blue((force ? "forced" : "changed")), chalk.bold.white("update")); this.emit('stats', this.prepareStats()); } } @@ -703,13 +704,13 @@ Node.prototype.setWatches = function() this._latestQueue.drain = function() { - console.success("Finished processing", 'latest', 'queue'); + console.sstats("Finished processing", 'latest', 'queue'); self.getPending(); } this._debouncedChain = debounce(function(hash) { - console.info('>>>', 'Debounced'); + console.stats('>>>', 'Debounced'); self._latestQueue.push(hash); }, 120); @@ -730,7 +731,7 @@ Node.prototype.setWatches = function() hash = web3.eth.blockNumber; } - console.info('>>>', 'Chain Filter triggered: ', chalk.reset.red(hash), '- last trigger:', chalk.reset.cyan(time)); + console.stats('>>>', 'Chain Filter triggered: ', chalk.reset.red(hash), '- last trigger:', chalk.reset.cyan(time)); if(time < self._chan_min_time) { @@ -788,7 +789,7 @@ Node.prototype.setWatches = function() var time = now - self._lastPendingLog; self._lastPendingLog = now; - console.info('>>>', 'Pending Filter triggered:', chalk.reset.red(hash), '- last trigger:', chalk.reset.cyan(time)); + console.stats('>>>', 'Pending Filter triggered:', chalk.reset.red(hash), '- last trigger:', chalk.reset.cyan(time)); if(time > 50) { diff --git a/lib/utils/logger.js b/lib/utils/logger.js index f45b934..b50e68f 100644 --- a/lib/utils/logger.js +++ b/lib/utils/logger.js @@ -10,7 +10,8 @@ var signs = [ '===', '>>>', 'xxx', - '=H=' + '=H=', + ' ' ]; var types = [ @@ -20,6 +21,15 @@ var types = [ 'his' ]; +var verbosity = [ + [], + ['error', 'warn'], + ['info', 'error', 'warn', 'success'], + ['info', 'stats', 'sstats', 'error', 'warn', 'success', 'time', 'timeEnd'] +]; + +var ENV_VERBOSITY = process.env.VERBOSITY || 2; + [ { name: "info", @@ -31,6 +41,17 @@ var types = [ return [sign, message]; } }, + { + name: "stats", + inherit: 'log', + sign: '=s=', + signColor: chalk.blue, + messageColor: chalk.bold, + formatter: function (sign, message) + { + return [sign, message]; + } + }, { name: "success", inherit: 'log', @@ -42,6 +63,17 @@ var types = [ return [sign, message]; } }, + { + name: "sstats", + inherit: 'log', + sign: '=✓=', + signColor: chalk.green, + messageColor: chalk.bold.green, + formatter: function (sign, message) + { + return [sign, message]; + } + }, { name: "warn", sign: '=!=', @@ -84,6 +116,8 @@ var types = [ } ].forEach( function (item) { + var fnName = item.name; + if(item.inherit !== undefined) console[item.name] = console[item.inherit]; @@ -91,6 +125,9 @@ var types = [ console[item.name] = function () { + if(verbosity[ENV_VERBOSITY].indexOf(fnName) === -1) + return false; + var args = Array.prototype.slice.call(arguments); var sign = item.sign; var section = 'eth';