From 1126c9f211f3fca34f21fd249c7384fa78a4a0bf Mon Sep 17 00:00:00 2001 From: Piro / YUKI Hiroshi Date: Mon, 15 Feb 2016 23:45:58 +0900 Subject: [PATCH] Output log more safely #1067 --- modules/bookmark.js | 6 ++-- modules/browser.js | 2 +- modules/fullscreenObserver.js | 2 +- modules/utils.js | 62 ++++++++++++++++++++++++++++++++++- modules/window.js | 12 +++---- 5 files changed, 72 insertions(+), 12 deletions(-) diff --git a/modules/bookmark.js b/modules/bookmark.js index ecbf7a93..943d25cb 100644 --- a/modules/bookmark.js +++ b/modules/bookmark.js @@ -302,7 +302,7 @@ var TreeStyleTabBookmarksService = inherit(TreeStyleTabConstants, { let treeStructure = result.behavior & this.kGROUP_BOOKMARK_DONT_RESTORE_TREE_STRUCTURE ? null : this.getTreeStructureFromItems(aIDs) ; - log(' treeStructure => '+JSON.stringify(treeStructure)); + log(' treeStructure => ', treeStructure); if (treeStructure) { let parentTabs = treeStructure.filter(function(aParent) { return aParent < 0; @@ -340,7 +340,7 @@ var TreeStyleTabBookmarksService = inherit(TreeStyleTabConstants, { title: aFolderTitle, uri: uri }) - log(' updated treeStructure => '+JSON.stringify(treeStructure)); + log(' updated treeStructure => ', treeStructure); } } else if (!haveMultipleTrees) { @@ -428,7 +428,7 @@ PlacesUIUtils._openTabset = function(aItemsToOpen, aEvent, aWindow, ...aArgs) { return this.__treestyletab__openTabset.apply(this, allArgs); var result = BS.handleTabsOpenProcess(where, aEvent, w, ids, uris, aItemsToOpen, this.__treestyletab__folderName); - log(' result: '+JSON.stringify(result)); + log(' result: ', result); var tabs = TST.doAndGetNewTabs((function() { this.__treestyletab__openTabset.apply(this, allArgs); diff --git a/modules/browser.js b/modules/browser.js index 722456da..f24f8cc8 100644 --- a/modules/browser.js +++ b/modules/browser.js @@ -3436,7 +3436,7 @@ TreeStyleTabBrowser.prototype = inherit(TreeStyleTabWindow.prototype, { var closeParentBehavior = this.getCloseParentBehaviorForTab(tab); var backupAttributes = this._collectBackupAttributes(tab); - log('onTabClose: backupAttributes = '+JSON.stringify(backupAttributes)); + log('onTabClose: backupAttributes = ', backupAttributes); if (closeParentBehavior == this.kCLOSE_PARENT_BEHAVIOR_CLOSE_ALL_CHILDREN || this.isSubtreeCollapsed(tab)) diff --git a/modules/fullscreenObserver.js b/modules/fullscreenObserver.js index baace8a9..54210dc2 100644 --- a/modules/fullscreenObserver.js +++ b/modules/fullscreenObserver.js @@ -97,7 +97,7 @@ FullscreenObserver.prototype = { onSizeModeChange : function FullscreenObserver_onSizeModeChange() { - log('onSizeModeChange: '+this.window.document.documentElement.getAttribute('sizemode')); + log('onSizeModeChange: ', this.window.document.documentElement.getAttribute('sizemode')); this.updateToolboxPosition(); if (!this.window.gBrowser.treeStyleTab.notifyingRenderedEvent) this.window.gBrowser.treeStyleTab.updateFloatingTabbar(TreeStyleTabConstants.kTABBAR_UPDATE_BY_WINDOW_RESIZE); diff --git a/modules/utils.js b/modules/utils.js index 24d02348..368318bf 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -257,7 +257,7 @@ var TreeStyleTabUtils = { if (!this.isDebugging(aModule)) return; - var logString = '[treestyletab:' + aModule+'] '+ aArgs.join(', '); + var logString = '[treestyletab:' + aModule+'] '+ aArgs.map(this.objectToLogString, this).join(''); Services.console.logStringMessage(logString); dump(logString+'\n'); }, @@ -266,6 +266,66 @@ var TreeStyleTabUtils = { var stack = (new Error()).stack.replace(/^/gm, ' '); return this.log.apply(this, [aModule].concat(aArgs).concat([stack])); }, + objectToLogString : function utils_objectToLogString(aObject) + { + if (!aObject) + return JSON.stringify(aObject); + + if (/^(string|number|boolean)$/.test(typeof aObject)) + return aObject; + + return this.objectToString(aObject); + }, + objectToString : function utils_objectToString(aObject) + { + try { + if (!aObject || + /^(string|number|boolean)$/.test(typeof aObject)) + return JSON.stringify(aObject); + + if (Array.isArray(aObject)) + return '['+aObject.map(this.objectToString, this).join(', ')+']'; + + var constructor = String(aObject.constructor).match(/^function ([^\(]+)/); + if (constructor) { + constructor = constructor[1]; + switch (constructor) + { + case 'String': + case 'Number': + case 'Boolean': + return JSON.stringify(aObject); + + case 'Object': + return '{' + Object.keys(aObject).map(function(aKey) { + return '"' + aKey + '":' + this.objectToString(aObject[aKey]); + }, this).join(', ') + '}'; + + default: + break; + } + + if (/Element$/.test(constructor)) { + let id = ''; + if (aObject.hasAttribute('id')) + id = '#' + aObject.getAttribute('id'); + + let classes = ''; + if (aObject.className) + classes = '.' + aObject.className.replace(/\s+/g, '.'); + + return '<' + aObject.localName + id + classes + '>'; + } + + return ''; + } + + return String(aObject); + } + catch(e) { + return String(e); + } + }, /* string bundle */ get treeBundle () { diff --git a/modules/window.js b/modules/window.js index 7fc393e8..5624ac55 100644 --- a/modules/window.js +++ b/modules/window.js @@ -1385,7 +1385,7 @@ TreeStyleTabWindow.prototype = inherit(TreeStyleTabBase, { if (!aTab) return; - log('onBeforeOpenLinkWithTab '+[aTab, JSON.stringify(aParams), this.checkToOpenChildTab(aTab)]); + log('onBeforeOpenLinkWithTab: ', [aTab, aParams, this.checkToOpenChildTab(aTab)]); if (!this.checkToOpenChildTab(aTab)) { if (!aParams.fromChrome) @@ -1397,7 +1397,7 @@ TreeStyleTabWindow.prototype = inherit(TreeStyleTabBase, { onBeforeOpenNewTabByThirdParty : function TSTWindow_onBeforeOpenNewTabByThirdParty(aOwner) { - log('onBeforeOpenNewTabByThirdParty '+[aOwner, this.checkToOpenChildTab(aTab)]); + log('onBeforeOpenNewTabByThirdParty: ', [aOwner, this.checkToOpenChildTab(aTab)]); if (!this.checkToOpenChildTab(aOwner)) { this.handleNewTabFromCurrent(aOwner); @@ -1413,7 +1413,7 @@ TreeStyleTabWindow.prototype = inherit(TreeStyleTabBase, { log('onBeforeBrowserAccessOpenURI: opener is DOMWindow'); opener = aOpener; hasOwnerTab = this.getTabFromFrame(opener.top); - log(' opener =>'+[opener,hasOwnerTab]); + log(' opener =>', [opener, hasOwnerTab]); } else if (Ci.nsIOpenURIInFrameParams && aOpener instanceof Ci.nsIOpenURIInFrameParams) { @@ -1432,7 +1432,7 @@ TreeStyleTabWindow.prototype = inherit(TreeStyleTabBase, { break; } } - log(' opener =>'+[opener,hasOwnerTab]); + log(' opener =>', [opener, hasOwnerTab]); } } if (aOpener && @@ -1462,7 +1462,7 @@ TreeStyleTabWindow.prototype = inherit(TreeStyleTabBase, { { var where = String(this.window.whereToOpenLink(aEvent, false, true)); - log('onBeforeViewMedia '+[aEvent, aOwner, where]); + log('onBeforeViewMedia: ', [aEvent, aOwner, where]); if (where.indexOf('tab') == 0) this.handleNewTabFromCurrent(aOwner); @@ -1472,7 +1472,7 @@ TreeStyleTabWindow.prototype = inherit(TreeStyleTabBase, { onBeforeBrowserSearch : function TSTWindow_onBeforeBrowserSearch(aTerm, aForceNewTab) { - log('onBeforeBrowserSearch '+[aTerm, aForceNewTab, this.shouldOpenSearchResultAsChild(aTerm)]); + log('onBeforeBrowserSearch: ', [aTerm, aForceNewTab, this.shouldOpenSearchResultAsChild(aTerm)]); if ((arguments.length == 1 || aForceNewTab) && this.shouldOpenSearchResultAsChild(aTerm))