2011-07-18 19:10:29 -07:00
|
|
|
/**
|
|
|
|
* MozillaLogger, a base class logger that just logs to stdout.
|
|
|
|
*/
|
|
|
|
|
2014-06-13 10:56:38 -07:00
|
|
|
"use strict";
|
|
|
|
|
2011-07-18 19:10:29 -07:00
|
|
|
function MozillaLogger(aPath) {
|
|
|
|
}
|
2014-07-23 06:31:00 -07:00
|
|
|
|
2014-07-17 00:02:00 -07:00
|
|
|
function formatLogMessage(msg) {
|
2014-07-23 06:31:00 -07:00
|
|
|
return msg.info.join(' ') + "\n";
|
2014-07-17 00:02:00 -07:00
|
|
|
}
|
2011-07-18 19:10:29 -07:00
|
|
|
|
|
|
|
MozillaLogger.prototype = {
|
|
|
|
init : function(path) {},
|
2014-07-17 00:02:00 -07:00
|
|
|
|
2011-07-18 19:10:29 -07:00
|
|
|
getLogCallback : function() {
|
|
|
|
return function (msg) {
|
2014-07-17 00:02:00 -07:00
|
|
|
var data = formatLogMessage(msg);
|
2011-07-18 19:10:29 -07:00
|
|
|
dump(data);
|
2014-07-17 00:02:00 -07:00
|
|
|
};
|
2011-07-18 19:10:29 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
log : function(msg) {
|
|
|
|
dump(msg);
|
|
|
|
},
|
|
|
|
|
|
|
|
close : function() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SpecialPowersLogger, inherits from MozillaLogger and utilizes SpecialPowers.
|
|
|
|
* intented to be used in content scripts to write to a file
|
|
|
|
*/
|
|
|
|
function SpecialPowersLogger(aPath) {
|
|
|
|
// Call the base constructor
|
|
|
|
MozillaLogger.call(this);
|
|
|
|
this.prototype = new MozillaLogger(aPath);
|
|
|
|
this.init(aPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
SpecialPowersLogger.prototype = {
|
|
|
|
init : function (path) {
|
|
|
|
SpecialPowers.setLogFile(path);
|
|
|
|
},
|
|
|
|
|
|
|
|
getLogCallback : function () {
|
|
|
|
return function (msg) {
|
2014-07-17 00:02:00 -07:00
|
|
|
var data = formatLogMessage(msg);
|
2011-07-18 19:10:29 -07:00
|
|
|
SpecialPowers.log(data);
|
|
|
|
|
|
|
|
if (data.indexOf("SimpleTest FINISH") >= 0) {
|
|
|
|
SpecialPowers.closeLogFile();
|
|
|
|
}
|
2014-07-17 00:02:00 -07:00
|
|
|
};
|
2011-07-18 19:10:29 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
log : function (msg) {
|
|
|
|
SpecialPowers.log(msg);
|
|
|
|
},
|
|
|
|
|
|
|
|
close : function () {
|
|
|
|
SpecialPowers.closeLogFile();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MozillaFileLogger, a log listener that can write to a local file.
|
|
|
|
* intended to be run from chrome space
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Init the file logger with the absolute path to the file.
|
|
|
|
It will create and append if the file already exists **/
|
|
|
|
function MozillaFileLogger(aPath) {
|
|
|
|
// Call the base constructor
|
|
|
|
MozillaLogger.call(this);
|
|
|
|
this.prototype = new MozillaLogger(aPath);
|
|
|
|
this.init(aPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
MozillaFileLogger.prototype = {
|
2014-07-17 00:02:00 -07:00
|
|
|
|
2011-07-18 19:10:29 -07:00
|
|
|
init : function (path) {
|
|
|
|
var PR_WRITE_ONLY = 0x02; // Open for writing only.
|
|
|
|
var PR_CREATE_FILE = 0x08;
|
|
|
|
var PR_APPEND = 0x10;
|
|
|
|
this._file = Components.classes["@mozilla.org/file/local;1"].
|
|
|
|
createInstance(Components.interfaces.nsILocalFile);
|
|
|
|
this._file.initWithPath(path);
|
|
|
|
this._foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
|
2014-07-11 05:15:29 -07:00
|
|
|
createInstance(Components.interfaces.nsIFileOutputStream);
|
|
|
|
this._foStream.init(this._file, PR_WRITE_ONLY | PR_CREATE_FILE | PR_APPEND,
|
2014-06-13 10:56:38 -07:00
|
|
|
436 /* 0664 */, 0);
|
2014-07-17 00:02:00 -07:00
|
|
|
|
|
|
|
this._converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
|
|
|
|
createInstance(Components.interfaces.nsIConverterOutputStream);
|
|
|
|
this._converter.init(this._foStream, "UTF-8", 0, 0);
|
2011-07-18 19:10:29 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getLogCallback : function() {
|
|
|
|
return function (msg) {
|
2014-07-17 00:02:00 -07:00
|
|
|
var data = formatLogMessage(msg);
|
|
|
|
if (MozillaFileLogger._converter) {
|
|
|
|
this._converter.writeString(data);
|
|
|
|
}
|
2011-07-18 19:10:29 -07:00
|
|
|
|
|
|
|
if (data.indexOf("SimpleTest FINISH") >= 0) {
|
|
|
|
MozillaFileLogger.close();
|
|
|
|
}
|
2014-07-17 00:02:00 -07:00
|
|
|
};
|
2011-07-18 19:10:29 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
log : function(msg) {
|
2014-07-17 00:02:00 -07:00
|
|
|
if (this._converter) {
|
|
|
|
this._converter.writeString(msg);
|
|
|
|
}
|
2011-07-18 19:10:29 -07:00
|
|
|
},
|
2014-07-11 05:15:29 -07:00
|
|
|
close : function() {
|
2014-07-17 00:02:00 -07:00
|
|
|
if (this._converter) {
|
|
|
|
this._converter.flush();
|
|
|
|
this._converter.close();
|
|
|
|
}
|
|
|
|
|
2011-07-18 19:10:29 -07:00
|
|
|
this._foStream = null;
|
2014-07-17 00:02:00 -07:00
|
|
|
this._converter = null;
|
2011-07-18 19:10:29 -07:00
|
|
|
this._file = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-23 21:32:27 -08:00
|
|
|
this.MozillaLogger = MozillaLogger;
|
|
|
|
this.SpecialPowersLogger = SpecialPowersLogger;
|
|
|
|
this.MozillaFileLogger = MozillaFileLogger;
|