IT story

방화범을 사용하여 전체 프로그램에 대한 기능 로그 / 스택 추적 인쇄

hot-time 2020. 9. 6. 12:03
반응형

방화범을 사용하여 전체 프로그램에 대한 기능 로그 / 스택 추적 인쇄


Firebug에는 특정 함수 이름에 대한 호출을 기록하는 기능이 있습니다. 때때로 페이지 렌더링을 중지하지만 오류나 경고를 발생시키지 않는 버그를 찾고 있습니다. 버그는 절반 정도만 나타납니다. 그렇다면 전체 프로그램에 대한 모든 함수 호출 목록이나 전체 프로그램 실행에 대한 일종의 스택 추적 목록을 얻으려면 어떻게해야합니까?


Firefox는 console.trace() 호출 스택을 인쇄하는 데 매우 편리한 기능을 제공합니다 . ChromeIE 11 에서도 사용할 수 있습니다 .

또는 다음과 같이 시도하십시오.

function print_call_stack() {
  var stack = new Error().stack;
  console.log("PRINTING CALL STACK");
  console.log( stack );
}

스택 추적이 필요할 때 다음을 수행합니다. 아마도 여기에서 영감을 얻을 수 있습니다.

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.


I accomplished this without firebug. Tested in both chrome and 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.


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.

참고URL : https://stackoverflow.com/questions/4671031/print-function-log-stack-trace-for-entire-program-using-firebug

반응형