summaryrefslogtreecommitdiffstats
path: root/testing/specialpowers/content/MozillaLogger.js
blob: 52e16cabc4c8306b8181c73a8e87257a338cfc2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 * MozillaLogger, a base class logger that just logs to stdout.
 */

"use strict";

function MozillaLogger(aPath) {
}

function formatLogMessage(msg) {
    return msg.info.join(' ') + "\n";
}

MozillaLogger.prototype = {
  init : function(path) {},

  getLogCallback : function() {
    return function (msg) {
      var data = formatLogMessage(msg);
      dump(data);
    };
  },

  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) {
      var data = formatLogMessage(msg);
      SpecialPowers.log(data);

      if (data.indexOf("SimpleTest FINISH") >= 0) {
        SpecialPowers.closeLogFile();
      }
    };
  },

  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 = {

  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"].
                                     createInstance(Components.interfaces.nsIFileOutputStream);
    this._foStream.init(this._file, PR_WRITE_ONLY | PR_CREATE_FILE | PR_APPEND,
                                     436 /* 0664 */, 0);

    this._converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                    createInstance(Components.interfaces.nsIConverterOutputStream);
    this._converter.init(this._foStream, "UTF-8", 0, 0);
  },

  getLogCallback : function() {
    return function (msg) {
      var data = formatLogMessage(msg);
      if (MozillaFileLogger._converter) {
        this._converter.writeString(data);
      }

      if (data.indexOf("SimpleTest FINISH") >= 0) {
        MozillaFileLogger.close();
      }
    };
  },

  log : function(msg) {
    if (this._converter) {
      this._converter.writeString(msg);
    }
  },
  close : function() {
    if (this._converter) {
      this._converter.flush();
      this._converter.close();
    }

    this._foStream = null;
    this._converter = null;
    this._file = null;
  }
};

this.MozillaLogger = MozillaLogger;
this.SpecialPowersLogger = SpecialPowersLogger;
this.MozillaFileLogger = MozillaFileLogger;