mobile/android/chrome/content/ConsoleAPI.js

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:b88a52ac2a94
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict";
5
6 var ConsoleAPI = {
7 observe: function observe(aMessage, aTopic, aData) {
8 aMessage = aMessage.wrappedJSObject;
9
10 let mappedArguments = Array.map(aMessage.arguments, this.formatResult, this);
11 let joinedArguments = Array.join(mappedArguments, " ");
12
13 if (aMessage.level == "error" || aMessage.level == "warn") {
14 let flag = (aMessage.level == "error" ? Ci.nsIScriptError.errorFlag : Ci.nsIScriptError.warningFlag);
15 let consoleMsg = Cc["@mozilla.org/scripterror;1"].createInstance(Ci.nsIScriptError);
16 consoleMsg.init(joinedArguments, null, null, 0, 0, flag, "content javascript");
17 Services.console.logMessage(consoleMsg);
18 } else if (aMessage.level == "trace") {
19 let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
20 let args = aMessage.arguments;
21 let filename = this.abbreviateSourceURL(args[0].filename);
22 let functionName = args[0].functionName || bundle.GetStringFromName("stacktrace.anonymousFunction");
23 let lineNumber = args[0].lineNumber;
24
25 let body = bundle.formatStringFromName("stacktrace.outputMessage", [filename, functionName, lineNumber], 3);
26 body += "\n";
27 args.forEach(function(aFrame) {
28 let functionName = aFrame.functionName || bundle.GetStringFromName("stacktrace.anonymousFunction");
29 body += " " + aFrame.filename + " :: " + functionName + " :: " + aFrame.lineNumber + "\n";
30 });
31
32 Services.console.logStringMessage(body);
33 } else if (aMessage.level == "time" && aMessage.arguments) {
34 let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
35 let body = bundle.formatStringFromName("timer.start", [aMessage.arguments.name], 1);
36 Services.console.logStringMessage(body);
37 } else if (aMessage.level == "timeEnd" && aMessage.arguments) {
38 let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
39 let body = bundle.formatStringFromName("timer.end", [aMessage.arguments.name, aMessage.arguments.duration], 2);
40 Services.console.logStringMessage(body);
41 } else if (["group", "groupCollapsed", "groupEnd"].indexOf(aMessage.level) != -1) {
42 // Do nothing yet
43 } else {
44 Services.console.logStringMessage(joinedArguments);
45 }
46 },
47
48 getResultType: function getResultType(aResult) {
49 let type = aResult === null ? "null" : typeof aResult;
50 if (type == "object" && aResult.constructor && aResult.constructor.name)
51 type = aResult.constructor.name;
52 return type.toLowerCase();
53 },
54
55 formatResult: function formatResult(aResult) {
56 let output = "";
57 let type = this.getResultType(aResult);
58 switch (type) {
59 case "string":
60 case "boolean":
61 case "date":
62 case "error":
63 case "number":
64 case "regexp":
65 output = aResult.toString();
66 break;
67 case "null":
68 case "undefined":
69 output = type;
70 break;
71 default:
72 output = aResult.toString();
73 break;
74 }
75
76 return output;
77 },
78
79 abbreviateSourceURL: function abbreviateSourceURL(aSourceURL) {
80 // Remove any query parameters.
81 let hookIndex = aSourceURL.indexOf("?");
82 if (hookIndex > -1)
83 aSourceURL = aSourceURL.substring(0, hookIndex);
84
85 // Remove a trailing "/".
86 if (aSourceURL[aSourceURL.length - 1] == "/")
87 aSourceURL = aSourceURL.substring(0, aSourceURL.length - 1);
88
89 // Remove all but the last path component.
90 let slashIndex = aSourceURL.lastIndexOf("/");
91 if (slashIndex > -1)
92 aSourceURL = aSourceURL.substring(slashIndex + 1);
93
94 return aSourceURL;
95 }
96 };

mercurial