michael@0: /* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * The contents of this file were copied almost entirely from michael@0: * toolkit/identity/LogUtils.jsm. Until we've got a more generalized logging michael@0: * mechanism for toolkit, I think this is going to be how we roll. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["DownloadsLogger"]; michael@0: const PREF_DEBUG = "browser.download.debug"; michael@0: michael@0: const Cu = Components.utils; michael@0: const Ci = Components.interfaces; michael@0: const Cc = Components.classes; michael@0: const Cr = Components.results; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: this.DownloadsLogger = { michael@0: _generateLogMessage: function _generateLogMessage(args) { michael@0: // create a string representation of a list of arbitrary things michael@0: let strings = []; michael@0: michael@0: for (let arg of args) { michael@0: if (typeof arg === 'string') { michael@0: strings.push(arg); michael@0: } else if (arg === undefined) { michael@0: strings.push('undefined'); michael@0: } else if (arg === null) { michael@0: strings.push('null'); michael@0: } else { michael@0: try { michael@0: strings.push(JSON.stringify(arg, null, 2)); michael@0: } catch(err) { michael@0: strings.push("<>"); michael@0: } michael@0: } michael@0: }; michael@0: return 'Downloads: ' + strings.join(' '); michael@0: }, michael@0: michael@0: /** michael@0: * log() - utility function to print a list of arbitrary things michael@0: * michael@0: * Enable with about:config pref browser.download.debug michael@0: */ michael@0: log: function DL_log(...args) { michael@0: let output = this._generateLogMessage(args); michael@0: dump(output + "\n"); michael@0: michael@0: // Additionally, make the output visible in the Error Console michael@0: Services.console.logStringMessage(output); michael@0: }, michael@0: michael@0: /** michael@0: * reportError() - report an error through component utils as well as michael@0: * our log function michael@0: */ michael@0: reportError: function DL_reportError(...aArgs) { michael@0: // Report the error in the browser michael@0: let output = this._generateLogMessage(aArgs); michael@0: Cu.reportError(output); michael@0: dump("ERROR:" + output + "\n"); michael@0: for (let frame = Components.stack.caller; frame; frame = frame.caller) { michael@0: dump("\t" + frame + "\n"); michael@0: } michael@0: } michael@0: michael@0: };