How to get Javascript Function Calls/Trace at Runtime

前端 未结 8 1877
天命终不由人
天命终不由人 2020-12-23 13:56

As I interact with my AJAX based application at RUNTIME I\'d like the console to spit out all the functions it\'s calling. (so no stack trace, or breakpoints, or pr

相关标签:
8条回答
  • 2020-12-23 14:52

    Give a try to diyism_trace_for_javascript.htm:

    https://code.google.com/p/diyism-trace/downloads/list

    eval('window.c=function(){3+5;}');
    declare_ticks_for(window);
    
    function a(k, c) {
      return k + 2;
    }
    
    function b() {
      4 + 3;
      a(3, {'a':'c','b':'d'});
      c();
      return 5 + 4;
    }
    
    b();
    

    View logs in console tab of chrome or firefox

    0 讨论(0)
  • 2020-12-23 14:59

    I can't think of a great way to intercept all function calls globally to insert logging (though there is a decent workaround in the update section below).

    Instead, how about only adding logging to functions in a certain namespace that you care about? You can do this with the following setup code:

    var functionLogger = {};
    
    functionLogger.log = true;//Set this to false to disable logging 
    
    /**
     * Gets a function that when called will log information about itself if logging is turned on.
     *
     * @param func The function to add logging to.
     * @param name The name of the function.
     *
     * @return A function that will perform logging and then call the function. 
     */
    functionLogger.getLoggableFunction = function(func, name) {
        return function() {
            if (functionLogger.log) {
                var logText = name + '(';
    
                for (var i = 0; i < arguments.length; i++) {
                    if (i > 0) {
                        logText += ', ';
                    }
                    logText += arguments[i];
                }
                logText += ');';
    
                console.log(logText);
            }
    
            return func.apply(this, arguments);
        }
    };
    
    /**
     * After this is called, all direct children of the provided namespace object that are 
     * functions will log their name as well as the values of the parameters passed in.
     *
     * @param namespaceObject The object whose child functions you'd like to add logging to.
     */
    functionLogger.addLoggingToNamespace = function(namespaceObject){
        for(var name in namespaceObject){
            var potentialFunction = namespaceObject[name];
    
            if(Object.prototype.toString.call(potentialFunction) === '[object Function]'){
                namespaceObject[name] = functionLogger.getLoggableFunction(potentialFunction, name);
            }
        }
    };
    

    Then, for whatever namespaceObject you want to add logging to, you just call:

    functionLogger.addLoggingToNamespace(yourNamespaceObject);
    

    Here's a fiddle to see it in action.

    UPDATE
    Note that you can call functionLogger.addLoggingToNamespace(window); to add logging to all global functions at the time of the call. Also, if you really want, you can traverse the tree to find any functions and update them accordingly. The one downfall of this method is that it only works on functions that exist at the time. Thus, it's still not the greatest solution, but it's a LOT less work than adding logging statements by hand :)

    0 讨论(0)
提交回复
热议问题