|
1 /** |
|
2 * MozillaLogger, a base class logger that just logs to stdout. |
|
3 */ |
|
4 |
|
5 function MozillaLogger(aPath) { |
|
6 } |
|
7 |
|
8 MozillaLogger.prototype = { |
|
9 |
|
10 init : function(path) {}, |
|
11 |
|
12 getLogCallback : function() { |
|
13 return function (msg) { |
|
14 var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; |
|
15 dump(data); |
|
16 } |
|
17 }, |
|
18 |
|
19 log : function(msg) { |
|
20 dump(msg); |
|
21 }, |
|
22 |
|
23 close : function() {} |
|
24 }; |
|
25 |
|
26 |
|
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 } |
|
37 |
|
38 SpecialPowersLogger.prototype = { |
|
39 init : function (path) { |
|
40 SpecialPowers.setLogFile(path); |
|
41 }, |
|
42 |
|
43 getLogCallback : function () { |
|
44 return function (msg) { |
|
45 var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; |
|
46 SpecialPowers.log(data); |
|
47 |
|
48 if (data.indexOf("SimpleTest FINISH") >= 0) { |
|
49 SpecialPowers.closeLogFile(); |
|
50 } |
|
51 } |
|
52 }, |
|
53 |
|
54 log : function (msg) { |
|
55 SpecialPowers.log(msg); |
|
56 }, |
|
57 |
|
58 close : function () { |
|
59 SpecialPowers.closeLogFile(); |
|
60 } |
|
61 }; |
|
62 |
|
63 |
|
64 /** |
|
65 * MozillaFileLogger, a log listener that can write to a local file. |
|
66 * intended to be run from chrome space |
|
67 */ |
|
68 |
|
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 } |
|
77 |
|
78 MozillaFileLogger.prototype = { |
|
79 |
|
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 }, |
|
92 |
|
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); |
|
98 |
|
99 if (data.indexOf("SimpleTest FINISH") >= 0) { |
|
100 MozillaFileLogger.close(); |
|
101 } |
|
102 } |
|
103 }, |
|
104 |
|
105 log : function(msg) { |
|
106 if (this._foStream) |
|
107 this._foStream.write(msg, msg.length); |
|
108 }, |
|
109 |
|
110 close : function() { |
|
111 if(this._foStream) |
|
112 this._foStream.close(); |
|
113 |
|
114 this._foStream = null; |
|
115 this._file = null; |
|
116 } |
|
117 }; |
|
118 |
|
119 this.MozillaLogger = MozillaLogger; |
|
120 this.SpecialPowersLogger = SpecialPowersLogger; |
|
121 this.MozillaFileLogger = MozillaFileLogger; |