From a39da882b2ee0ccf5861cd6a9be12f9476f1eed1 Mon Sep 17 00:00:00 2001 From: Andrea Baccega Date: Tue, 14 Jan 2014 17:56:06 +0100 Subject: [PATCH] Added Sophisticated logging interface. Added package.json which contains also a dependency --- coins/dogecoin_example.json | 2 +- init.js | 44 ++++++++++++------- libs/logutils.js | 87 +++++++++++++++++++++++++++++++++++++ package.json | 30 +++++++++++++ 4 files changed, 146 insertions(+), 17 deletions(-) create mode 100644 libs/logutils.js create mode 100644 package.json diff --git a/coins/dogecoin_example.json b/coins/dogecoin_example.json index 8ef6b21..9485c8a 100644 --- a/coins/dogecoin_example.json +++ b/coins/dogecoin_example.json @@ -6,7 +6,7 @@ "address": "n3s8iDk1onxyY2nuC1k4HoRQFGJ7BhjFcq", "stratumPort": 3334, "difficulty": 8, - "blockRefreshInterval": 5, + "blockRefreshInterval": 1, "daemon": { "host": "localhost", "port": 19334, diff --git a/init.js b/init.js index 001f251..3a32291 100644 --- a/init.js +++ b/init.js @@ -1,21 +1,27 @@ -var fs = require('fs'); +var fs = require('fs'); +var dateFormat = require('dateformat'); +var Stratum = require('stratum-pool'); +var PoolLogger = require('./libs/logutils.js'); -var Stratum = require('stratum-pool'); +var loggerInstance = new PoolLogger({ + 'default': true, + 'keys': { + 'client' : 'warning', + 'system' : true, + 'submitblock' : true, + } +}); - - -var timeLog = function(text, poolName){ - var desc = poolName ? '[' + poolName + '] ' : ''; - var time = new Date().toISOString(); - console.log(time + ': ' + desc + text); -}; +var logDebug = loggerInstance.logDebug; +var logWarning = loggerInstance.logWarning; +var logError = loggerInstance.logError; var config = JSON.parse(fs.readFileSync("config.json")); var stratum = new Stratum(config); stratum.on('log', function(logText){ - timeLog(logText); + logDebug(logText); }); @@ -25,7 +31,7 @@ fs.readdirSync('coins').forEach(function(file){ var authorizeFN = function (ip, workerName, password, callback) { // Default implementation just returns true - timeLog(coinOptions.name, "Authorize ["+ip+"] "+workerName+":"+password); + logDebug(coinOptions.name, 'client', "Authorize ["+ip+"] "+workerName+":"+password); callback({ error: null, authorized: true, @@ -37,12 +43,18 @@ fs.readdirSync('coins').forEach(function(file){ var pool = stratum.createPool(coinOptions, authorizeFN); pool.on('share', function(isValid, data){ if (isValid) - timeLog(coinOptions.name, "A new Valid share from " + data.client.workerName + " has arrived! - " + data.headerHex); + logDebug(coinOptions.name, 'client', "A new Valid share from " + data.client.workerName + " has arrived! - " + data.blockHeaderHex); else - timeLog(coinOptions.name, "Invalid share form " + data.client.workerName + " ErrorCode: " + data.errorCode + " ErrorDescription: " + data.errorDescription); - }).on('log', function(logText){ - timeLog(coinOptions.name, logText); - }); + logDebug(coinOptions.name, 'client', "Invalid share form " + data.client.workerName + " ErrorCode: " + data.errorCode + " ErrorDescription: " + data.errorDescription); + }).on('log', function(severity, logKey, logText) { + if (severity == 'debug') { + logDebug(coinOptions.name, logKey, logText); + } else if (severity == 'warning') { + logWarning(coinOptions.name, logKey, logText); + } else if (severity == 'error') { + logError(coinOptions.name, logKey, logText); + } + }); }); diff --git a/libs/logutils.js b/libs/logutils.js new file mode 100644 index 0000000..68eeee9 --- /dev/null +++ b/libs/logutils.js @@ -0,0 +1,87 @@ +var dateFormat = require('dateformat'); +/* +var defaultConfiguration = { + 'default': true, + 'keys': { + 'client' : 'warning', + 'system' : true, + 'submitblock' : true, + } +}; +*/ + +var severityToInt = function(severity) { + switch(severity) { + case 'debug': + return 10; + case 'warning': + return 20; + case 'error': + return 30; + default: + console.log("Unknown severity "+severity); + return 1000; + } +} +var getSeverityColor = function(severity) { + switch(severity) { + case 'debug': + return 32; + case 'warning': + return 33; + case 'error': + return 31; + default: + console.log("Unknown severity "+severity); + return 31; + } +} + +var PoolLogger = function (configuration) { + + // privates + var shouldLog = function(key, severity) { + var keyValue = configuration.keys[key]; + if (typeof(keyValue) === 'undefined') { + keyValue = configuration.default; + } + + if (typeof(keyValue) === 'boolean') { + return keyValue; + } else if (typeof(keyValue) === 'string') { + return severityToInt(severity) >= severityToInt(keyValue); + } + } + + var log = function(severity, key, poolName, text) { + if ( ! shouldLog(key, severity) ) { + // if this tag is set to not be logged or the default value is false then drop it! + //console.log(key+"DROPPED "+text); + return; + + } + var desc = poolName ? '[' + poolName + '] ' : ''; + console.log( + '\u001b['+getSeverityColor(severity)+'m' + + dateFormat(new Date(), 'yyyy-mm-dd HH:mm:ss') + + " ["+key+"]" + '\u001b[39m: ' + "\t" + + desc + + text); + } + + // public + + this.logDebug = function(poolName, logKey, text){ + log('debug', logKey, poolName, text); + } + + this.logWarning = function(poolName, logKey, text) { + log('warning', logKey, poolName, text); + } + + this.logError = function(poolName, logKey, text) { + log('error', logKey, poolName, text); + } +} + +module.exports = PoolLogger; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..c75e08f --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "node-stratum-portal", + "version": "0.0.1", + "description": "Node quick start example portal", + "main": "init.js", + "dependencies": { + "stratum-pool": "~0.0.2", + "dateformat": "~1.0.7-1.2.3" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/zone117x/node-stratum-portal.git" + }, + "keywords": [ + "node", + "stratum", + "pool", + "mining" + ], + "author": "zone117x", + "license": "GPL", + "bugs": { + "url": "https://github.com/zone117x/node-stratum-portal/issues" + }, + "homepage": "https://github.com/zone117x/node-stratum-portal" +}