tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MozillaLogger.js (2287B)


      1 /**
      2 * MozillaLogger, a base class logger that just logs to stdout.
      3 */
      4 
      5 "use strict";
      6 
      7 function formatLogMessage(msg) {
      8  return msg.info.join(" ") + "\n";
      9 }
     10 
     11 function importMJS(mjs) {
     12  if (typeof ChromeUtils === "object") {
     13    return ChromeUtils.importESModule(mjs);
     14  }
     15  /* globals SpecialPowers */
     16  return SpecialPowers.ChromeUtils.importESModule(mjs);
     17 }
     18 
     19 // When running in release builds, we get a fake Components object in
     20 // web contexts, so we need to check that the Components object is sane,
     21 // too, not just that it exists.
     22 let haveComponents =
     23  typeof Components === "object" &&
     24  typeof Components.Constructor === "function";
     25 
     26 let CC = (
     27  haveComponents ? Components : SpecialPowers.wrap(SpecialPowers.Components)
     28 ).Constructor;
     29 
     30 let ConverterOutputStream = CC(
     31  "@mozilla.org/intl/converter-output-stream;1",
     32  "nsIConverterOutputStream",
     33  "init"
     34 );
     35 
     36 class MozillaLogger {
     37  get logCallback() {
     38    return msg => {
     39      this.log(formatLogMessage(msg));
     40    };
     41  }
     42 
     43  log(msg) {
     44    dump(msg);
     45  }
     46 
     47  close() {}
     48 }
     49 
     50 /**
     51 * MozillaFileLogger, a log listener that can write to a local file.
     52 * intended to be run from chrome space
     53 */
     54 
     55 /**
     56 * Init the file logger with the absolute path to the file.
     57 * It will create and append if the file already exists.
     58 */
     59 class MozillaFileLogger extends MozillaLogger {
     60  constructor(aPath) {
     61    super();
     62 
     63    const { FileUtils } = importMJS("resource://gre/modules/FileUtils.sys.mjs");
     64 
     65    this._file = FileUtils.File(aPath);
     66    this._foStream = FileUtils.openFileOutputStream(
     67      this._file,
     68      FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_APPEND
     69    );
     70 
     71    this._converter = ConverterOutputStream(this._foStream, "UTF-8");
     72  }
     73 
     74  get logCallback() {
     75    return msg => {
     76      if (this._converter) {
     77        var data = formatLogMessage(msg);
     78        this.log(data);
     79 
     80        if (data.includes("SimpleTest FINISH")) {
     81          this.close();
     82        }
     83      }
     84    };
     85  }
     86 
     87  log(msg) {
     88    if (this._converter) {
     89      this._converter.writeString(msg);
     90    }
     91  }
     92 
     93  close() {
     94    this._converter.flush();
     95    this._converter.close();
     96 
     97    this._foStream = null;
     98    this._converter = null;
     99    this._file = null;
    100  }
    101 }
    102 
    103 this.MozillaLogger = MozillaLogger;
    104 this.MozillaFileLogger = MozillaFileLogger;