Javascript 使用 firebug 打印整个程序的函数日志/堆栈跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4671031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print function log /stack trace for entire program using firebug
提问by amccormack
Firebug has the ability to log calls to a particular function name. I'm looking for a bug that sometimes stops a page from rendering, but doesn't cause any errors or warnings. The bug only appears about half the time. So how do I get a list of all the function calls for the entire program, or some kind of stack trace for the execution of the entire program?
Firebug 能够记录对特定函数名称的调用。我正在寻找有时会阻止页面呈现但不会导致任何错误或警告的错误。该错误仅出现大约一半的时间。那么我如何获得整个程序的所有函数调用的列表,或者整个程序执行的某种堆栈跟踪?
回答by Matt Schwartz
Firefox providesconsole.trace()
which is very handy to print the call stack. It is also available in Chromeand IE 11.
Firefox 提供了console.trace()
非常方便的打印调用堆栈。它也适用于Chrome和IE 11。
Alternatively try something like this:
或者尝试这样的事情:
function print_call_stack() {
var stack = new Error().stack;
console.log("PRINTING CALL STACK");
console.log( stack );
}
回答by Martin Jespersen
When i need a stack trace i do the following, maybe you can draw some inspiration from it:
当我需要堆栈跟踪时,我会执行以下操作,也许您可以从中汲取一些灵感:
function logStackTrace(levels) {
var callstack = [];
var isCallstackPopulated = false;
try {
i.dont.exist += 0; //doesn't exist- that's the point
} catch (e) {
if (e.stack) { //Firefox / chrome
var lines = e.stack.split('\n');
for (var i = 0, len = lines.length; i < len; i++) {
callstack.push(lines[i]);
}
//Remove call to logStackTrace()
callstack.shift();
isCallstackPopulated = true;
}
else if (window.opera && e.message) { //Opera
var lines = e.message.split('\n');
for (var i = 0, len = lines.length; i < len; i++) {
if (lines[i].match(/^\s*[A-Za-z0-9\-_$]+\(/)) {
var entry = lines[i];
//Append next line also since it has the file info
if (lines[i + 1]) {
entry += " at " + lines[i + 1];
i++;
}
callstack.push(entry);
}
}
//Remove call to logStackTrace()
callstack.shift();
isCallstackPopulated = true;
}
}
if (!isCallstackPopulated) { //IE and Safari
var currentFunction = arguments.callee.caller;
while (currentFunction) {
var fn = currentFunction.toString();
var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf("(")) || "anonymous";
callstack.push(fname);
currentFunction = currentFunction.caller;
}
}
if (levels) {
console.log(callstack.slice(0, levels).join('\n'));
}
else {
console.log(callstack.join('\n'));
}
};
Moderator's note: The code in this answer seems to also appear in this post from Eric Wenderlin's blog. The author of this answer claims it as his own code, though, written prior to the blog post linked here. Just for purposes of good-faith, I've added the link to the post and this note.
版主注:此答案中的代码似乎也出现在Eric Wenderlin 博客的这篇文章中。不过,这个答案的作者声称它是他自己的代码,是在此处链接的博客文章之前编写的。出于善意的目的,我添加了帖子和此注释的链接。
回答by satnam
I accomplished this without firebug. Tested in both chrome and firefox:
我在没有萤火虫的情况下完成了这项工作。在 chrome 和 firefox 中测试:
console.error("I'm debugging this code.");
Once your program prints that to the console, you can click the little arrow to it to expand the call stack.
一旦您的程序将其打印到控制台,您就可以单击指向它的小箭头以展开调用堆栈。
回答by casablanca
Try stepping through your code one line or one function at a time to determine where it stops working correctly. Or make some reasonable guesses and scatter logging statements through your code.
尝试一次单行执行代码或单步执行一个函数,以确定它在何处停止正常工作。或者通过您的代码进行一些合理的猜测和分散日志记录。
回答by Abraham Albero
Try this:
尝试这个:
console.trace()
I don't know if it's supported on all browsers, so I would check if it exists first.
我不知道是否所有浏览器都支持它,所以我会先检查它是否存在。