Bug 882552 - Change Binder Thread's default priority to 0. r=mwu, r=jlebar
[gecko.git] / toolkit / modules / debug.js
blob822ae9068e51b15374df1ceff1316e2a17320cf0
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 "beta":
44       case "default":
45         releaseBuild = false;
46     }
47   } catch(ex) {}
49   var caller = arguments.callee.caller;
50   var assertionText = "ASSERT: " + message + "\n";
52   if (releaseBuild) {
53     // Just report the error to the console
54     Components.utils.reportError(assertionText);
55     return;
56   }
58   // Otherwise, dump to stdout and launch an assertion failure dialog
59   dump(assertionText);
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   var environment = Components.classes["@mozilla.org/process/environment;1"].
79                     getService(Components.interfaces.nsIEnvironment);
80   if (environment.exists("XUL_ASSERT_PROMPT") &&
81       !parseInt(environment.get("XUL_ASSERT_PROMPT")))
82     return;
84   var source = null;
85   if (this.window)
86     source = this.window;
87   var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
88            getService(Components.interfaces.nsIPromptService);
89   ps.alert(source, "Assertion Failed", assertionText + stackText);