1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/chrome/content/ConsoleAPI.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +"use strict"; 1.8 + 1.9 +var ConsoleAPI = { 1.10 + observe: function observe(aMessage, aTopic, aData) { 1.11 + aMessage = aMessage.wrappedJSObject; 1.12 + 1.13 + let mappedArguments = Array.map(aMessage.arguments, this.formatResult, this); 1.14 + let joinedArguments = Array.join(mappedArguments, " "); 1.15 + 1.16 + if (aMessage.level == "error" || aMessage.level == "warn") { 1.17 + let flag = (aMessage.level == "error" ? Ci.nsIScriptError.errorFlag : Ci.nsIScriptError.warningFlag); 1.18 + let consoleMsg = Cc["@mozilla.org/scripterror;1"].createInstance(Ci.nsIScriptError); 1.19 + consoleMsg.init(joinedArguments, null, null, 0, 0, flag, "content javascript"); 1.20 + Services.console.logMessage(consoleMsg); 1.21 + } else if (aMessage.level == "trace") { 1.22 + let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); 1.23 + let args = aMessage.arguments; 1.24 + let filename = this.abbreviateSourceURL(args[0].filename); 1.25 + let functionName = args[0].functionName || bundle.GetStringFromName("stacktrace.anonymousFunction"); 1.26 + let lineNumber = args[0].lineNumber; 1.27 + 1.28 + let body = bundle.formatStringFromName("stacktrace.outputMessage", [filename, functionName, lineNumber], 3); 1.29 + body += "\n"; 1.30 + args.forEach(function(aFrame) { 1.31 + let functionName = aFrame.functionName || bundle.GetStringFromName("stacktrace.anonymousFunction"); 1.32 + body += " " + aFrame.filename + " :: " + functionName + " :: " + aFrame.lineNumber + "\n"; 1.33 + }); 1.34 + 1.35 + Services.console.logStringMessage(body); 1.36 + } else if (aMessage.level == "time" && aMessage.arguments) { 1.37 + let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); 1.38 + let body = bundle.formatStringFromName("timer.start", [aMessage.arguments.name], 1); 1.39 + Services.console.logStringMessage(body); 1.40 + } else if (aMessage.level == "timeEnd" && aMessage.arguments) { 1.41 + let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); 1.42 + let body = bundle.formatStringFromName("timer.end", [aMessage.arguments.name, aMessage.arguments.duration], 2); 1.43 + Services.console.logStringMessage(body); 1.44 + } else if (["group", "groupCollapsed", "groupEnd"].indexOf(aMessage.level) != -1) { 1.45 + // Do nothing yet 1.46 + } else { 1.47 + Services.console.logStringMessage(joinedArguments); 1.48 + } 1.49 + }, 1.50 + 1.51 + getResultType: function getResultType(aResult) { 1.52 + let type = aResult === null ? "null" : typeof aResult; 1.53 + if (type == "object" && aResult.constructor && aResult.constructor.name) 1.54 + type = aResult.constructor.name; 1.55 + return type.toLowerCase(); 1.56 + }, 1.57 + 1.58 + formatResult: function formatResult(aResult) { 1.59 + let output = ""; 1.60 + let type = this.getResultType(aResult); 1.61 + switch (type) { 1.62 + case "string": 1.63 + case "boolean": 1.64 + case "date": 1.65 + case "error": 1.66 + case "number": 1.67 + case "regexp": 1.68 + output = aResult.toString(); 1.69 + break; 1.70 + case "null": 1.71 + case "undefined": 1.72 + output = type; 1.73 + break; 1.74 + default: 1.75 + output = aResult.toString(); 1.76 + break; 1.77 + } 1.78 + 1.79 + return output; 1.80 + }, 1.81 + 1.82 + abbreviateSourceURL: function abbreviateSourceURL(aSourceURL) { 1.83 + // Remove any query parameters. 1.84 + let hookIndex = aSourceURL.indexOf("?"); 1.85 + if (hookIndex > -1) 1.86 + aSourceURL = aSourceURL.substring(0, hookIndex); 1.87 + 1.88 + // Remove a trailing "/". 1.89 + if (aSourceURL[aSourceURL.length - 1] == "/") 1.90 + aSourceURL = aSourceURL.substring(0, aSourceURL.length - 1); 1.91 + 1.92 + // Remove all but the last path component. 1.93 + let slashIndex = aSourceURL.lastIndexOf("/"); 1.94 + if (slashIndex > -1) 1.95 + aSourceURL = aSourceURL.substring(slashIndex + 1); 1.96 + 1.97 + return aSourceURL; 1.98 + } 1.99 +};