michael@0: /** michael@0: * MozillaLogger, a base class logger that just logs to stdout. michael@0: */ michael@0: michael@0: function MozillaLogger(aPath) { michael@0: } michael@0: michael@0: MozillaLogger.prototype = { michael@0: michael@0: init : function(path) {}, michael@0: michael@0: getLogCallback : function() { michael@0: return function (msg) { michael@0: var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; michael@0: dump(data); michael@0: } michael@0: }, michael@0: michael@0: log : function(msg) { michael@0: dump(msg); michael@0: }, michael@0: michael@0: close : function() {} michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * SpecialPowersLogger, inherits from MozillaLogger and utilizes SpecialPowers. michael@0: * intented to be used in content scripts to write to a file michael@0: */ michael@0: function SpecialPowersLogger(aPath) { michael@0: // Call the base constructor michael@0: MozillaLogger.call(this); michael@0: this.prototype = new MozillaLogger(aPath); michael@0: this.init(aPath); michael@0: } michael@0: michael@0: SpecialPowersLogger.prototype = { michael@0: init : function (path) { michael@0: SpecialPowers.setLogFile(path); michael@0: }, michael@0: michael@0: getLogCallback : function () { michael@0: return function (msg) { michael@0: var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; michael@0: SpecialPowers.log(data); michael@0: michael@0: if (data.indexOf("SimpleTest FINISH") >= 0) { michael@0: SpecialPowers.closeLogFile(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: log : function (msg) { michael@0: SpecialPowers.log(msg); michael@0: }, michael@0: michael@0: close : function () { michael@0: SpecialPowers.closeLogFile(); michael@0: } michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * MozillaFileLogger, a log listener that can write to a local file. michael@0: * intended to be run from chrome space michael@0: */ michael@0: michael@0: /** Init the file logger with the absolute path to the file. michael@0: It will create and append if the file already exists **/ michael@0: function MozillaFileLogger(aPath) { michael@0: // Call the base constructor michael@0: MozillaLogger.call(this); michael@0: this.prototype = new MozillaLogger(aPath); michael@0: this.init(aPath); michael@0: } michael@0: michael@0: MozillaFileLogger.prototype = { michael@0: michael@0: init : function (path) { michael@0: var PR_WRITE_ONLY = 0x02; // Open for writing only. michael@0: var PR_CREATE_FILE = 0x08; michael@0: var PR_APPEND = 0x10; michael@0: this._file = Components.classes["@mozilla.org/file/local;1"]. michael@0: createInstance(Components.interfaces.nsILocalFile); michael@0: this._file.initWithPath(path); michael@0: this._foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Components.interfaces.nsIFileOutputStream); michael@0: this._foStream.init(this._file, PR_WRITE_ONLY | PR_CREATE_FILE | PR_APPEND, michael@0: 0664, 0); michael@0: }, michael@0: michael@0: getLogCallback : function() { michael@0: return function (msg) { michael@0: var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; michael@0: if (MozillaFileLogger._foStream) michael@0: this._foStream.write(data, data.length); michael@0: michael@0: if (data.indexOf("SimpleTest FINISH") >= 0) { michael@0: MozillaFileLogger.close(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: log : function(msg) { michael@0: if (this._foStream) michael@0: this._foStream.write(msg, msg.length); michael@0: }, michael@0: michael@0: close : function() { michael@0: if(this._foStream) michael@0: this._foStream.close(); michael@0: michael@0: this._foStream = null; michael@0: this._file = null; michael@0: } michael@0: }; michael@0: michael@0: this.MozillaLogger = MozillaLogger; michael@0: this.SpecialPowersLogger = SpecialPowersLogger; michael@0: this.MozillaFileLogger = MozillaFileLogger;