From 38921035770aa94a456e3491e46320129c82be44 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Tue, 12 Sep 2017 22:56:52 +0300 Subject: [PATCH] in mem app runtime log --- main.js | 1 + routes/cache.js | 60 +++++++++++++++++++++++----------------------- routes/shepherd.js | 13 ++++++++++ 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/main.js b/main.js index d580673..c6b0fd6 100644 --- a/main.js +++ b/main.js @@ -371,6 +371,7 @@ function createWindow(status) { mainWindow.iguanaIcon = iguanaIcon; mainWindow.testLocation = shepherd.testLocation; mainWindow.kmdMainPassiveMode = shepherd.kmdMainPassiveMode; + mainWindow.getAppRuntimeLog = shepherd.getAppRuntimeLog; if (appConfig.dev) { mainWindow.loadURL('http://127.0.0.1:3000'); diff --git a/routes/cache.js b/routes/cache.js index c01256c..379f920 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -15,7 +15,7 @@ cache.setVar = function(variable, value) { */ cache.dumpCacheBeforeExit = function() { if (inMemCache) { - shepherd.log('dumping cache before exit'); + cache.shepherd.log('dumping cache before exit'); fs.writeFileSync(`${cache.iguanaDir}/shepherd/cache-${inMemPubkey}.json`, JSON.stringify(inMemCache), 'utf8'); } } @@ -27,7 +27,7 @@ cache.get = function(req, res, next) { inMemPubkey = pubkey; if (!inMemCache) { - shepherd.log('serving cache from disk'); + cache.shepherd.log('serving cache from disk'); if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`)) { fs.readFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8', function(err, data) { @@ -49,13 +49,13 @@ cache.get = function(req, res, next) { inMemCache = parsedJSON; res.end(JSON.stringify(successObj)); } catch (e) { - shepherd.log('JSON parse error while reading cache data from disk:'); - shepherd.log(e); + cache.shepherd.log('JSON parse error while reading cache data from disk:'); + cache.shepherd.log(e); if (e.toString().indexOf('at position') > -1) { const errorPos = e.toString().split(' '); - shepherd.log(`JSON error ---> ${data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20)} | error sequence: ${data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)}`); - shepherd.log('attempting to recover JSON data'); + cache.shepherd.log(`JSON error ---> ${data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20)} | error sequence: ${data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)}`); + cache.shepherd.log('attempting to recover JSON data'); fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, data.substring(0, errorPos[errorPos.length - 1]), function(err) { const successObj = { @@ -195,8 +195,8 @@ cache.groomPost = function(req, res) { res.end(JSON.stringify(errorObj)); } else { inMemCache = JSON.parse(_payload); - shepherd.log('appending groom post to in mem cache'); - shepherd.log('appending groom post to on disk cache'); + cache.shepherd.log('appending groom post to in mem cache'); + cache.shepherd.log('appending groom post to on disk cache'); fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${_filename}.json`, _payload, function(err) { if (err) { @@ -326,8 +326,8 @@ cache.one = function(req, res, next) { inMemPubkey = pubkey; callStack[coin] = 1; - shepherd.log(callsArray); - shepherd.log(`iguana core port ${iguanaCorePort}`); + cache.shepherd.log(callsArray); + cache.shepherd.log(`iguana core port ${iguanaCorePort}`); if (!sessionKey) { const errorObj = { @@ -349,7 +349,7 @@ cache.one = function(req, res, next) { internalError = true; } - shepherd.log('cache-one call started'); + cache.shepherd.log('cache-one call started'); function fixJSON(data) { if (data && @@ -359,12 +359,12 @@ cache.one = function(req, res, next) { return parsedJSON; } catch (e) { - shepherd.log(e); + cache.shepherd.log(e); if (e.toString().indexOf('at position') > -1) { const errorPos = e.toString().split(' '); - shepherd.log(`JSON error ---> ${data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20)} | error sequence: ${data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)}`); - shepherd.log('attempting to recover JSON data'); + cache.shepherd.log(`JSON error ---> ${data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20)} | error sequence: ${data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)}`); + cache.shepherd.log('attempting to recover JSON data'); return JSON.parse(data.substring(0, errorPos[errorPos.length - 1])); } if (e.toString().indexOf('Unexpected end of JSON input')) { @@ -379,23 +379,23 @@ cache.one = function(req, res, next) { if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`) && coin !== 'all') { if (inMemCache) { - shepherd.log('cache one from mem'); + cache.shepherd.log('cache one from mem'); outObj = inMemCache; } else { const _file = fs.readFileSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8'); - shepherd.log('cache one from disk'); + cache.shepherd.log('cache one from disk'); outObj = fixJSON(_file); } if (!outObj || !outObj.basilisk) { - shepherd.log('no local basilisk info'); + cache.shepherd.log('no local basilisk info'); outObj['basilisk'] = {}; outObj['basilisk'][coin] = {}; } else { if (!outObj['basilisk'][coin]) { - shepherd.log('no local coin info'); + cache.shepherd.log('no local coin info'); outObj['basilisk'][coin] = {}; } } @@ -438,7 +438,7 @@ cache.one = function(req, res, next) { delete _dexUrls.getbalance; } - shepherd.log(`${coin} address ${address}`); + cache.shepherd.log(`${coin} address ${address}`); if (!outObj.basilisk[coin][address]) { outObj.basilisk[coin][address] = {}; @@ -524,7 +524,7 @@ cache.one = function(req, res, next) { if (key === 'getbalance' && coin === 'KMD'/* && ((_parsedJSON && _parsedJSON.balance === 0) || body === [])*/) { - shepherd.log('fallback to kmd explorer ======>'); + cache.shepherd.log('fallback to kmd explorer ======>'); request({ url: `http://kmd.explorer.supernet.org/api/addr/${address}/?noTxList=1`, method: 'GET' @@ -558,10 +558,10 @@ cache.one = function(req, res, next) { outObj.basilisk[coin][address][key].data = JSON.parse(body); outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp outObj.basilisk[coin][address][key].status = 'done'; - shepherd.log(dexUrl); - shepherd.log(body); + cache.shepherd.log(dexUrl); + cache.shepherd.log(body); callStack[coin]--; - shepherd.log(`${coin} _stack len ${callStack[coin]}`); + cache.shepherd.log(`${coin} _stack len ${callStack[coin]}`); cache.io.emit('messages', { message: { shepherd: { @@ -585,7 +585,7 @@ cache.one = function(req, res, next) { outObj.basilisk[coin][address][key].timestamp = 1471620867 // add timestamp outObj.basilisk[coin][address][key].status = 'done'; callStack[coin]--; - shepherd.log(`${coin} _stack len ${callStack[coin]}`); + cache.shepherd.log(`${coin} _stack len ${callStack[coin]}`); cache.io.emit('messages', { message: { shepherd: { @@ -602,9 +602,9 @@ cache.one = function(req, res, next) { } }); } else { - shepherd.log(`${key} is fresh, check back in ${(cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp))}s`); + cache.shepherd.log(`${key} is fresh, check back in ${(cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp))}s`); callStack[coin]--; - shepherd.log(`${coin} _stack len ${callStack[coin]}`); + cache.shepherd.log(`${coin} _stack len ${callStack[coin]}`); cache.io.emit('messages', { message: { shepherd: { @@ -637,7 +637,7 @@ cache.one = function(req, res, next) { }, }); outObj.basilisk[coin].addresses = addrArray; - shepherd.log(addrArray); + cache.shepherd.log(addrArray); writeCache(); const addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; @@ -650,7 +650,7 @@ cache.one = function(req, res, next) { callsArrayBTC--; } callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length); - shepherd.log(`${coin} stack len ${callStack[coin]}`); + cache.shepherd.log(`${coin} stack len ${callStack[coin]}`); cache.io.emit('messages', { message: { @@ -713,7 +713,7 @@ cache.one = function(req, res, next) { if (response && response.statusCode && response.statusCode === 200) { - shepherd.log(JSON.parse(body).basilisk); + cache.shepherd.log(JSON.parse(body).basilisk); cache.io.emit('messages', { message: { shepherd: { @@ -777,7 +777,7 @@ cache.one = function(req, res, next) { } callStack[coin] = callStack[coin] + (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length); - shepherd.log(`${coin} stack len ${callStack[coin]}`); + cache.shepherd.log(`${coin} stack len ${callStack[coin]}`); cache.io.emit('messages', { message: { diff --git a/routes/shepherd.js b/routes/shepherd.js index 939ef3d..683f9d7 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -36,6 +36,7 @@ var ps = require('ps-node'), syncOnlyInstanceInterval = -1, guiLog = {}, rpcConf = {}, + appRuntimeLog = [], lockDownAddCoin = false; shepherd.appConfig = _appConfig.config; @@ -99,10 +100,21 @@ shepherd.kmdMainPassiveMode = false; shepherd.coindInstanceRegistry = coindInstanceRegistry; +shepherd.getAppRuntimeLog = function() { + return new Promise((resolve, reject) => { + resolve(appRuntimeLog); + }); +}; + shepherd.log = function(msg) { if (shepherd.appConfig.dev) { console.log(msg); } + + appRuntimeLog.push({ + time: Date.now(), + msg: msg, + }); }; shepherd.startKMDNative = function(selection, isManual) { @@ -1937,6 +1949,7 @@ var mock = require('./mock'); shepherd.setIO = function(io) { shepherd.io = io; cache.setVar('io', io); + cache.setVar('shepherd', shepherd); }; shepherd.setVar = function(_name, _body) {