cowboy / javascript-debug

JavaScript Debug: A simple wrapper for console.log
http://benalman.com/projects/javascript-debug-console-log/
GNU General Public License v2.0
434 stars 96 forks source link

[enhancement] Log function call order #5

Closed dcondrey closed 8 years ago

dcondrey commented 8 years ago

Occasionally I will add console messages at the beginning of each function usually when beginning to review code I'm not familiar with.

Add a check for url parameter to enable or disable any logging.

// example
window.log = function f() {
    log.history = log.history || [];
    log.history.push(arguments);
    if (this.console) {
        var args = arguments,
            newarr;
        args.callee = args.callee.caller;
        newarr = [].slice.call(args);
        if (typeof console.log === 'object') log.apply.call(console.log, console, newarr);
        else console.log.apply(console, newarr);
    }
};
options={
    'debug': window.location.href.indexOf('?debug=true') > -1
}
options.log = options.debug ? function () {
    console && console.log.apply(console, arguments);
} : function () {};

Add a parameter to output a log message for every function call. Something like console.log([class] method);

// example
function augment(withFn) {
    var name, fn;
    for (name in window) {
        fn = window[name];
        if (typeof fn === 'function') {
            window[name] = (function(name, fn) {
                var args = arguments;
                return function() {
                    withFn.apply(this, args);
                    return fn.apply(this, arguments);

                }
            })(name, fn);
        }
    }
}

augment(function(name, fn) {
    console.log([class]: fn);
});