added colors to output

This commit is contained in:
cubedro 2015-05-05 06:59:48 +03:00
parent 99edf03c35
commit f7f0765434
3 changed files with 173 additions and 40 deletions

7
app.js
View File

@ -5,13 +5,14 @@ var nodeModel = require('./lib/node');
var node = new nodeModel(); var node = new nodeModel();
var gracefulShutdown = function() { var gracefulShutdown = function() {
console.warn("Received kill signal, shutting down gracefully."); console.log('');
console.error("xxx", "sys", "Received kill signal, shutting down gracefully.");
node.stop(); node.stop();
console.info("Closed node watcher"); console.info("xxx", "sys", "Closed node watcher");
setTimeout(function(){ setTimeout(function(){
console.info("Closed out remaining connections."); console.info("xxx", "sys", "Closed out remaining connections.");
process.exit(0); process.exit(0);
}, 5*1000); }, 5*1000);
} }

View File

@ -1,9 +1,11 @@
'use strict'; 'use strict';
require('./utils/logger.js');
var os = require('os');
var web3 = require('web3'); var web3 = require('web3');
var async = require('async'); var async = require('async');
var _ = require('lodash'); var _ = require('lodash');
var os = require('os');
var shelljs = require('shelljs'); var shelljs = require('shelljs');
var debounce = require('debounce'); var debounce = require('debounce');
var registrar = require('./registrar.js'); var registrar = require('./registrar.js');
@ -46,6 +48,11 @@ if(process.env.NODE_ENV === 'production' && INSTANCE_NAME === "")
INSTANCE_NAME = shelljs.exec('ec2metadata --instance-id', {silent: true}).output; INSTANCE_NAME = shelljs.exec('ec2metadata --instance-id', {silent: true}).output;
} }
console.log('');
console.info('NET STATS CLIENT');
console.success('v' + pjson.version);
console.log('');
console.log('');
function Node () function Node ()
{ {
@ -111,7 +118,7 @@ function Node ()
Node.prototype.startWeb3Connection = function() Node.prototype.startWeb3Connection = function()
{ {
console.info(chalk.blue('==>'), chalk.bold('Starting eth connection')); console.info('Starting web3 connection');
web3.setProvider( new web3.providers.HttpProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')) ); web3.setProvider( new web3.providers.HttpProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')) );
@ -129,8 +136,8 @@ Node.prototype.checkWeb3Connection = function()
if( !_.isUndefined(tmp) ) if( !_.isUndefined(tmp) )
{ {
console.log('eth', tmp); console.log(' ', tmp);
console.info('==>', 'Ethereum connection established'); console.success('Web3 connection established');
this._web3 = true; this._web3 = true;
this.init(); this.init();
@ -140,7 +147,7 @@ Node.prototype.checkWeb3Connection = function()
} }
catch(err) catch(err)
{ {
console.error('xx>', 'Ethereum connection attempt #' + this._called++ + ' failed;'); console.error('Web3 connection attempt', chalk.cyan('#' + this._called++), 'failed');
process.nextTick( function() process.nextTick( function()
{ {
@ -152,7 +159,7 @@ Node.prototype.checkWeb3Connection = function()
Node.prototype.startSocketConnection = function() Node.prototype.startSocketConnection = function()
{ {
console.info("==> Starting socket connection"); console.info('wsc', 'Starting socket connection');
socket = new Socket( process.env.WS_SERVER || 'ws://localhost:3000' ); socket = new Socket( process.env.WS_SERVER || 'ws://localhost:3000' );
@ -166,8 +173,8 @@ Node.prototype.setupSockets = function()
// Setup events // Setup events
socket.on('open', function open() socket.on('open', function open()
{ {
console.info('==> The connection has been opened.'); console.info('wsc', 'The connection has been opened.');
console.info('Trying to login'); console.log(' ', 'Trying to login');
socket.emit('hello', { socket.emit('hello', {
id: self.id, id: self.id,
@ -178,7 +185,7 @@ Node.prototype.setupSockets = function()
.on('ready', function() .on('ready', function()
{ {
self._socket = true; self._socket = true;
console.info('==> The connection has been established.'); console.success('wsc', 'The connection has been established.');
self.updateBlock(); self.updateBlock();
self.update(true); self.update(true);
@ -189,7 +196,7 @@ Node.prototype.setupSockets = function()
}) })
.on('history', function (data) .on('history', function (data)
{ {
console.info('==> Getting history'); console.info('wsc', 'Getting history');
var reqHistory = self.getHistory( data ); var reqHistory = self.getHistory( data );
@ -210,15 +217,15 @@ Node.prototype.setupSockets = function()
.on('end', function end() .on('end', function end()
{ {
self._socket = false; self._socket = false;
console.error('xx> Socket connection closed'); console.error('wsc', 'Socket connection closed');
}) })
.on('error', function error(err) .on('error', function error(err)
{ {
console.error("socket:", err); console.error('wsc', 'Socket error:', err);
}) })
.on('reconnecting', function reconnecting(opts) .on('reconnecting', function reconnecting(opts)
{ {
console.warn('!!!', 'We are scheduling a reconnect operation', opts); console.warn('wsc', 'We are scheduling a reconnect operation', opts);
}); });
} }
@ -230,7 +237,7 @@ Node.prototype.emit = function(message, payload)
socket.emit(message, payload); socket.emit(message, payload);
} }
catch (err) { catch (err) {
console.error("socket.emit:", err); console.error('wsc', 'Socket emit error:', err);
} }
} }
} }
@ -251,9 +258,8 @@ Node.prototype.setInactive = function()
Node.prototype.getInfo = function() Node.prototype.getInfo = function()
{ {
var start = _.now();
console.info('==>', 'Getting info'); console.info('==>', 'Getting info');
console.time('Got info');
try { try {
this.info.coinbase = web3.eth.coinbase; this.info.coinbase = web3.eth.coinbase;
@ -262,8 +268,8 @@ Node.prototype.getInfo = function()
this.info.protocol = web3.toDecimal(web3.version.ethereum); this.info.protocol = web3.toDecimal(web3.version.ethereum);
this.info.api = web3.version.api; this.info.api = web3.version.api;
console.info('==>', 'Got info in', _.now() - start, 'ms'); console.timeEnd('Got info');
console.info(' i ', this.info); console.info(this.info);
return true; return true;
} }
@ -297,7 +303,7 @@ Node.prototype.getBlock = function(number)
} }
} }
catch (err) { catch (err) {
console.error("getBlock(" + number + "):", err); console.error('getBlock(' + chalk.reset.cyan(number) + '):', err);
return false; return false;
} }
@ -349,7 +355,9 @@ Node.prototype.getStats = function(forced)
if (this._web3 && (lastFetchAgo >= UPDATE_INTERVAL || forced === true)) if (this._web3 && (lastFetchAgo >= UPDATE_INTERVAL || forced === true))
{ {
console.log('==> Getting stats; last update:', lastFetchAgo, '- forced:', (forced === true)); console.info('==>', 'Getting stats')
console.log(' ', 'last update:', chalk.reset.cyan(lastFetchAgo));
console.log(' ', 'forced:', chalk.reset.cyan(forced === true));
async.parallel({ async.parallel({
start: function (callback) start: function (callback)
@ -365,7 +373,7 @@ Node.prototype.getStats = function(forced)
peers = web3.toDecimal(web3.net.peerCount); peers = web3.toDecimal(web3.net.peerCount);
} }
catch (err) { catch (err) {
console.error('xx> PeerCount failed: ', err); console.error('xx>', 'PeerCount failed: ', err);
callback(err, null); callback(err, null);
} }
@ -375,13 +383,17 @@ Node.prototype.getStats = function(forced)
pending: function (callback) pending: function (callback)
{ {
async.nextTick(function () { async.nextTick(function () {
var pending;
try { try {
web3.eth.getBlockTransactionCount('pending', callback); pending = web3.eth.getBlockTransactionCount('pending');
} }
catch (err) { catch (err) {
console.error('xx> Pending failed: ', err); console.error('xx>', 'Pending failed: ', err);
callback(err, null); callback(err, null);
} }
callback(null, pending);
}); });
}, },
mining: function (callback) mining: function (callback)
@ -393,7 +405,7 @@ Node.prototype.getStats = function(forced)
mining = web3.eth.mining; mining = web3.eth.mining;
} }
catch (err) { catch (err) {
console.error('xx> Mining failed: ', err); console.error('xx>', 'Mining failed: ', err);
callback(err, null); callback(err, null);
} }
@ -409,7 +421,7 @@ Node.prototype.getStats = function(forced)
hashrate = web3.eth.hashrate; hashrate = web3.eth.hashrate;
} }
catch (err) { catch (err) {
console.error('xx> Hashrate failed: ', err); console.error('xx>', 'Hashrate failed: ', err);
callback(err, null); callback(err, null);
} }
@ -425,7 +437,7 @@ Node.prototype.getStats = function(forced)
gasPrice = web3.toBigNumber(web3.eth.gasPrice).toString(10); gasPrice = web3.toBigNumber(web3.eth.gasPrice).toString(10);
} }
catch (err) { catch (err) {
console.error('xx> gasPrice failed: ', err); console.error('xx>', 'gasPrice failed: ', err);
callback(err, null); callback(err, null);
} }
@ -441,7 +453,7 @@ Node.prototype.getStats = function(forced)
minerName = self.getMinerName(self.stats.block.miner); minerName = self.getMinerName(self.stats.block.miner);
} }
catch (err) { catch (err) {
console.error('xx> minerName failed: ', err); console.error('xx>', 'minerName failed: ', err);
callback(err, null); callback(err, null);
} }
@ -454,7 +466,7 @@ Node.prototype.getStats = function(forced)
self._tries++; self._tries++;
if (err) { if (err) {
console.error('xx> getStats error: ', err); console.error('xx>', 'getStats error: ', err);
self.setInactive(); self.setInactive();
@ -465,7 +477,7 @@ Node.prototype.getStats = function(forced)
results.diff = results.end - results.start; results.diff = results.end - results.start;
// console.log('==> Got getStats results: ', results); // console.log('==> Got getStats results: ', results);
console.log('==> Got getStats results in', results.diff, 'ms'); console.success('==>', 'Got getStats results in', chalk.reset.cyan(results.diff, 'ms'));
if(results.peers !== null) if(results.peers !== null)
{ {
@ -500,12 +512,12 @@ Node.prototype.getStatsBlock = function ()
if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.hash) && block.hash !== '?' ) if( !_.isUndefined(block) && !_.isUndefined(block.number) && !_.isUndefined(block.hash) && block.hash !== '?' )
{ {
console.log("==> Got block:", block.number, 'in', end - start, 'ms'); console.success("==>", "Got block:", chalk.reset.cyan(block.number), 'in', chalk.reset.cyan(end - start, 'ms'));
this.stats.block = block; this.stats.block = block;
} }
else else
{ {
console.error("xx> getStatsBlock: couldn't fetch block..."); console.error("xx>", "eth", "getStatsBlock: couldn't fetch block...");
} }
} }
@ -517,7 +529,7 @@ Node.prototype.getHistory = function (range)
var history = []; var history = [];
var interv = {}; var interv = {};
console.time('=== Got history in'); console.time('=H=', 'his', 'Got history in');
if( _.isUndefined(range) || range === null) if( _.isUndefined(range) || range === null)
{ {
@ -545,7 +557,7 @@ Node.prototype.getHistory = function (range)
} }
} }
console.timeEnd('=== Got history in'); console.timeEnd('=H=', 'his', 'Got history in');
return history.reverse(); return history.reverse();
} }
@ -603,7 +615,7 @@ Node.prototype.setWatches = function()
var time = now - self._lastChainLog; var time = now - self._lastChainLog;
self._lastChainLog = now; self._lastChainLog = now;
console.log('>>> Chain Filter triggered: ', now, '- last trigger:', time); console.info('>>>', 'Chain Filter triggered: ', chalk.reset.cyan(now), '- last trigger:', chalk.reset.cyan(time));
if(time > 50) if(time > 50)
{ {
@ -631,7 +643,7 @@ Node.prototype.setWatches = function()
var time = now - self._lastPendingLog; var time = now - self._lastPendingLog;
self._lastPendingLog = now; self._lastPendingLog = now;
console.log('>>> Pending Filter triggered: ', now, '- last trigger:', time); console.info('>>>', 'Pending Filter triggered', chalk.reset.cyan(now), '- last trigger:', chalk.reset.cyan(time));
if(time > 50) if(time > 50)
{ {
@ -668,11 +680,11 @@ Node.prototype.installContract = function()
Contract = web3.eth.contract( registrar.desc ); Contract = web3.eth.contract( registrar.desc );
this._Registrar = new Contract( registrar.address ); this._Registrar = new Contract( registrar.address );
console.log('==>', 'Installed Registrar contract in', _.now() - start, 'ms'); console.success('Installed Registrar contract in', chalk.reset.cyan(_.now() - start, 'ms'));
} }
catch (err) catch (err)
{ {
console.error("!!!", "Couldn't set up registrar contract"); console.error("!!!", "eth", "Couldn't set up registrar contract");
console.error(err); console.error(err);
} }
} }

120
lib/utils/logger.js Normal file
View File

@ -0,0 +1,120 @@
'use strict';
var util = require('util');
var chalk = require('chalk');
var signs = [
'==>',
'!!!',
'xx>',
'===',
'>>>',
'xxx'
];
var types = [
'eth',
'wsc',
'sys',
'his'
];
[
{
name: "info",
sign: '=i=',
signColor: chalk.blue,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "success",
inherit: 'log',
sign: '=s=',
signColor: chalk.green,
messageColor: chalk.bold.green,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "warn",
sign: '=!=',
signColor: chalk.yellow,
messageColor: chalk.bold.yellow,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "error",
sign: '=x=',
signColor: chalk.red,
messageColor: chalk.bold.red,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "time",
sign: '=T=',
signColor: chalk.cyan,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [util.format.apply(util, [sign, message])];
}
},
{
name: "timeEnd",
sign: '=T=',
signColor: chalk.cyan,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [util.format.apply(util, [sign, message])];
}
}
].forEach( function (item)
{
if(item.inherit !== undefined)
console[item.name] = console[item.inherit];
var fn = console[item.name];
console[item.name] = function ()
{
var args = Array.prototype.slice.call(arguments);
var sign = item.sign;
var section = 'eth';
var message = '';
if (signs.indexOf(args[0]) >= 0)
{
sign = args.splice(0, 1);
}
if (types.indexOf(args[0]) >= 0)
{
section = args.splice(0, 1);
}
sign = item.signColor( '[' + section + '] ' + sign );
if (typeof args[0] === 'object')
{
message = util.inspect( args[0], { depth: null, colors: true } );
}
else {
message = item.messageColor( util.format.apply(util, args) );
}
return fn.apply( this, item.formatter(sign, message) );
}
});