1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/internal/AddonLogging.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,180 @@ 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 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const Cc = Components.classes; 1.11 +const Ci = Components.interfaces; 1.12 +const Cr = Components.results; 1.13 + 1.14 +const KEY_PROFILEDIR = "ProfD"; 1.15 +const FILE_EXTENSIONS_LOG = "extensions.log"; 1.16 +const PREF_LOGGING_ENABLED = "extensions.logging.enabled"; 1.17 + 1.18 +const LOGGER_FILE_PERM = parseInt("666", 8); 1.19 + 1.20 +const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed"; 1.21 + 1.22 +Components.utils.import("resource://gre/modules/FileUtils.jsm"); 1.23 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.24 + 1.25 +this.EXPORTED_SYMBOLS = [ "LogManager" ]; 1.26 + 1.27 +var gDebugLogEnabled = false; 1.28 + 1.29 +function formatLogMessage(aType, aName, aStr, aException) { 1.30 + let message = aType.toUpperCase() + " " + aName + ": " + aStr; 1.31 + if (aException) { 1.32 + if (typeof aException == "number") 1.33 + return message + ": " + Components.Exception("", aException).name; 1.34 + 1.35 + message = message + ": " + aException; 1.36 + // instanceOf doesn't work here, let's duck type 1.37 + if (aException.fileName) 1.38 + message = message + " (" + aException.fileName + ":" + aException.lineNumber + ")"; 1.39 + 1.40 + if (aException.message == "too much recursion") 1.41 + dump(message + "\n" + aException.stack + "\n"); 1.42 + } 1.43 + return message; 1.44 +} 1.45 + 1.46 +function getStackDetails(aException) { 1.47 + // Defensively wrap all this to ensure that failing to get the message source 1.48 + // doesn't stop the message from being logged 1.49 + try { 1.50 + if (aException) { 1.51 + if (aException instanceof Ci.nsIException) { 1.52 + return { 1.53 + sourceName: aException.filename, 1.54 + lineNumber: aException.lineNumber 1.55 + }; 1.56 + } 1.57 + 1.58 + if (typeof aException == "object") { 1.59 + return { 1.60 + sourceName: aException.fileName, 1.61 + lineNumber: aException.lineNumber 1.62 + }; 1.63 + } 1.64 + } 1.65 + 1.66 + let stackFrame = Components.stack.caller.caller.caller; 1.67 + return { 1.68 + sourceName: stackFrame.filename, 1.69 + lineNumber: stackFrame.lineNumber 1.70 + }; 1.71 + } 1.72 + catch (e) { 1.73 + return { 1.74 + sourceName: null, 1.75 + lineNumber: 0 1.76 + }; 1.77 + } 1.78 +} 1.79 + 1.80 +function AddonLogger(aName) { 1.81 + this.name = aName; 1.82 +} 1.83 + 1.84 +AddonLogger.prototype = { 1.85 + name: null, 1.86 + 1.87 + error: function AddonLogger_error(aStr, aException) { 1.88 + let message = formatLogMessage("error", this.name, aStr, aException); 1.89 + 1.90 + let stack = getStackDetails(aException); 1.91 + 1.92 + let consoleMessage = Cc["@mozilla.org/scripterror;1"]. 1.93 + createInstance(Ci.nsIScriptError); 1.94 + consoleMessage.init(message, stack.sourceName, null, stack.lineNumber, 0, 1.95 + Ci.nsIScriptError.errorFlag, "component javascript"); 1.96 + Services.console.logMessage(consoleMessage); 1.97 + 1.98 + // Always dump errors, in case the Console Service isn't listening yet 1.99 + dump("*** " + message + "\n"); 1.100 + 1.101 + try { 1.102 + var tstamp = new Date(); 1.103 + var logfile = FileUtils.getFile(KEY_PROFILEDIR, [FILE_EXTENSIONS_LOG]); 1.104 + var stream = Cc["@mozilla.org/network/file-output-stream;1"]. 1.105 + createInstance(Ci.nsIFileOutputStream); 1.106 + stream.init(logfile, 0x02 | 0x08 | 0x10, LOGGER_FILE_PERM, 0); // write, create, append 1.107 + var writer = Cc["@mozilla.org/intl/converter-output-stream;1"]. 1.108 + createInstance(Ci.nsIConverterOutputStream); 1.109 + writer.init(stream, "UTF-8", 0, 0x0000); 1.110 + writer.writeString(tstamp.toLocaleFormat("%Y-%m-%d %H:%M:%S ") + 1.111 + message + " at " + stack.sourceName + ":" + 1.112 + stack.lineNumber + "\n"); 1.113 + writer.close(); 1.114 + } 1.115 + catch (e) { } 1.116 + }, 1.117 + 1.118 + warn: function AddonLogger_warn(aStr, aException) { 1.119 + let message = formatLogMessage("warn", this.name, aStr, aException); 1.120 + 1.121 + let stack = getStackDetails(aException); 1.122 + 1.123 + let consoleMessage = Cc["@mozilla.org/scripterror;1"]. 1.124 + createInstance(Ci.nsIScriptError); 1.125 + consoleMessage.init(message, stack.sourceName, null, stack.lineNumber, 0, 1.126 + Ci.nsIScriptError.warningFlag, "component javascript"); 1.127 + Services.console.logMessage(consoleMessage); 1.128 + 1.129 + if (gDebugLogEnabled) 1.130 + dump("*** " + message + "\n"); 1.131 + }, 1.132 + 1.133 + log: function AddonLogger_log(aStr, aException) { 1.134 + if (gDebugLogEnabled) { 1.135 + let message = formatLogMessage("log", this.name, aStr, aException); 1.136 + dump("*** " + message + "\n"); 1.137 + Services.console.logStringMessage(message); 1.138 + } 1.139 + } 1.140 +}; 1.141 + 1.142 +this.LogManager = { 1.143 + getLogger: function LogManager_getLogger(aName, aTarget) { 1.144 + let logger = new AddonLogger(aName); 1.145 + 1.146 + if (aTarget) { 1.147 + ["error", "warn", "log"].forEach(function(name) { 1.148 + let fname = name.toUpperCase(); 1.149 + delete aTarget[fname]; 1.150 + aTarget[fname] = function LogManager_targetName(aStr, aException) { 1.151 + logger[name](aStr, aException); 1.152 + }; 1.153 + }); 1.154 + } 1.155 + 1.156 + return logger; 1.157 + } 1.158 +}; 1.159 + 1.160 +var PrefObserver = { 1.161 + init: function PrefObserver_init() { 1.162 + Services.prefs.addObserver(PREF_LOGGING_ENABLED, this, false); 1.163 + Services.obs.addObserver(this, "xpcom-shutdown", false); 1.164 + this.observe(null, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, PREF_LOGGING_ENABLED); 1.165 + }, 1.166 + 1.167 + observe: function PrefObserver_observe(aSubject, aTopic, aData) { 1.168 + if (aTopic == "xpcom-shutdown") { 1.169 + Services.prefs.removeObserver(PREF_LOGGING_ENABLED, this); 1.170 + Services.obs.removeObserver(this, "xpcom-shutdown"); 1.171 + } 1.172 + else if (aTopic == NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) { 1.173 + try { 1.174 + gDebugLogEnabled = Services.prefs.getBoolPref(PREF_LOGGING_ENABLED); 1.175 + } 1.176 + catch (e) { 1.177 + gDebugLogEnabled = false; 1.178 + } 1.179 + } 1.180 + } 1.181 +}; 1.182 + 1.183 +PrefObserver.init();