1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/specialpowers/content/MozillaLogger.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 1.4 +/** 1.5 + * MozillaLogger, a base class logger that just logs to stdout. 1.6 + */ 1.7 + 1.8 +function MozillaLogger(aPath) { 1.9 +} 1.10 + 1.11 +MozillaLogger.prototype = { 1.12 + 1.13 + init : function(path) {}, 1.14 + 1.15 + getLogCallback : function() { 1.16 + return function (msg) { 1.17 + var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; 1.18 + dump(data); 1.19 + } 1.20 + }, 1.21 + 1.22 + log : function(msg) { 1.23 + dump(msg); 1.24 + }, 1.25 + 1.26 + close : function() {} 1.27 +}; 1.28 + 1.29 + 1.30 +/** 1.31 + * SpecialPowersLogger, inherits from MozillaLogger and utilizes SpecialPowers. 1.32 + * intented to be used in content scripts to write to a file 1.33 + */ 1.34 +function SpecialPowersLogger(aPath) { 1.35 + // Call the base constructor 1.36 + MozillaLogger.call(this); 1.37 + this.prototype = new MozillaLogger(aPath); 1.38 + this.init(aPath); 1.39 +} 1.40 + 1.41 +SpecialPowersLogger.prototype = { 1.42 + init : function (path) { 1.43 + SpecialPowers.setLogFile(path); 1.44 + }, 1.45 + 1.46 + getLogCallback : function () { 1.47 + return function (msg) { 1.48 + var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; 1.49 + SpecialPowers.log(data); 1.50 + 1.51 + if (data.indexOf("SimpleTest FINISH") >= 0) { 1.52 + SpecialPowers.closeLogFile(); 1.53 + } 1.54 + } 1.55 + }, 1.56 + 1.57 + log : function (msg) { 1.58 + SpecialPowers.log(msg); 1.59 + }, 1.60 + 1.61 + close : function () { 1.62 + SpecialPowers.closeLogFile(); 1.63 + } 1.64 +}; 1.65 + 1.66 + 1.67 +/** 1.68 + * MozillaFileLogger, a log listener that can write to a local file. 1.69 + * intended to be run from chrome space 1.70 + */ 1.71 + 1.72 +/** Init the file logger with the absolute path to the file. 1.73 + It will create and append if the file already exists **/ 1.74 +function MozillaFileLogger(aPath) { 1.75 + // Call the base constructor 1.76 + MozillaLogger.call(this); 1.77 + this.prototype = new MozillaLogger(aPath); 1.78 + this.init(aPath); 1.79 +} 1.80 + 1.81 +MozillaFileLogger.prototype = { 1.82 + 1.83 + init : function (path) { 1.84 + var PR_WRITE_ONLY = 0x02; // Open for writing only. 1.85 + var PR_CREATE_FILE = 0x08; 1.86 + var PR_APPEND = 0x10; 1.87 + this._file = Components.classes["@mozilla.org/file/local;1"]. 1.88 + createInstance(Components.interfaces.nsILocalFile); 1.89 + this._file.initWithPath(path); 1.90 + this._foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]. 1.91 + createInstance(Components.interfaces.nsIFileOutputStream); 1.92 + this._foStream.init(this._file, PR_WRITE_ONLY | PR_CREATE_FILE | PR_APPEND, 1.93 + 0664, 0); 1.94 + }, 1.95 + 1.96 + getLogCallback : function() { 1.97 + return function (msg) { 1.98 + var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; 1.99 + if (MozillaFileLogger._foStream) 1.100 + this._foStream.write(data, data.length); 1.101 + 1.102 + if (data.indexOf("SimpleTest FINISH") >= 0) { 1.103 + MozillaFileLogger.close(); 1.104 + } 1.105 + } 1.106 + }, 1.107 + 1.108 + log : function(msg) { 1.109 + if (this._foStream) 1.110 + this._foStream.write(msg, msg.length); 1.111 + }, 1.112 + 1.113 + close : function() { 1.114 + if(this._foStream) 1.115 + this._foStream.close(); 1.116 + 1.117 + this._foStream = null; 1.118 + this._file = null; 1.119 + } 1.120 +}; 1.121 + 1.122 +this.MozillaLogger = MozillaLogger; 1.123 +this.SpecialPowersLogger = SpecialPowersLogger; 1.124 +this.MozillaFileLogger = MozillaFileLogger;