From f7f021f6f36381dfb41c68b3772e1472c65931c1 Mon Sep 17 00:00:00 2001 From: Alex Yocom-Piatt Date: Tue, 25 Jul 2017 15:31:09 -0500 Subject: [PATCH] Add logging package and do first pass in adding to main.dev.js (#511) --- app/main.development.js | 79 +++++++++++++++++------------------------ package.json | 3 +- somefile.log | 1 + 3 files changed, 35 insertions(+), 48 deletions(-) create mode 100644 somefile.log diff --git a/app/main.development.js b/app/main.development.js index a70edaef3f..cb57db0a31 100644 --- a/app/main.development.js +++ b/app/main.development.js @@ -4,6 +4,7 @@ import path from 'path'; import os from 'os'; import parseArgs from 'minimist'; import mv from 'mv'; +import winston from 'winston'; let menu; let template; @@ -56,44 +57,44 @@ if (process.env.NODE_ENV === 'development') { app.setPath('userData', appDataDirectory()); var cfg = getCfg(); +var logger = new (winston.Logger)({ + transports: [ + new (winston.transports.File)({ json: false, filename: path.join(app.getPath('userData'),'decrediton.log') }) + ] +}); + if (debug) { - console.log('Using config/data from:', app.getPath('userData')); + logger.add(winston.transports.Console, {colorize: 'all'}); } +logger.log('info', 'Using config/data from:' + app.getPath('userData')); + // Check if network was set on command line (but only allow one!). if (argv.testnet && argv.mainnet) { - console.log('Cannot use both --testnet and --mainnet.'); + logger.log('Cannot use both --testnet and --mainnet.'); app.quit(); } if (argv.testnet) { cfg.set('network', 'testnet'); - if (debug) { - console.log('Running on testnet.'); - } + logger.log('info', 'Running on testnet.'); } if (argv.mainnet) { cfg.set('network', 'mainnet'); - if (debug) { - console.log('Running on mainnet.'); - } + logger.log('info', 'Running on mainnet.'); } function closeDCRW() { if (require('is-running')(dcrwPID)) { - if (debug) { - console.log('Sending SIGINT to dcrwallet at pid:', dcrwPID); - } + logger.log('info', 'Sending SIGINT to dcrwallet at pid:' + dcrwPID); process.kill(dcrwPID, 'SIGINT'); } } function closeDCRD() { if (require('is-running')(dcrdPID)) { - if (debug) { - console.log('Sending SIGINT to dcrd at pid:', dcrdPID); - } + logger.log('info', 'Sending SIGINT to dcrd at pid:' + dcrdPID); process.kill(dcrdPID, 'SIGINT'); } } @@ -114,9 +115,7 @@ function cleanShutdown() { // Sent shutdown message again as we have seen it missed in the past if they // are still running. setTimeout(function(){closeClis();}, cliShutDownPause*1000); - if (debug) { - console.log('Closing decrediton.'); - } + logger.log('info', 'Closing decrediton.'); setTimeout(function(){app.quit();}, shutDownPause*1000); } else { app.quit(); @@ -161,19 +160,16 @@ const launchDCRD = () => { args.push('--piperx=3'); } - if (debug) { - console.log(`Starting dcrd with ${args}`); - } + logger.log('info', `Starting dcrd with ${args}`); + var dcrd = spawn(dcrdExe, args, { detached: false, stdio: [ 'ignore', stdout, stderr, 'pipe' ] }); dcrd.on('error', function (err) { - console.log('error starting ' + dcrdExe + ': ' + path + err); + logger.log('error', 'error starting ' + dcrdExe + ': ' + path + err); }); dcrd.on('close', (code) => { - if (debug) { - console.log(`dcrd exited with code ${code}`); - } + logger.log('info', `dcrd exited with code ${code}`); }); if (debug) { @@ -187,9 +183,7 @@ const launchDCRD = () => { } dcrdPID = dcrd.pid; - if (debug) { - console.log('dcrd started with pid:', dcrdPID); - } + logger.log('info', 'dcrd started with pid:' + dcrdPID); dcrd.unref(); }; @@ -217,19 +211,16 @@ const launchDCRWallet = () => { } } - if (debug) { - console.log(`Starting dcrwallet with ${args}`); - } + logger.log('info', `Starting dcrwallet with ${args}`); + var dcrwallet = spawn(dcrwExe, args, { detached: false, stdio: [ 'ignore', stdout, stderr, 'ignore', 'pipe' ] }); dcrwallet.on('error', function (err) { - console.log('error starting ' + dcrwExe + ': ' + path + err); + logger.log('error', 'error starting ' + dcrwExe + ': ' + path + err); }); dcrwallet.on('close', (code) => { - if (debug) { - console.log(`dcrwallet exited with code ${code}`); - } + logger.log('info', `dcrwallet exited with code ${code}`); }); if (debug) { @@ -243,9 +234,7 @@ const launchDCRWallet = () => { } dcrwPID = dcrwallet.pid; - if (debug) { - console.log('dcrwallet started with pid:', dcrwPID); - } + logger.log('info', 'dcrwallet started with pid:' + dcrwPID); dcrwallet.unref(); }; @@ -259,12 +248,12 @@ app.on('ready', async () => { try { await launchDCRD(); } catch (e) { - console.log('error launching dcrd: ' + e); + logger.log('error', 'error launching dcrd: ' + e); } try { await launchDCRWallet(); } catch (e) { - console.log('error launching dcrwallet: ' + e); + logger.log('error', 'error launching dcrwallet: ' + e); } } @@ -280,16 +269,12 @@ app.on('ready', async () => { if (process.env.NODE_ENV === 'production') { // Check if daemon and wallet started up and error if not. if (!require('is-running')(dcrwPID)) { - if (debug) { - console.log('Error running dcrwallet. Check logs and restart!'); - } + logger.log('error', 'Error running dcrwallet. Check logs and restart!'); mainWindow.webContents.executeJavaScript('alert("Error running dcrwallet. Check logs and restart!");'); mainWindow.webContents.executeJavaScript('window.close();'); } if (!require('is-running')(dcrdPID)) { - if (debug) { - console.log('Error running dcrd. Check logs and restart!'); - } + logger.log('error', 'Error running dcrd. Check logs and restart!'); mainWindow.webContents.executeJavaScript('alert("Error running dcrd. Check logs and restart!");'); mainWindow.webContents.executeJavaScript('window.close();'); } @@ -447,14 +432,14 @@ app.on('ready', async () => { }, { label: 'Remove Wallet (Requires Restart)', click() { - console.log(getWalletFile()); + logger.log(getWalletFile()); closeDCRW(); var origFile = getWalletFile(); var date = new Date(); var backupFile = origFile + '-' + date.toISOString(); mv(origFile, backupFile, function(err) { if (err != undefined) { - console.log('Cannot remove file!', err); + logger.log('error', 'Cannot remove file!', err); } }); cleanShutdown(); diff --git a/package.json b/package.json index ac6728dc02..badc17a587 100644 --- a/package.json +++ b/package.json @@ -188,7 +188,8 @@ "react-select": "^1.0.0-rc.5", "redux": "^3.6.0", "redux-thunk": "^2.1.0", - "source-map-support": "^0.4.6" + "source-map-support": "^0.4.6", + "winston": "^2.3.1" }, "devEngines": { "node": ">=6.x", diff --git a/somefile.log b/somefile.log new file mode 100644 index 0000000000..4b8e183c5f --- /dev/null +++ b/somefile.log @@ -0,0 +1 @@ +{"level":"info","message":"This is an information message.","timestamp":"2017-07-24T21:04:52.956Z"}