home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / firefox / debug.js < prev    next >
Text File  |  2006-08-07  |  12KB  |  479 lines

  1. if (!isDef(G_GDEBUG)) {
  2. throw new Error("G_GDEBUG constant must be set before loading debug.js");
  3. }
  4. function G_Debug(who, msg) {
  5. if (G_GDEBUG) {
  6. G_GetDebugZone(who).debug(msg);
  7. }
  8. }
  9. function G_DebugL(who, msg) {
  10. if (G_GDEBUG) {
  11. var zone = G_GetDebugZone(who);
  12. if (zone.zoneIsEnabled()) {
  13. G_debugService.dump(
  14. G_File.LINE_END_CHAR +
  15. "************************************************************" +
  16. G_File.LINE_END_CHAR);
  17. G_Debug(who, msg);
  18. G_debugService.dump(
  19. "************************************************************" +
  20. G_File.LINE_END_CHAR +
  21. G_File.LINE_END_CHAR);
  22. }
  23. }
  24. }
  25. function G_TraceCall(who, msg) {
  26. if (G_GDEBUG) {
  27. if (G_debugService.callTracingEnabled()) {
  28. G_debugService.dump(msg + G_File.LINE_END_CHAR);
  29. }
  30. }
  31. }
  32. function G_Error(who, msg) {
  33. if (G_GDEBUG) {
  34. G_GetDebugZone(who).error(msg);
  35. }
  36. }
  37. function G_Assert(who, condition, msg) {
  38. if (G_GDEBUG) {
  39. G_GetDebugZone(who).assert(condition, msg);
  40. }
  41. }
  42. function G_AssertEqual(who, expected, actual, msg) {
  43. if (G_GDEBUG) {
  44. G_GetDebugZone(who).assert(
  45. expected == actual,
  46. msg + " Expected: {%s}, got: {%s}".subs(expected, actual));
  47. }
  48. }
  49. function G_GetDebugZone(who) {
  50. if (G_GDEBUG) {
  51. var zone = "?";
  52. if (who && who.debugZone) {
  53. zone = who.debugZone;
  54. } else if (isString(who)) {
  55. zone = who;
  56. }
  57. return G_debugService.getZone(zone);
  58. }
  59. }
  60. function G_DebugZone(service, prefix, zone) {
  61. if (G_GDEBUG) {
  62. this.debugService_ = service;
  63. this.prefix_ = prefix;
  64. this.zone_ = zone;
  65. this.zoneEnabledPrefName_ = prefix + ".zone." + this.zone_;
  66. this.settings_ = new G_DebugSettings();
  67. }
  68. }
  69. G_DebugZone.prototype.zoneIsEnabled = function() {
  70. if (G_GDEBUG) {
  71. var explicit = this.settings_.getSetting(this.zoneEnabledPrefName_, null);
  72. if (explicit !== null) {
  73. return explicit;
  74. } else {
  75. return this.debugService_.allZonesEnabled();
  76. }
  77. }
  78. }
  79. G_DebugZone.prototype.enableZone = function() {
  80. if (G_GDEBUG) {
  81. this.settings_.setDefault(this.zoneEnabledPrefName_, true);
  82. }
  83. }
  84. G_DebugZone.prototype.disableZone = function() {
  85. if (G_GDEBUG) {
  86. this.settings_.setDefault(this.zoneEnabledPrefName_, false);
  87. }
  88. }
  89. G_DebugZone.prototype.debug = function(msg) {
  90. if (G_GDEBUG) {
  91. if (this.zoneIsEnabled()) {
  92. this.debugService_.dump("[%s] %s%s".subs(this.zone_,
  93. msg,
  94. G_File.LINE_END_CHAR));
  95. }
  96. }
  97. }
  98. G_DebugZone.prototype.error = function(msg) {
  99. if (G_GDEBUG) {
  100. this.debugService_.dump("[%s] %s%s".subs(this.zone_,
  101. msg,
  102. G_File.LINE_END_CHAR));
  103. throw new Error(msg);
  104. debugger;
  105. }
  106. }
  107. G_DebugZone.prototype.assert = function(condition, msg) {
  108. if (G_GDEBUG) {
  109. if (condition !== true) {
  110. G_Error(this.zone_, "ASSERT FAILED: " + msg);
  111. }
  112. }
  113. }
  114. function G_DebugService(opt_prefix) {
  115. if (G_GDEBUG) {
  116. this.prefix_ = opt_prefix ? opt_prefix : "google-debug-service";
  117. this.shellEnabledPrefName_ = this.prefix_ + ".alsologtoshell";
  118. this.consoleEnabledPrefName_ = this.prefix_ + ".alsologtoconsole";
  119. this.allZonesEnabledPrefName_ = this.prefix_ + ".enableallzones";
  120. this.callTracingEnabledPrefName_ = this.prefix_ + ".trace-function-calls";
  121. this.logFileEnabledPrefName_ = this.prefix_ + ".logfileenabled";
  122. this.logFileErrorLevelPrefName_ = this.prefix_ + ".logfile-errorlevel";
  123. this.zones_ = {};
  124. this.loggifier = new G_Loggifier();
  125. this.settings_ = new G_DebugSettings();
  126. this.timer_ = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  127. this.timer_.initWithCallback(this,
  128. G_DebugService.TIMESTAMP_INTERVAL,
  129. Ci.nsITimer.TYPE_REPEATING_SLACK);
  130. this.activeSinceLastTimestamp_ = false;
  131. Cc["@mozilla.org/consoleservice;1"]
  132. .getService(Ci.nsIConsoleService)
  133. .registerListener(this);
  134. }
  135. }
  136. G_DebugService.ERROR_LEVEL_INFO = "INFO";
  137. G_DebugService.ERROR_LEVEL_WARNING = "WARNING";
  138. G_DebugService.ERROR_LEVEL_EXCEPTION = "EXCEPTION";
  139. G_DebugService.TIMESTAMP_INTERVAL = 1000 * 60 * 5; // 5 minutes
  140. G_DebugService.prototype.QueryInterface = function(iid) {
  141. if (iid.equals(Ci.nsISupports) ||
  142. iid.equals(Ci.nsITimerCallback)) {
  143. return this;
  144. }
  145. throw Components.results.NS_ERROR_NO_INTERFACE;
  146. }
  147. G_DebugService.prototype.alsoDumpToShell = function() {
  148. if (G_GDEBUG) {
  149. return this.settings_.getSetting(this.shellEnabledPrefName_, true);
  150. }
  151. }
  152. G_DebugService.prototype.alsoDumpToConsole = function() {
  153. if (G_GDEBUG) {
  154. return this.settings_.getSetting(this.consoleEnabledPrefName_, false);
  155. }
  156. }
  157. G_DebugService.prototype.logFileIsEnabled = function() {
  158. if (G_GDEBUG) {
  159. return this.settings_.getSetting(this.logFileEnabledPrefName_, false);
  160. }
  161. }
  162. G_DebugService.prototype.enableLogFile = function() {
  163. if (G_GDEBUG) {
  164. this.settings_.setDefault(this.logFileEnabledPrefName_, true);
  165. }
  166. }
  167. G_DebugService.prototype.disableLogFile = function() {
  168. if (G_GDEBUG) {
  169. this.settings_.setDefault(this.logFileEnabledPrefName_, false);
  170. this.closeLogFile();
  171. }
  172. }
  173. G_DebugService.prototype.getLogFile = function() {
  174. if (G_GDEBUG) {
  175. return this.logFile_;
  176. }
  177. }
  178. G_DebugService.prototype.setLogFile = function(file) {
  179. if (G_GDEBUG) {
  180. if (this.logWriter_) {
  181. this.closeLogFile();
  182. }
  183. this.logFile_ = file;
  184. }
  185. }
  186. G_DebugService.prototype.closeLogFile = function() {
  187. this.logWriter_.close();
  188. this.logWriter_ = null;
  189. }
  190. G_DebugService.prototype.enableDumpToShell = function() {
  191. if (G_GDEBUG) {
  192. this.settings_.setDefault(this.shellEnabledPrefName_, true);
  193. }
  194. }
  195. G_DebugService.prototype.disableDumpToShell = function() {
  196. if (G_GDEBUG) {
  197. this.settings_.setDefault(this.shellEnabledPrefName_, false);
  198. }
  199. }
  200. G_DebugService.prototype.enableDumpToConsole = function() {
  201. if (G_GDEBUG) {
  202. this.settings_.setDefault(this.consoleEnabledPrefName_, true);
  203. }
  204. }
  205. G_DebugService.prototype.disableDumpToConsole = function() {
  206. if (G_GDEBUG) {
  207. this.settings_.setDefault(this.consoleEnabledPrefName_, false);
  208. }
  209. }
  210. G_DebugService.prototype.getZone = function(zone) {
  211. if (G_GDEBUG) {
  212. if (!this.zones_[zone])
  213. this.zones_[zone] = new G_DebugZone(this, this.prefix_, zone);
  214. return this.zones_[zone];
  215. }
  216. }
  217. G_DebugService.prototype.enableZone = function(zone) {
  218. if (G_GDEBUG) {
  219. var toEnable = this.getZone(zone);
  220. toEnable.enableZone();
  221. }
  222. }
  223. G_DebugService.prototype.disableZone = function(zone) {
  224. if (G_GDEBUG) {
  225. var toDisable = this.getZone(zone);
  226. toDisable.disableZone();
  227. }
  228. }
  229. G_DebugService.prototype.allZonesEnabled = function() {
  230. if (G_GDEBUG) {
  231. return this.settings_.getSetting(this.allZonesEnabledPrefName_, false);
  232. }
  233. }
  234. G_DebugService.prototype.enableAllZones = function() {
  235. if (G_GDEBUG) {
  236. this.settings_.setDefault(this.allZonesEnabledPrefName_, true);
  237. }
  238. }
  239. G_DebugService.prototype.disableAllZones = function() {
  240. if (G_GDEBUG) {
  241. this.settings_.setDefault(this.allZonesEnabledPrefName_, false);
  242. }
  243. }
  244. G_DebugService.prototype.callTracingEnabled = function() {
  245. if (G_GDEBUG) {
  246. return this.settings_.getSetting(this.callTracingEnabledPrefName_, false);
  247. }
  248. }
  249. G_DebugService.prototype.enableCallTracing = function() {
  250. if (G_GDEBUG) {
  251. this.settings_.setDefault(this.callTracingEnabledPrefName_, true);
  252. }
  253. }
  254. G_DebugService.prototype.disableCallTracing = function() {
  255. if (G_GDEBUG) {
  256. this.settings_.setDefault(this.callTracingEnabledPrefName_, false);
  257. }
  258. }
  259. G_DebugService.prototype.getLogFileErrorLevel = function() {
  260. if (G_GDEBUG) {
  261. var level = this.settings_.getSetting(this.logFileErrorLevelPrefName_,
  262. G_DebugService.ERROR_LEVEL_EXCEPTION);
  263. return level.toUpperCase();
  264. }
  265. }
  266. G_DebugService.prototype.setLogFileErrorLevel = function(level) {
  267. if (G_GDEBUG) {
  268. level = level.toUpperCase();
  269. if (level != G_DebugService.ERROR_LEVEL_INFO &&
  270. level != G_DebugService.ERROR_LEVEL_WARNING &&
  271. level != G_DebugService.ERROR_LEVEL_EXCEPTION) {
  272. throw new Error("Invalid error level specified: {" + level + "}");
  273. }
  274. this.settings_.setDefault(this.logFileErrorLevelPrefName_, level);
  275. }
  276. }
  277. G_DebugService.prototype.notify = function(timer) {
  278. if (!this.activeSinceLastTimestamp_) {
  279. return;
  280. }
  281. this.dump(G_File.LINE_END_CHAR +
  282. "=========== Date: " +
  283. new Date().toLocaleString() +
  284. " ============" +
  285. G_File.LINE_END_CHAR +
  286. G_File.LINE_END_CHAR);
  287. this.activeSinceLastTimestamp_ = false;
  288. }
  289. G_DebugService.prototype.dump = function(msg) {
  290. if (G_GDEBUG) {
  291. if (this.alsoDumpToShell()) {
  292. dump(msg);
  293. }
  294. if (this.alsoDumpToConsole()) {
  295. try {
  296. var console = Components.classes['@mozilla.org/consoleservice;1']
  297. .getService(Components.interfaces.nsIConsoleService);
  298. console.logStringMessage(msg);
  299. } catch(e) {
  300. dump("G_DebugZone ERROR: COULD NOT DUMP TO CONSOLE" +
  301. G_File.LINE_END_CHAR);
  302. }
  303. }
  304. this.maybeDumpToFile(msg);
  305. this.activeSinceLastTimestamp_ = true;
  306. }
  307. }
  308. G_DebugService.prototype.maybeDumpToFile = function(msg) {
  309. if (this.logFileIsEnabled() && this.logFile_) {
  310. if (!this.logWriter_) {
  311. this.logWriter_ = new G_FileWriter(this.logFile_, true);
  312. }
  313. this.logWriter_.write(msg);
  314. }
  315. }
  316. G_DebugService.prototype.observe = function(consoleMessage) {
  317. if (G_GDEBUG) {
  318. var errorLevel = this.getLogFileErrorLevel();
  319. if (!(consoleMessage instanceof Ci.nsIScriptError)) {
  320. if (errorLevel == G_DebugService.ERROR_LEVEL_INFO) {
  321. this.maybeDumpToFile(G_DebugService.ERROR_LEVEL_INFO + ": " +
  322. consoleMessage.message + G_File.LINE_END_CHAR);
  323. }
  324. return;
  325. }
  326. var flags = consoleMessage.flags;
  327. var sourceName = consoleMessage.sourceName;
  328. var lineNumber = consoleMessage.lineNumber;
  329. if (!flags) {
  330. flags = Ci.nsIScriptError.exceptionFlag;
  331. }
  332. if (!sourceName) {
  333. sourceName = "<unknown>";
  334. }
  335. if (!lineNumber) {
  336. lineNumber = "<unknown>";
  337. }
  338. if (flags & Ci.nsIScriptError.warningFlag) {
  339. if (errorLevel == G_DebugService.ERROR_LEVEL_WARNING ||
  340. errorLevel == G_DebugService.ERROR_LEVEL_INFO) {
  341. this.reportScriptError_(consoleMessage.message,
  342. sourceName,
  343. lineNumber,
  344. G_DebugService.ERROR_LEVEL_WARNING);
  345. }
  346. } else if (flags & Ci.nsIScriptError.exceptionFlag) {
  347. this.reportScriptError_(consoleMessage.message,
  348. sourceName,
  349. lineNumber,
  350. G_DebugService.ERROR_LEVEL_EXCEPTION);
  351. }
  352. }
  353. }
  354. G_DebugService.prototype.reportScriptError_ = function(message, sourceName,
  355. lineNumber, label) {
  356. if (sourceName.startsWith("http")) {
  357. return;
  358. }
  359. var message = ["",
  360. "------------------------------------------------------------",
  361. label + ": " + message,
  362. "location: " + sourceName + ", " + "line: " + lineNumber,
  363. "------------------------------------------------------------",
  364. "",
  365. ""].join(G_File.LINE_END_CHAR);
  366. if (this.alsoDumpToShell()) {
  367. dump(message);
  368. }
  369. this.maybeDumpToFile(message);
  370. }
  371. function G_Loggifier() {
  372. if (G_GDEBUG) {
  373. this.mark_(this);
  374. }
  375. }
  376. G_Loggifier.prototype.mark_ = function(obj) {
  377. if (G_GDEBUG) {
  378. obj.__loggified_ = true;
  379. }
  380. }
  381. G_Loggifier.prototype.isLoggified = function(obj) {
  382. if (G_GDEBUG) {
  383. return !!obj.__loggified_;
  384. }
  385. }
  386. G_Loggifier.prototype.getFunctionName_ = function(constructor) {
  387. if (G_GDEBUG) {
  388. return constructor.name || "???";
  389. }
  390. }
  391. G_Loggifier.prototype.loggify = function(obj) {
  392. if (G_GDEBUG) {
  393. if (!G_debugService.callTracingEnabled()) {
  394. return;
  395. }
  396. if (typeof window != "undefined" && obj == window ||
  397. this.isLoggified(obj))   // Don't go berserk!
  398. return;
  399. var zone = G_GetDebugZone(obj);
  400. if (!zone || !zone.zoneIsEnabled()) {
  401. return;
  402. }
  403. this.mark_(obj);
  404. function wrap(meth, objName, methName) {
  405. return function() {
  406. var args = new Array(arguments.length);
  407. var argsString = "";
  408. for (var i = 0; i < args.length; i++) {
  409. args[i] = arguments[i];
  410. argsString += (i == 0 ? "" : ", ");
  411. if (isFunction(args[i])) {
  412. argsString += "[function]";
  413. } else {
  414. argsString += args[i];
  415. }
  416. }
  417. G_TraceCall(this, "> " + objName + "." + methName + "(" +
  418. argsString + ")");
  419. try {
  420. var retVal = meth.apply(this, arguments);
  421. var reportedRetVal = retVal;
  422. if (typeof reportedRetVal == "undefined")
  423. reportedRetVal = "void";
  424. else if (reportedRetVal === "")
  425. reportedRetVal = "\"\" (empty string)";
  426. } catch (e) {
  427. if (e && !e.__logged) {
  428. G_TraceCall(this, "Error: " + e.message + ". " +
  429. e.fileName + ": " + e.lineNumber);
  430. try {
  431. e.__logged = true;
  432. } catch (e2) {
  433. throw e;
  434. }
  435. }
  436. throw e;      // Re-throw!
  437. }
  438. G_TraceCall(
  439. this,
  440. "< " + objName + "." + methName + ": " + reportedRetVal);
  441. return retVal;
  442. };
  443. };
  444. var ignoreLookup = {};
  445. if (arguments.length > 1) {
  446. for (var i = 1; i < arguments.length; i++) {
  447. ignoreLookup[arguments[i]] = true;
  448. }
  449. }
  450. for (var p in obj) {
  451. if (typeof obj[p] == "function" && obj[p].call && !ignoreLookup[p]) {
  452. var objName = this.getFunctionName_(obj.constructor);
  453. obj[p] = wrap(obj[p], objName, p);
  454. }
  455. }
  456. }
  457. }
  458. function G_DebugSettings() {
  459. this.defaults_ = {};
  460. this.prefs_ = new G_Preferences();
  461. }
  462. G_DebugSettings.prototype.getSetting = function(name, opt_default) {
  463. var override = this.prefs_.getPref(name, null);
  464. if (override !== null) {
  465. return override;
  466. } else if (name in this.defaults_) {
  467. return this.defaults_[name];
  468. } else {
  469. return opt_default;
  470. }
  471. }
  472. G_DebugSettings.prototype.setDefault = function(name, val) {
  473. this.defaults_[name] = val;
  474. }
  475. var G_debugService = new G_DebugService(); // Instantiate us!
  476. if (G_GDEBUG) {
  477. G_debugService.enableAllZones();
  478. }
  479.