From c30855c8e3ec37471995aa0fd5ec5c727a3dbe11 Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Mon, 22 May 2017 18:38:22 +0200 Subject: [PATCH] gui logger wip --- main.js | 8 ++++++-- routes/shepherd.js | 45 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/main.js b/main.js index 3175a01..1e62e4a 100644 --- a/main.js +++ b/main.js @@ -7,6 +7,7 @@ const electron = require('electron'), path = require('path'), url = require('url'), os = require('os'), + md5 = require('md5'), spawn = require('child_process').spawn, exec = require('child_process').exec, { Menu } = require('electron'), @@ -28,7 +29,7 @@ Promise = require('bluebird'); const appBasicInfo = { name: 'Agama', - version: '0.1.6.2e-beta' + version: '0.1.7.77a-beta' }; app.setName(appBasicInfo.name); @@ -45,7 +46,9 @@ var shepherd = require('./routes/shepherd'), shepherd.createIguanaDirs(); -shepherd.writeLog('app init'); +const appSessionHash = md5(new Date(Date.now()).toLocaleString); + +shepherd.writeLog('app init ' + appSessionHash); shepherd.writeLog('app info: ' + appBasicInfo.name + ' ' + appBasicInfo.version); shepherd.writeLog('sys info:'); shepherd.writeLog('totalmem_readable: ' + formatBytes(os.totalmem())); @@ -170,6 +173,7 @@ io.on('connection', function(client) { shepherd.setIO(io); // pass sockets object to shepherd router shepherd.setVar('appBasicInfo', appBasicInfo); +shepherd.setVar('appSessionHash', appSessionHash); module.exports = guiapp; // END GUI App Settings diff --git a/routes/shepherd.js b/routes/shepherd.js index 0ae6ad4..8d6fb63 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -27,7 +27,8 @@ var ps = require('ps-node'), shepherd = express.Router(), iguanaInstanceRegistry = {}, syncOnlyIguanaInstanceInfo = {}, - syncOnlyInstanceInterval = -1; + syncOnlyInstanceInterval = -1, + guiLog = {}; // IGUANA FILES AND CONFIG SETTINGS var iguanaConfsDirSrc = path.join(__dirname, '../assets/deps/confs'), @@ -100,7 +101,7 @@ shepherd.appConfig = { shepherd.writeLog = function(data) { const logLocation = iguanaDir + '/shepherd'; - const timeFormatted = new Date(Date.now()).toLocaleString().replace('AM', '').replace('PM', ''); + const timeFormatted = new Date(Date.now()).toLocaleString('en-US', { hour12: false }); if (fs.existsSync(logLocation + '/agamalog.txt')) { fs.appendFile(logLocation + '/agamalog.txt', timeFormatted + ' ' + data + '\r\n', function (err) { @@ -168,6 +169,44 @@ shepherd.get('/coinslist', function(req, res, next) { } }); +/* + * type: POST + * params: payload + */ +shepherd.post('/guilog', function(req, res, next) { + const logLocation = iguanaDir + '/shepherd'; + + if (!guiLog[shepherd.appSessionHash]) { + guiLog[shepherd.appSessionHash] = {}; + } + + if (guiLog[shepherd.appSessionHash][req.body.timestamp]) { + guiLog[shepherd.appSessionHash][req.body.timestamp].status = req.body.status; + guiLog[shepherd.appSessionHash][req.body.timestamp].response = req.body.response; + } else { + guiLog[shepherd.appSessionHash][req.body.timestamp] = { + 'function': req.body.function, + 'type': req.body.type, + 'url': req.body.url, + 'payload': req.body.payload, + 'status': req.body.status, + }; + } + + fs.writeFile(logLocation + '/agamalog.json', JSON.stringify(guiLog), function (err) { + if (err) { + shepherd.writeLog('error writing gui log file'); + } + + const returnObj = { + 'msg': 'success', + 'result': 'gui log entry is added' + }; + + res.end(JSON.stringify(returnObj)); + }); +}); + shepherd.post('/coinslist', function(req, res, next) { const _payload = req.body.payload; @@ -206,6 +245,7 @@ shepherd.quitKomodod = function(chain) { exec(komodocliBin + (chain ? ' ac_name=' + chain : '') + ' stop', function(error, stdout, stderr) { console.log('stdout: ' + stdout) console.log('stderr: ' + stderr) + if (error !== null) { console.log('exec error: ' + error) } @@ -1587,6 +1627,7 @@ shepherd.appInfo = function() { sysInfo, releaseInfo, dirs, + appSession: shepherd.appSessionHash }; }