Bumping manifests a=b2g-bump
[gecko.git] / toolkit / modules / debug.js
blobba59c657fa62c6a22a019dc677690eb87d0c0d43
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 "beta":
45       case "default":
46         releaseBuild = false;
47     }
48   } catch(ex) {}
50   var caller = arguments.callee.caller;
51   var assertionText = "ASSERT: " + message + "\n";
53   // Report the error to the console
54   Components.utils.reportError(assertionText);
56   if (releaseBuild) {
57     return;
58   }
60   // dump the stack to stdout too in non-release builds
61   var stackText = "";
62   if (gTraceOnAssert) {
63     stackText = "Stack Trace: \n";
64     var count = 0;
65     while (caller) {
66       stackText += count++ + ":" + caller.name + "(";
67       for (var i = 0; i < caller.arguments.length; ++i) {
68         var arg = caller.arguments[i];
69         stackText += arg;
70         if (i < caller.arguments.length - 1)
71           stackText += ",";
72       }
73       stackText += ")\n";
74       caller = caller.arguments.callee.caller;
75     }
76   }
78   dump(assertionText + stackText);