browser/components/downloads/src/DownloadsLogger.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/downloads/src/DownloadsLogger.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
     1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * The contents of this file were copied almost entirely from
    1.12 + * toolkit/identity/LogUtils.jsm. Until we've got a more generalized logging
    1.13 + * mechanism for toolkit, I think this is going to be how we roll.
    1.14 + */
    1.15 +
    1.16 +"use strict";
    1.17 +
    1.18 +this.EXPORTED_SYMBOLS = ["DownloadsLogger"];
    1.19 +const PREF_DEBUG = "browser.download.debug";
    1.20 +
    1.21 +const Cu = Components.utils;
    1.22 +const Ci = Components.interfaces;
    1.23 +const Cc = Components.classes;
    1.24 +const Cr = Components.results;
    1.25 +
    1.26 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.27 +Cu.import("resource://gre/modules/Services.jsm");
    1.28 +
    1.29 +this.DownloadsLogger = {
    1.30 +  _generateLogMessage: function _generateLogMessage(args) {
    1.31 +    // create a string representation of a list of arbitrary things
    1.32 +    let strings = [];
    1.33 +
    1.34 +    for (let arg of args) {
    1.35 +      if (typeof arg === 'string') {
    1.36 +        strings.push(arg);
    1.37 +      } else if (arg === undefined) {
    1.38 +        strings.push('undefined');
    1.39 +      } else if (arg === null) {
    1.40 +        strings.push('null');
    1.41 +      } else {
    1.42 +        try {
    1.43 +          strings.push(JSON.stringify(arg, null, 2));
    1.44 +        } catch(err) {
    1.45 +          strings.push("<<something>>");
    1.46 +        }
    1.47 +      }
    1.48 +    };
    1.49 +    return 'Downloads: ' + strings.join(' ');
    1.50 +  },
    1.51 +
    1.52 +  /**
    1.53 +   * log() - utility function to print a list of arbitrary things
    1.54 +   *
    1.55 +   * Enable with about:config pref browser.download.debug
    1.56 +   */
    1.57 +  log: function DL_log(...args) {
    1.58 +    let output = this._generateLogMessage(args);
    1.59 +    dump(output + "\n");
    1.60 +
    1.61 +    // Additionally, make the output visible in the Error Console
    1.62 +    Services.console.logStringMessage(output);
    1.63 +  },
    1.64 +
    1.65 +  /**
    1.66 +   * reportError() - report an error through component utils as well as
    1.67 +   * our log function
    1.68 +   */
    1.69 +  reportError: function DL_reportError(...aArgs) {
    1.70 +    // Report the error in the browser
    1.71 +    let output = this._generateLogMessage(aArgs);
    1.72 +    Cu.reportError(output);
    1.73 +    dump("ERROR:" + output + "\n");
    1.74 +    for (let frame = Components.stack.caller; frame; frame = frame.caller) {
    1.75 +      dump("\t" + frame + "\n");
    1.76 +    }
    1.77 +  }
    1.78 +
    1.79 +};

mercurial