Mercurial > hg > config
comparison chrome/MozillaFileLogger.js @ 198:59d4f04497dd
package into extension for profile, add direct file logging instead of stdout r=vlad
| author | Joel Maher <jmaher@mozilla.com> |
|---|---|
| date | Tue, 05 Oct 2010 17:01:29 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 197:b4da709724e1 | 198:59d4f04497dd |
|---|---|
| 1 /** | |
| 2 * MozillaFileLogger, a log listener that can write to a local file. | |
| 3 */ | |
| 4 | |
| 5 var ipcMode = false; // running in e10s build and need to use IPC? | |
| 6 try { | |
| 7 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); | |
| 8 var ipcsanity = Components.classes["@mozilla.org/preferences-service;1"] | |
| 9 .getService(Components.interfaces.nsIPrefBranch); | |
| 10 ipcsanity.setIntPref("mochitest.ipcmode", 0); | |
| 11 } catch (e) { | |
| 12 ipcMode = true; | |
| 13 } | |
| 14 | |
| 15 function contentDispatchEvent(type, data, sync) { | |
| 16 if (typeof(data) === "undefined") { | |
| 17 data = {}; | |
| 18 } | |
| 19 | |
| 20 var element = document.createEvent("datacontainerevent"); | |
| 21 element.initEvent("contentEvent", true, false); | |
| 22 element.setData("sync", sync); | |
| 23 element.setData("type", type); | |
| 24 element.setData("data", JSON.stringify(data)); | |
| 25 document.dispatchEvent(element); | |
| 26 } | |
| 27 | |
| 28 function contentSyncEvent(type, data) { | |
| 29 contentDispatchEvent(type, data, 1); | |
| 30 } | |
| 31 | |
| 32 function contentAsyncEvent(type, data) { | |
| 33 contentDispatchEvent(type, data, 0); | |
| 34 } | |
| 35 | |
| 36 try { | |
| 37 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); | |
| 38 | |
| 39 if (Cc === undefined) { | |
| 40 var Cc = Components.classes; | |
| 41 var Ci = Components.interfaces; | |
| 42 } | |
| 43 } catch (ex) {} //running in ipcMode-chrome | |
| 44 | |
| 45 try { | |
| 46 const FOSTREAM_CID = "@mozilla.org/network/file-output-stream;1"; | |
| 47 const LF_CID = "@mozilla.org/file/local;1"; | |
| 48 | |
| 49 // File status flags. It is a bitwise OR of the following bit flags. | |
| 50 // Only one of the first three flags below may be used. | |
| 51 const PR_READ_ONLY = 0x01; // Open for reading only. | |
| 52 const PR_WRITE_ONLY = 0x02; // Open for writing only. | |
| 53 const PR_READ_WRITE = 0x04; // Open for reading and writing. | |
| 54 | |
| 55 // If the file does not exist, the file is created. | |
| 56 // If the file exists, this flag has no effect. | |
| 57 const PR_CREATE_FILE = 0x08; | |
| 58 | |
| 59 // The file pointer is set to the end of the file prior to each write. | |
| 60 const PR_APPEND = 0x10; | |
| 61 | |
| 62 // If the file exists, its length is truncated to 0. | |
| 63 const PR_TRUNCATE = 0x20; | |
| 64 | |
| 65 // If set, each write will wait for both the file data | |
| 66 // and file status to be physically updated. | |
| 67 const PR_SYNC = 0x40; | |
| 68 | |
| 69 // If the file does not exist, the file is created. If the file already | |
| 70 // exists, no action and NULL is returned. | |
| 71 const PR_EXCL = 0x80; | |
| 72 } catch (ex) { | |
| 73 // probably not running in the test harness | |
| 74 } | |
| 75 | |
| 76 /** Init the file logger with the absolute path to the file. | |
| 77 It will create and append if the file already exists **/ | |
| 78 var MozillaFileLogger = {}; | |
| 79 | |
| 80 | |
| 81 MozillaFileLogger.init = function(path) { | |
| 82 if (ipcMode) { | |
| 83 contentAsyncEvent("LoggerInit", {"filename": path}); | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 try { | |
| 88 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); | |
| 89 } catch (ex) {} //running in ipcMode-chrome | |
| 90 | |
| 91 MozillaFileLogger._file = Cc[LF_CID].createInstance(Ci.nsILocalFile); | |
| 92 MozillaFileLogger._file.initWithPath(path); | |
| 93 MozillaFileLogger._foStream = Cc[FOSTREAM_CID].createInstance(Ci.nsIFileOutputStream); | |
| 94 MozillaFileLogger._foStream.init(this._file, PR_WRITE_ONLY | PR_CREATE_FILE | PR_APPEND, | |
| 95 0664, 0); | |
| 96 } | |
| 97 | |
| 98 MozillaFileLogger.getLogCallback = function() { | |
| 99 if (ipcMode) { | |
| 100 return function(msg) { | |
| 101 contentAsyncEvent("Logger", {"num": msg.num, "level": msg.level, "info": msg.info.join(' ')}); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 return function (msg) { | |
| 106 try { | |
| 107 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); | |
| 108 } catch(ex) {} //running in ipcMode-chrome | |
| 109 | |
| 110 var data = msg.num + " " + msg.level + " " + msg.info.join(' ') + "\n"; | |
| 111 if (MozillaFileLogger._foStream) | |
| 112 MozillaFileLogger._foStream.write(data, data.length); | |
| 113 | |
| 114 if (data.indexOf("SimpleTest FINISH") >= 0) { | |
| 115 MozillaFileLogger.close(); | |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 // This is only used from chrome space by the reftest harness | |
| 121 MozillaFileLogger.log = function(msg) { | |
| 122 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); | |
| 123 if (MozillaFileLogger._foStream) | |
| 124 MozillaFileLogger._foStream.write(msg, msg.length); | |
| 125 } | |
| 126 | |
| 127 MozillaFileLogger.close = function() { | |
| 128 if (ipcMode) { | |
| 129 contentAsyncEvent("LoggerClose"); | |
| 130 return; | |
| 131 } | |
| 132 | |
| 133 try { | |
| 134 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); | |
| 135 } catch(ex) {} //running in ipcMode-chrome | |
| 136 | |
| 137 if(MozillaFileLogger._foStream) | |
| 138 MozillaFileLogger._foStream.close(); | |
| 139 | |
| 140 MozillaFileLogger._foStream = null; | |
| 141 MozillaFileLogger._file = null; | |
| 142 } | |
| 143 | |
| 144 if (ipcMode == false) { | |
| 145 try { | |
| 146 var prefs = Components.classes['@mozilla.org/preferences-service;1'] | |
| 147 .getService(Components.interfaces.nsIPrefBranch2); | |
| 148 var filename = prefs.getCharPref('talos.logfile'); | |
| 149 MozillaFileLogger.init(filename); | |
| 150 } catch (ex) {} //pref does not exist, return empty string | |
| 151 } | |
| 152 | |
| 153 |
