Print function log /stack trace for entire program using firebug

后端 未结 5 1235
温柔的废话
温柔的废话 2020-12-07 23:52

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 warn

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 00:34

    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.

提交回复
热议问题