JSFiddle - React, Tailwind, and code Playground

by remguif

HTML

<script src="//cdn.jsdelivr.net/medium-editor/latest/js/medium-editor.min.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/medium-editor/latest/css/medium-editor.min.css">
<div class='editable'>
iuhohuh houiho
</div>Sets up logging for all functions in a namespace.

JavaScript

//**************************Set up your functionLogger*****************//
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);
        }

        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);
        }
    }
};    


//**************************Set up your namespace functions*****************//
var namespaceObject = {};

namespaceObject.test1 = function(a, b, c, d, e) {
    namespaceObject.test2(a + b, c + d + e);
};

namespaceObject.test2 = function(ab, cde) {

};

var editor = new MediumEditor('.editable');
functionLogger.addLoggingToNamespace(editor);       



//**************************Add logging to your namespace...