amc*_*ack 90 javascript firebug
Firebug能够记录对特定函数名称的调用.我正在寻找一个有时会阻止页面呈现的错误,但不会导致任何错误或警告.这个bug只出现了大约一半的时间.那么如何获取整个程序的所有函数调用列表,或者执行整个程序的某种堆栈跟踪?
Mat*_*rtz 212
Firefox提供 console.trace()
了打印调用堆栈非常方便的功能.它也可以在Chrome和IE 11中使用.
或者尝试这样的事情:
function print_call_stack() {
var stack = new Error().stack;
console.log("PRINTING CALL STACK");
console.log( stack );
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*sen 13
当我需要堆栈跟踪时,我会执行以下操作,也许您可以从中获取一些灵感:
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'));
}
};
Run Code Online (Sandbox Code Playgroud)
主持人的说明:这个答案中的代码似乎也出现在Eric Wenderlin博客的这篇文章中.这个答案的作者声称它是他自己的代码,但是在这里链接的博客文章之前写的.出于善意的目的,我已经添加了帖子和本说明的链接.
我没有萤火虫就做到了。经过chrome和firefox测试:
console.error("I'm debugging this code.");
Run Code Online (Sandbox Code Playgroud)
程序将其打印到控制台后,您可以单击它的小箭头以扩展调用堆栈。