tags:

views:

51

answers:

2

Hi, I am trying to write a logger object which logs messages to screen. here is my code. http://github.com/huseyinyilmaz/javascript-logger in every function that needs to log something, I am writing loggerstart and loggerEnd functions at start and end of my functions. But I want to run thos codes automaticalls for every function. is there a way to modify Function prototype so every function call can run automatically. (I am not using any javascript framework.)

A: 

You could call every function with a wrapper function.

function wrapper(callback) {
    loggerstart();
    callback();
    loggerend();
}

And call it with wrapper(yourfunction);

alopix
So now he will need to augment each of his function in entire project with 10 millions of different functions with wrappers?
Artem Barger
Yes, he does. Otherwise there is no solution I know of. If you have one, post it, but don't only criticize my approach. Anyway...you can easly search and replace those 10 millions of different functions (I on't think that he has that much functions).
alopix
I need this logger to have as minimal footprint in my code as possible, so It can be integrated to my project with minimum efford. Your suggestion would definitely work, but its footprint will be huge.
yilmazhuseyin
Voting here is to express my attitude regarding your solution. And I assume once @yilmazhusein asked here such question he has already eliminated your suggestion as not quite feasible. And I'll promise to post my solution here right after I've find one.
Artem Barger
it's not the vote that annoys me. It's your unhelpful comment.
alopix
my unhelpful comment here is to let you know who and why down voted your unhelpful answer.
Artem Barger
Could be written nicer. And it is not unhelpfull, it's only a solution that needs a little efford using it. I don't think that there is another way to solve this problem.
alopix
+4  A: 

EDIT: Rewritten the function to make it more modular

Well, this is a creepy way to do it, but I use this way sometimes when I need overriding some functions. It works well, allows any kind of customization and easy to understand (still creepy).

However, you will need to have all your functions stored in some kind of global object. See the example for details.

function dynamic_call_params(func, fp) {
    return func(fp[0],fp[1],fp[2],fp[3],fp[4],fp[5],fp[6],fp[7],fp[8],fp[9],fp[10],fp[11],fp[12],fp[13],fp[14],fp[15],fp[16],fp[17],fp[18],fp[19]);
}

function attachWrapperToFunc(object, funcName, wrapperFunction) {
    object["_original_function_"+funcName] = object[funcName];
    object[funcName] = function() {
        return wrapperFunction(object, object["_original_function_"+funcName], funcName, arguments);
    }
}

function attachWrapperToObject(object, wrapperFunction) {
    for (varname in object) {
        if (typeof(object[varname]) == "function") {
            attachWrapperToFunc(object, varname, wrapperFunction);
        }
    }
}

And some usage example:

var myProgram = new Object();
myProgram.function_one = function(a,b,c,d) {
    alert(a+b+c+d);
}
myProgram.function_two = function(a,b) {
    alert(a*b);
}

myProgram.function_three = function(a) {
    alert(a);
}

function loggerWrapperFunction(functionObject, origFunction, origFunctionName, origParams) {
    alert("start: "+origFunctionName);
    var result = dynamic_call_params(origFunction, origParams);
    alert("end: "+origFunctionName);
    return result;
}

attachWrapperToObject(myProgram,loggerWrapperFunction);
myProgram.function_one(1,2,3,4);
myProgram.function_two(2,3);
myProgram.function_three(5);

Output will be: start,10,end,start,6,end,start,5,end

So generally it allows you to wrap each function in some object automatically with a custom written wrapper function.

Max
methods to my logger that way. May be I can go trough window object and append my global methods too. But functions in closure of another functions will be left(which can be added to logger manually). Looks like this is the best aproach to go. Thank you for your answer.
yilmazhuseyin
Looping through window object will fail. Problem is that there are some window properties that are loopable but not readable. For example try this: `alert(typeof(window["sessionStorage"]));`. Still, maybe you would be able to do it with some nasty exception handling to skip those unreadable properties. By the way, you can also add additional parameters (for example called function name) to the wrapper function parameters. Not hard to do but will provide your logger some info on what function is called and with what parameters.
Max
Changed the code to reflect that 'additional parameters' changes.
Max