Skip to content

Commit

Permalink
Add logging package and do first pass in adding to main.dev.js (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlyp authored Jul 25, 2017
1 parent 4f43279 commit f7f021f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 48 deletions.
79 changes: 32 additions & 47 deletions app/main.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
};
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
};
Expand All @@ -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);
}
}

Expand All @@ -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();');
}
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions somefile.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"level":"info","message":"This is an information message.","timestamp":"2017-07-24T21:04:52.956Z"}

0 comments on commit f7f021f

Please sign in to comment.