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