PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/firebug1.8/content/firebug/console/consoleExposed.js

http://fbug.googlecode.com/
JavaScript | 455 lines | 334 code | 85 blank | 36 comment | 63 complexity | dab987d4cb5b893ad9f25dbaccd69ed8 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. /* See license.txt for terms of usage */
  2. define([
  3. "firebug/chrome/reps",
  4. "firebug/lib/locale",
  5. "firebug/lib/wrapper",
  6. "firebug/lib/url",
  7. "firebug/js/stackFrame",
  8. "firebug/console/errors",
  9. "firebug/trace/debug",
  10. ],
  11. function(FirebugReps, Locale, Wrapper, Url, StackFrame, Errors, Debug) {
  12. // ********************************************************************************************* //
  13. /**
  14. * Returns a console object (bundled with passed window through closure). The object
  15. * provides all necessary APIs as described here: http://getfirebug.com/wiki/index.php/Console_API
  16. *
  17. * @param {Object} context
  18. * @param {Object} win
  19. */
  20. function createFirebugConsole(context, win)
  21. {
  22. // Defined as a chrome object, but exposed into the web content scope.
  23. var console = {
  24. __exposedProps__: {}
  25. };
  26. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  27. // Exposed Properties
  28. console.log = function log()
  29. {
  30. return logFormatted(arguments, "log", true);
  31. };
  32. console.debug = function debug()
  33. {
  34. return logFormatted(arguments, "debug", true);
  35. };
  36. console.info = function info()
  37. {
  38. return logFormatted(arguments, "info", true);
  39. };
  40. console.warn = function warn()
  41. {
  42. return logFormatted(arguments, "warn", true);
  43. };
  44. console.exception = function exception()
  45. {
  46. return logAssert("error", arguments);
  47. };
  48. console.assert = function assert(x)
  49. {
  50. if (!x)
  51. {
  52. var rest = [];
  53. for (var i = 1; i < arguments.length; i++)
  54. rest.push(arguments[i]);
  55. return logAssert("assert", rest);
  56. }
  57. return "_firebugIgnore";
  58. };
  59. console.dir = function dir(o)
  60. {
  61. Firebug.Console.log(o, context, "dir", Firebug.DOMPanel.DirTable);
  62. return "_firebugIgnore";
  63. };
  64. console.dirxml = function dirxml(o)
  65. {
  66. if (o instanceof Wrapper.getContentView(win).Window)
  67. o = o.document.documentElement;
  68. else if (o instanceof Wrapper.getContentView(win).Document)
  69. o = o.documentElement;
  70. Firebug.Console.log(o, context, "dirxml", Firebug.HTMLPanel.SoloElement);
  71. return "_firebugIgnore";
  72. };
  73. console.trace = function firebugDebuggerTracer()
  74. {
  75. var unwrapped = Wrapper.unwrapObject(win);
  76. unwrapped.top._firebugStackTrace = "console-tracer";
  77. debugger;
  78. delete unwrapped.top._firebugStackTrace;
  79. return "_firebugIgnore";
  80. };
  81. console.group = function group()
  82. {
  83. var sourceLink = getStackLink();
  84. Firebug.Console.openGroup(arguments, null, "group", null, false, sourceLink);
  85. return "_firebugIgnore";
  86. };
  87. console.groupEnd = function()
  88. {
  89. Firebug.Console.closeGroup(context);
  90. return "_firebugIgnore";
  91. };
  92. console.groupCollapsed = function()
  93. {
  94. var sourceLink = getStackLink();
  95. // noThrottle true can't be used here (in order to get the result row now)
  96. // because there can be some logs delayed in the queue and they would end up
  97. // in a different grup.
  98. // Use rather a different method that causes auto collapsing of the group
  99. // when it's created.
  100. Firebug.Console.openCollapsedGroup(arguments, null, "group", null, false, sourceLink);
  101. return "_firebugIgnore";
  102. };
  103. console.profile = function(title)
  104. {
  105. Firebug.Profiler.startProfiling(context, title);
  106. return "_firebugIgnore";
  107. };
  108. console.profileEnd = function()
  109. {
  110. Firebug.Profiler.stopProfiling(context);
  111. return "_firebugIgnore";
  112. };
  113. console.count = function(key)
  114. {
  115. var frameId = getStackFrameId();
  116. if (frameId)
  117. {
  118. if (!context.frameCounters)
  119. context.frameCounters = {};
  120. if (key != undefined)
  121. frameId += key;
  122. var frameCounter = context.frameCounters[frameId];
  123. if (!frameCounter)
  124. {
  125. var logRow = logFormatted(["0"], null, true, true);
  126. frameCounter = {logRow: logRow, count: 1};
  127. context.frameCounters[frameId] = frameCounter;
  128. }
  129. else
  130. ++frameCounter.count;
  131. var label = key == undefined
  132. ? frameCounter.count
  133. : key + " " + frameCounter.count;
  134. frameCounter.logRow.firstChild.firstChild.nodeValue = label;
  135. }
  136. return "_firebugIgnore";
  137. };
  138. console.clear = function()
  139. {
  140. Firebug.Console.clear(context);
  141. return "_firebugIgnore";
  142. };
  143. console.time = function(name, reset)
  144. {
  145. if (!name)
  146. return "_firebugIgnore";
  147. var time = new Date().getTime();
  148. if (!this.timeCounters)
  149. this.timeCounters = {};
  150. var key = "KEY"+name.toString();
  151. if (!reset && this.timeCounters[key])
  152. return "_firebugIgnore";
  153. this.timeCounters[key] = time;
  154. return "_firebugIgnore";
  155. };
  156. console.timeEnd = function(name)
  157. {
  158. var time = new Date().getTime();
  159. if (!this.timeCounters)
  160. return "_firebugIgnore";
  161. var key = "KEY"+name.toString();
  162. var timeCounter = this.timeCounters[key];
  163. if (timeCounter)
  164. {
  165. var diff = time - timeCounter;
  166. var label = name + ": " + diff + "ms";
  167. this.info(label);
  168. delete this.timeCounters[key];
  169. }
  170. return diff;
  171. };
  172. console.timeStamp = function(label)
  173. {
  174. label = label || "";
  175. if (FBTrace.DBG_CONSOLE)
  176. FBTrace.sysout("consoleExposed.timeStamp; " + label);
  177. var now = new Date();
  178. Firebug.NetMonitor.addTimeStamp(context, now.getTime(), label);
  179. var formattedTime = now.getHours() + ":" + now.getMinutes() + ":" +
  180. now.getSeconds() + "." + now.getMilliseconds();
  181. return logFormatted([formattedTime, label], "timeStamp");
  182. };
  183. console.table = function(data, columns)
  184. {
  185. FirebugReps.Table.log(data, columns, context);
  186. return "_firebugIgnore";
  187. };
  188. console.error = function error()
  189. {
  190. // TODO stack trace
  191. if (arguments.length == 1)
  192. {
  193. return logAssert("error", arguments); // add more info based on stack trace
  194. }
  195. else
  196. {
  197. Errors.increaseCount(context);
  198. return logFormatted(arguments, "error", true); // user already added info
  199. }
  200. };
  201. console.memoryProfile = function(title)
  202. {
  203. Firebug.MemoryProfiler.start(context, title);
  204. return "_firebugIgnore";
  205. };
  206. console.memoryProfileEnd = function()
  207. {
  208. Firebug.MemoryProfiler.stop(context);
  209. return "_firebugIgnore";
  210. };
  211. console.firebug = Firebug.version;
  212. // Expose only these properties to the content scope (read only).
  213. console.__exposedProps__.log = "r";
  214. console.__exposedProps__.debug = "r";
  215. console.__exposedProps__.info = "r";
  216. console.__exposedProps__.warn = "r";
  217. console.__exposedProps__.exception = "r";
  218. console.__exposedProps__.assert = "r";
  219. console.__exposedProps__.dir = "r";
  220. console.__exposedProps__.dirxml = "r";
  221. console.__exposedProps__.trace = "r";
  222. console.__exposedProps__.group = "r";
  223. console.__exposedProps__.groupEnd = "r";
  224. console.__exposedProps__.groupCollapsed = "r";
  225. console.__exposedProps__.time = "r";
  226. console.__exposedProps__.timeEnd = "r";
  227. console.__exposedProps__.timeStamp = "r";
  228. console.__exposedProps__.profile = "r";
  229. console.__exposedProps__.profileEnd = "r";
  230. console.__exposedProps__.count = "r";
  231. console.__exposedProps__.clear = "r";
  232. console.__exposedProps__.table = "r";
  233. console.__exposedProps__.error = "r";
  234. console.__exposedProps__.firebug = "r";
  235. console.__exposedProps__.memoryProfile = "r";
  236. console.__exposedProps__.memoryProfileEnd = "r";
  237. // DBG console.uid = Math.random();
  238. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  239. // Helpers (not accessible from web content)
  240. function logFormatted(args, className, linkToSource, noThrottle)
  241. {
  242. var sourceLink = linkToSource ? getStackLink() : null;
  243. var rc = Firebug.Console.logFormatted(args, context, className, noThrottle, sourceLink);
  244. return rc ? rc : "_firebugIgnore";
  245. };
  246. function logAssert(category, args)
  247. {
  248. Errors.increaseCount(context);
  249. if (!args || !args.length || args.length == 0)
  250. var msg = [Locale.$STR("Assertion")];
  251. else
  252. var msg = args[0];
  253. // If there's no error message, there's also no stack trace. See Issue 4700.
  254. if (!msg)
  255. {
  256. var trace = null;
  257. }
  258. else if (msg.stack)
  259. {
  260. var trace = StackFrame.parseToStackTrace(msg.stack, context);
  261. if (FBTrace.DBG_CONSOLE)
  262. FBTrace.sysout("logAssert trace from msg.stack", trace);
  263. }
  264. else if (context.stackTrace)
  265. {
  266. var trace = context.stackTrace;
  267. if (FBTrace.DBG_CONSOLE)
  268. FBTrace.sysout("logAssert trace from context.window.stackTrace", trace);
  269. }
  270. else
  271. {
  272. var trace = getJSDUserStack();
  273. if (FBTrace.DBG_CONSOLE)
  274. FBTrace.sysout("logAssert trace from getJSDUserStack", trace);
  275. }
  276. trace = StackFrame.cleanStackTraceOfFirebug(trace);
  277. var url = msg && msg.fileName ? msg.fileName : win.location.href;
  278. var lineNo = (trace && msg && msg.lineNumber) ? msg.lineNumber : 0; // we may have only the line popped above
  279. var errorObject = new FirebugReps.ErrorMessageObj(msg, url, lineNo, "", category, context, trace);
  280. if (trace && trace.frames && trace.frames[0])
  281. errorObject.correctWithStackTrace(trace);
  282. errorObject.resetSource();
  283. if (args.length > 1)
  284. {
  285. errorObject.objects = []
  286. for (var i = 1; i < args.length; i++)
  287. errorObject.objects.push(args[i]);
  288. }
  289. var row = Firebug.Console.log(errorObject, context, "errorMessage");
  290. if (row)
  291. row.scrollIntoView();
  292. return "_firebugIgnore";
  293. };
  294. function getComponentsStackDump()
  295. {
  296. // Starting with our stack, walk back to the user-level code
  297. var frame = Components.stack;
  298. var userURL = win.location.href.toString();
  299. if (FBTrace.DBG_CONSOLE)
  300. FBTrace.sysout("consoleInjector.getComponentsStackDump initial stack for userURL "+userURL, frame);
  301. // Drop frames until we get into user code.
  302. while (frame && Url.isSystemURL(frame.filename) )
  303. frame = frame.caller;
  304. // Drop two more frames, the injected console function and firebugAppendConsole()
  305. //if (frame)
  306. // frame = frame.caller;
  307. //if (frame)
  308. // frame = frame.caller;
  309. if (FBTrace.DBG_CONSOLE)
  310. FBTrace.sysout("consoleInjector.getComponentsStackDump final stack for userURL "+userURL, frame);
  311. return frame;
  312. };
  313. function getStackLink()
  314. {
  315. return StackFrame.getFrameSourceLink(getComponentsStackDump());
  316. };
  317. function getJSDUserStack()
  318. {
  319. var trace = Firebug.Debugger.getCurrentStackTrace(context);
  320. var frames = trace ? trace.frames : null;
  321. if (frames && (frames.length > 0) )
  322. {
  323. var filteredFrames = [];
  324. for (var i = 0; i < frames.length; i++)
  325. {
  326. if (frames[i].href.indexOf("chrome:") == 0)
  327. continue;
  328. if (frames[i].href.indexOf("resource:") == 0)
  329. continue;
  330. // firebug-service scope reached, in some cases the url starts with file://
  331. if (frames[i].href.indexOf("modules/firebug-service.js") != -1)
  332. continue;
  333. // command line
  334. var fn = frames[i].getFunctionName() + "";
  335. if (fn && (fn.indexOf("_firebugEvalEvent") != -1))
  336. continue;
  337. filteredFrames.push(frames[i]);
  338. }
  339. // take the oldest frames, leave 2 behind they are injection code
  340. trace.frames = filteredFrames; //trace.frames.slice(2 - i);
  341. return trace;
  342. }
  343. else
  344. {
  345. return "Firebug failed to get stack trace with any frames";
  346. }
  347. };
  348. function getStackFrameId(inputFrame)
  349. {
  350. for (var frame = Components.stack; frame; frame = frame.caller)
  351. {
  352. if (frame.languageName == "JavaScript"
  353. && !(frame.filename && frame.filename.indexOf("://firebug/") > 0))
  354. {
  355. return frame.filename + "/" + frame.lineNumber;
  356. }
  357. }
  358. return null;
  359. };
  360. return console;
  361. }
  362. // ********************************************************************************************* //
  363. // Registration
  364. Firebug.ConsoleExposed =
  365. {
  366. createFirebugConsole: createFirebugConsole
  367. };
  368. return Firebug.ConsoleExposed;
  369. // ********************************************************************************************* //
  370. });