From 85a21ec4e8d18afce8dca48c449a585d606ea8fe Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Fri, 2 Jun 2017 21:38:55 +0530 Subject: [PATCH] Electron-65 (Logging) (#121) * Electron-65 - Added more logs * Electron-65 - Removed some logs as per the review * Electron-65 - Removed unused log variables --- js/badgeCount.js | 3 +++ js/config.js | 3 --- js/desktopCapturer/getSources.js | 1 + js/mainApiMgr.js | 5 ++--- js/menus/menuTemplate.js | 8 ++++++++ js/notify/AnimationQueue.js | 5 +++++ js/notify/electron-notify-preload.js | 11 ++++------- js/notify/electron-notify.js | 15 +++++---------- js/screenSnippet/ScreenSnippet.js | 9 +++------ js/utils/getCmdLineArg.js | 4 ++++ js/utils/getRegistry.js | 4 ++++ 11 files changed, 39 insertions(+), 29 deletions(-) diff --git a/js/badgeCount.js b/js/badgeCount.js index 0ee4264d..1f1cf62c 100644 --- a/js/badgeCount.js +++ b/js/badgeCount.js @@ -7,9 +7,12 @@ const nativeImage = electron.nativeImage; const { isMac } = require('./utils/misc.js'); const windowMgr = require('./windowMgr.js'); const maxCount = 1e8; +const log = require('./log.js'); +const logLevels = require('./enums/logLevels.js'); function show(count) { if (typeof count !== 'number') { + log.send(logLevels.WARN, 'badgeCount: invalid func arg, must be a number: ' + count); return; } diff --git a/js/config.js b/js/config.js index e2198b79..89554d52 100644 --- a/js/config.js +++ b/js/config.js @@ -8,8 +8,6 @@ const isDevEnv = require('./utils/misc.js').isDevEnv; const isMac = require('./utils/misc.js').isMac; const getRegistry = require('./utils/getRegistry.js'); const configFileName = 'Symphony.config'; -const log = require('./log.js'); -const logLevels = require('./enums/logLevels.js'); /** * Tries to read given field from user config file, if field doesn't exist @@ -158,7 +156,6 @@ function saveUserConfig(fieldName, newValue, oldConfig) { fs.writeFile(configPath, jsonNewConfig, 'utf8', (err) => { if (err) { - log.send(logLevels.ERROR, 'error saving to user config file: ' + configPath + ',error:' + err); reject(err); } else { resolve(newConfig); diff --git a/js/desktopCapturer/getSources.js b/js/desktopCapturer/getSources.js index 79c6b164..ac7fc336 100644 --- a/js/desktopCapturer/getSources.js +++ b/js/desktopCapturer/getSources.js @@ -14,6 +14,7 @@ var { ipcRenderer } = require('electron'); + var nextId = 0; var includes = [].includes; diff --git a/js/mainApiMgr.js b/js/mainApiMgr.js index 14811c89..42098a46 100644 --- a/js/mainApiMgr.js +++ b/js/mainApiMgr.js @@ -8,6 +8,7 @@ const electron = require('electron'); const windowMgr = require('./windowMgr.js'); const log = require('./log.js'); +const logLevels = require('./enums/logLevels'); const activityDetection = require('./activityDetection/activityDetection'); const badgeCount = require('./badgeCount.js'); const protocolHandler = require('./protocolHandler'); @@ -39,9 +40,7 @@ function isValidWindow(event) { } if (!result) { - /* eslint-disable no-console */ - console.log('invalid window try to perform action, ignoring action'); - /* eslint-enable no-console */ + log.send(logLevels.WARN, 'invalid window try to perform action, ignoring action'); } return result; diff --git a/js/menus/menuTemplate.js b/js/menus/menuTemplate.js index e6f331bd..ccf5c770 100644 --- a/js/menus/menuTemplate.js +++ b/js/menus/menuTemplate.js @@ -5,6 +5,8 @@ const { getConfigField, updateConfigField } = require('../config.js'); const AutoLaunch = require('auto-launch'); const isMac = require('../utils/misc.js').isMac; const childProcess = require('child_process'); +const log = require('../log.js'); +const logLevels = require('../enums/logLevels.js'); var minimizeOnClose = false; var launchOnStartup = false; @@ -192,6 +194,7 @@ function getTemplate(app) { childProcess.exec(`launchctl load ${launchAgentPath}`, (err) => { if (err){ let title = 'Error setting AutoLaunch configuration'; + log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': process error ' + err); electron.dialog.showErrorBox(title, 'Please try reinstalling the application'); } }); @@ -199,6 +202,7 @@ function getTemplate(app) { symphonyAutoLauncher.enable() .catch(function (err) { let title = 'Error setting AutoLaunch configuration'; + log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': auto launch error ' + err); electron.dialog.showErrorBox(title, title + ': ' + err); }); } @@ -209,6 +213,7 @@ function getTemplate(app) { childProcess.exec(`launchctl unload ${launchAgentPath}`, (err) => { if (err){ let title = 'Error disabling AutoLaunch configuration'; + log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': process error ' + err); electron.dialog.showErrorBox(title, 'Please try reinstalling the application'); } }); @@ -216,6 +221,7 @@ function getTemplate(app) { symphonyAutoLauncher.disable() .catch(function (err) { let title = 'Error setting AutoLaunch configuration'; + log.send(logLevels.ERROR, 'MenuTemplate: ' + title + ': auto launch error ' + err); electron.dialog.showErrorBox(title, title + ': ' + err); }); } @@ -259,6 +265,7 @@ function setCheckboxValues(){ minimizeOnClose = mClose; }).catch(function (err){ let title = 'Error loading configuration'; + log.send(logLevels.ERROR, 'MenuTemplate: error getting config field minimizeOnClose, error: ' + err); electron.dialog.showErrorBox(title, title + ': ' + err); }); @@ -266,6 +273,7 @@ function setCheckboxValues(){ launchOnStartup = lStartup; }).catch(function (err){ let title = 'Error loading configuration'; + log.send(logLevels.ERROR, 'MenuTemplate: error getting config field launchOnStartup, error: ' + err); electron.dialog.showErrorBox(title, title + ': ' + err); }); } diff --git a/js/notify/AnimationQueue.js b/js/notify/AnimationQueue.js index 49e6bbdc..abfa5169 100644 --- a/js/notify/AnimationQueue.js +++ b/js/notify/AnimationQueue.js @@ -1,5 +1,8 @@ 'use strict'; +const log = require('../log.js'); +const logLevels = require('../enums/logLevels.js'); + // One animation at a time const AnimationQueue = function(options) { this.options = options; @@ -27,6 +30,8 @@ AnimationQueue.prototype.animate = function(object) { } }.bind(this)) .catch(function(err) { + log.send(logLevels.ERROR, 'animationQueue: encountered an error: ' + err + + ' with stack trace:' + err.stack); /* eslint-disable no-console */ console.error('animation queue encountered an error: ' + err + ' with stack trace:' + err.stack); diff --git a/js/notify/electron-notify-preload.js b/js/notify/electron-notify-preload.js index 85dd31a1..dfa6bcf3 100644 --- a/js/notify/electron-notify-preload.js +++ b/js/notify/electron-notify-preload.js @@ -8,6 +8,8 @@ // const electron = require('electron'); const ipc = electron.ipcRenderer; +const log = require('../log.js'); +const logLevels = require('../enums/logLevels.js'); function setStyle(config) { // Style it @@ -53,7 +55,8 @@ function setContents(event, notificationObj) { audio.play() } } catch (e) { - log.send('electron-notify: ERROR could not find sound file: ' + notificationObj.sound.replace('file://', ''), e, e.stack); + log.send(logLevels.ERROR, 'electron-notify: ERROR could not find sound file: ' + + notificationObj.sound.replace('file://', ''), e, e.stack); } } @@ -140,9 +143,3 @@ function reset() { ipc.on('electron-notify-set-contents', setContents) ipc.on('electron-notify-load-config', loadConfig) ipc.on('electron-notify-reset', reset) - -function log() { - /* eslint-disable no-console */ - console.log.apply(console, arguments) - /* eslint-enable no-console */ -} diff --git a/js/notify/electron-notify.js b/js/notify/electron-notify.js index 30084adf..eabf5222 100644 --- a/js/notify/electron-notify.js +++ b/js/notify/electron-notify.js @@ -15,6 +15,8 @@ const electron = require('electron'); const app = electron.app; const BrowserWindow = electron.BrowserWindow; const ipc = electron.ipcMain; +const log = require('../log.js'); +const logLevels = require('../enums/logLevels.js'); // maximum number of notifications that can be queued, after limit is // reached then error func callback will be invoked. @@ -158,7 +160,7 @@ function getTemplatePath() { try { fs.statSync(templatePath).isFile(); } catch (err) { - log.send('electron-notify: Could not find template ("' + templatePath + '").'); + log.send(logLevels.ERROR, 'electron-notify: Could not find template ("' + templatePath + '").'); } config.templatePath = 'file://' + templatePath; return config.templatePath; @@ -254,7 +256,7 @@ function notify(notification) { }) return notf.id } - log.send('electron-notify: ERROR notify() only accepts a single object with notification parameters.') + log.send(logLevels.ERROR, 'electron-notify: ERROR notify() only accepts a single object with notification parameters.'); return null; } @@ -272,6 +274,7 @@ function showNotification(notificationObj) { id: notificationObj.id, error: 'max notification queue size reached: ' + MAX_QUEUE_SIZE }); + log.send(logLevels.INFO, 'showNotification: max notification queue size reached: ' + MAX_QUEUE_SIZE); }, 0); } resolve(); @@ -646,13 +649,5 @@ function cleanUpInactiveWindow() { inactiveWindows = []; } -function log() { - if (config.logging === true) { - /* eslint-disable no-console */ - console.log.apply(console, arguments); - /* eslint-enable no-console */ - } -} - module.exports.notify = notify module.exports.reset = setupConfig diff --git a/js/screenSnippet/ScreenSnippet.js b/js/screenSnippet/ScreenSnippet.js index da7e107a..64163d9c 100644 --- a/js/screenSnippet/ScreenSnippet.js +++ b/js/screenSnippet/ScreenSnippet.js @@ -36,7 +36,7 @@ class ScreenSnippet { return new Promise((resolve, reject) => { let captureUtil, captureUtilArgs; - log.send(logLevels.INFO, 'starting screen capture'); + log.send(logLevels.INFO, 'ScreenSnippet: starting screen capture'); let tmpFilename = 'symphonyImage-' + Date.now() + '.jpg'; let tmpDir = os.tmpdir(); @@ -64,7 +64,7 @@ class ScreenSnippet { captureUtilArgs = [ outputFileName ]; } - log.send(logLevels.INFO, 'starting screen capture util: ' + captureUtil + ' with args=' + captureUtilArgs); + log.send(logLevels.INFO, 'ScreenSnippet: starting screen capture util: ' + captureUtil + ' with args=' + captureUtilArgs); // only allow one screen capture at a time. if (child) { @@ -124,13 +124,10 @@ class ScreenSnippet { fs.unlink(outputFileName, function(removeErr) { // note: node complains if calling async // func without callback. - /* eslint-disable no-console */ if (removeErr) { - console.error( - 'error removing temp snippet file: ' + + log.send(logLevels.ERROR, 'ScreenSnippet: error removing temp snippet file: ' + outputFileName + ', err:' + removeErr); } - /* eslint-enable no-console */ }); } }); diff --git a/js/utils/getCmdLineArg.js b/js/utils/getCmdLineArg.js index f9440eb5..b557dac7 100644 --- a/js/utils/getCmdLineArg.js +++ b/js/utils/getCmdLineArg.js @@ -1,5 +1,8 @@ 'use strict'; +const log = require('../log.js'); +const logLevels = require('../enums/logLevels.js'); + /** * Search given argv for argName using exact match or starts with. * @param {Array} argv Array of strings @@ -10,6 +13,7 @@ */ function getCmdLineArg(argv, argName, exactMatch) { if (!Array.isArray(argv)) { + log.send(logLevels.WARN, 'getCmdLineArg: TypeError invalid func arg, must be an array: '+ argv); return null; } diff --git a/js/utils/getRegistry.js b/js/utils/getRegistry.js index b5010466..fdd9781a 100644 --- a/js/utils/getRegistry.js +++ b/js/utils/getRegistry.js @@ -2,6 +2,8 @@ const symphonyRegistry = '\\Software\\Symphony\\Symphony\\'; const { isMac } = require('./misc.js'); +const log = require('../log.js'); +const logLevels = require('../enums/logLevels.js'); var Registry = require('winreg'); var symphonyRegistryHKCU = new Registry({ @@ -32,6 +34,7 @@ var getRegistry = function (name) { //Try to get registry on HKEY_CURRENT_USER symphonyRegistryHKCU.get( name, function( err1, reg1 ) { if (!err1 && reg1 !==null && reg1.value) { + log.send(logLevels.WARN, 'getRegistry: Cannot find ' + name + ' Registry. Using HKCU'); resolve(reg1.value); return; } @@ -39,6 +42,7 @@ var getRegistry = function (name) { //Try to get registry on HKEY_LOCAL_MACHINE symphonyRegistryHKLM.get( name, function( err2, reg2 ) { if ( !err2 && reg2!==null && reg2.value) { + log.send(logLevels.WARN, 'getRegistry: Cannot find ' + name + ' Registry. Using HKLM'); resolve(reg2.value); return; }