toolkit/modules/debug.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* vim:set ts=2 sw=2 sts=2 ci et: */
     2 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     4 /* This Source Code Form is subject to the terms of the Mozilla Public
     5  * License, v. 2.0. If a copy of the MPL was not distributed with this
     6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     8 // This file contains functions that are useful for debugging purposes from
     9 // within JavaScript code.
    11 this.EXPORTED_SYMBOLS = ["NS_ASSERT"];
    13 var gTraceOnAssert = true;
    15 /**
    16  * This function provides a simple assertion function for JavaScript.
    17  * If the condition is true, this function will do nothing.  If the
    18  * condition is false, then the message will be printed to the console
    19  * and an alert will appear showing a stack trace, so that the (alpha
    20  * or nightly) user can file a bug containing it.  For future enhancements, 
    21  * see bugs 330077 and 330078.
    22  *
    23  * To suppress the dialogs, you can run with the environment variable
    24  * XUL_ASSERT_PROMPT set to 0 (if unset, this defaults to 1).
    25  *
    26  * @param condition represents the condition that we're asserting to be
    27  *                  true when we call this function--should be
    28  *                  something that can be evaluated as a boolean.
    29  * @param message   a string to be displayed upon failure of the assertion
    30  */
    32 this.NS_ASSERT = function NS_ASSERT(condition, message) {
    33   if (condition)
    34     return;
    36   var releaseBuild = true;
    37   var defB = Components.classes["@mozilla.org/preferences-service;1"]
    38                        .getService(Components.interfaces.nsIPrefService)
    39                        .getDefaultBranch(null);
    40   try {
    41     switch (defB.getCharPref("app.update.channel")) {
    42       case "nightly":
    43       case "aurora":
    44       case "alpha":
    45       case "beta":
    46       case "default":
    47         releaseBuild = false;
    48     }
    49   } catch(ex) {}
    51   var caller = arguments.callee.caller;
    52   var assertionText = "ASSERT: " + message + "\n";
    54   // Report the error to the console
    55   Components.utils.reportError(assertionText);
    57   if (releaseBuild) {
    58     return;
    59   }
    61   // dump the stack to stdout too in non-release builds
    62   var stackText = "";
    63   if (gTraceOnAssert) {
    64     stackText = "Stack Trace: \n";
    65     var count = 0;
    66     while (caller) {
    67       stackText += count++ + ":" + caller.name + "(";
    68       for (var i = 0; i < caller.arguments.length; ++i) {
    69         var arg = caller.arguments[i];
    70         stackText += arg;
    71         if (i < caller.arguments.length - 1)
    72           stackText += ",";
    73       }
    74       stackText += ")\n";
    75       caller = caller.arguments.callee.caller;
    76     }
    77   }
    79   dump(assertionText + stackText);
    80 }

mercurial