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

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

mercurial