testing/specialpowers/content/MozillaLogger.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /**
michael@0 2 * MozillaLogger, a base class logger that just logs to stdout.
michael@0 3 */
michael@0 4
michael@0 5 function MozillaLogger(aPath) {
michael@0 6 }
michael@0 7
michael@0 8 MozillaLogger.prototype = {
michael@0 9
michael@0 10 init : function(path) {},
michael@0 11
michael@0 12 getLogCallback : function() {
michael@0 13 return function (msg) {
michael@0 14 var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n";
michael@0 15 dump(data);
michael@0 16 }
michael@0 17 },
michael@0 18
michael@0 19 log : function(msg) {
michael@0 20 dump(msg);
michael@0 21 },
michael@0 22
michael@0 23 close : function() {}
michael@0 24 };
michael@0 25
michael@0 26
michael@0 27 /**
michael@0 28 * SpecialPowersLogger, inherits from MozillaLogger and utilizes SpecialPowers.
michael@0 29 * intented to be used in content scripts to write to a file
michael@0 30 */
michael@0 31 function SpecialPowersLogger(aPath) {
michael@0 32 // Call the base constructor
michael@0 33 MozillaLogger.call(this);
michael@0 34 this.prototype = new MozillaLogger(aPath);
michael@0 35 this.init(aPath);
michael@0 36 }
michael@0 37
michael@0 38 SpecialPowersLogger.prototype = {
michael@0 39 init : function (path) {
michael@0 40 SpecialPowers.setLogFile(path);
michael@0 41 },
michael@0 42
michael@0 43 getLogCallback : function () {
michael@0 44 return function (msg) {
michael@0 45 var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n";
michael@0 46 SpecialPowers.log(data);
michael@0 47
michael@0 48 if (data.indexOf("SimpleTest FINISH") >= 0) {
michael@0 49 SpecialPowers.closeLogFile();
michael@0 50 }
michael@0 51 }
michael@0 52 },
michael@0 53
michael@0 54 log : function (msg) {
michael@0 55 SpecialPowers.log(msg);
michael@0 56 },
michael@0 57
michael@0 58 close : function () {
michael@0 59 SpecialPowers.closeLogFile();
michael@0 60 }
michael@0 61 };
michael@0 62
michael@0 63
michael@0 64 /**
michael@0 65 * MozillaFileLogger, a log listener that can write to a local file.
michael@0 66 * intended to be run from chrome space
michael@0 67 */
michael@0 68
michael@0 69 /** Init the file logger with the absolute path to the file.
michael@0 70 It will create and append if the file already exists **/
michael@0 71 function MozillaFileLogger(aPath) {
michael@0 72 // Call the base constructor
michael@0 73 MozillaLogger.call(this);
michael@0 74 this.prototype = new MozillaLogger(aPath);
michael@0 75 this.init(aPath);
michael@0 76 }
michael@0 77
michael@0 78 MozillaFileLogger.prototype = {
michael@0 79
michael@0 80 init : function (path) {
michael@0 81 var PR_WRITE_ONLY = 0x02; // Open for writing only.
michael@0 82 var PR_CREATE_FILE = 0x08;
michael@0 83 var PR_APPEND = 0x10;
michael@0 84 this._file = Components.classes["@mozilla.org/file/local;1"].
michael@0 85 createInstance(Components.interfaces.nsILocalFile);
michael@0 86 this._file.initWithPath(path);
michael@0 87 this._foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
michael@0 88 createInstance(Components.interfaces.nsIFileOutputStream);
michael@0 89 this._foStream.init(this._file, PR_WRITE_ONLY | PR_CREATE_FILE | PR_APPEND,
michael@0 90 0664, 0);
michael@0 91 },
michael@0 92
michael@0 93 getLogCallback : function() {
michael@0 94 return function (msg) {
michael@0 95 var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n";
michael@0 96 if (MozillaFileLogger._foStream)
michael@0 97 this._foStream.write(data, data.length);
michael@0 98
michael@0 99 if (data.indexOf("SimpleTest FINISH") >= 0) {
michael@0 100 MozillaFileLogger.close();
michael@0 101 }
michael@0 102 }
michael@0 103 },
michael@0 104
michael@0 105 log : function(msg) {
michael@0 106 if (this._foStream)
michael@0 107 this._foStream.write(msg, msg.length);
michael@0 108 },
michael@0 109
michael@0 110 close : function() {
michael@0 111 if(this._foStream)
michael@0 112 this._foStream.close();
michael@0 113
michael@0 114 this._foStream = null;
michael@0 115 this._file = null;
michael@0 116 }
michael@0 117 };
michael@0 118
michael@0 119 this.MozillaLogger = MozillaLogger;
michael@0 120 this.SpecialPowersLogger = SpecialPowersLogger;
michael@0 121 this.MozillaFileLogger = MozillaFileLogger;

mercurial