pmuellr (owner)

Revisions

gist: 189144 Download_button fork
public
Description:
have your JavaScript methods trace their execution
Public Clone URL: git://gist.github.com/189144.git
Embed All Files: show embed
function-tracer.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//------------------------------------------------------------------------------
// JavaScript function tracer for "classes"
//------------------------------------------------------------------------------
// from: pmuellr@muellerware.org
// home: http://gist.github.com/189144
//------------------------------------------------------------------------------
 
//------------------------------------------------------------------------------
// define our function tracer
//------------------------------------------------------------------------------
FunctionTracer = {};
 
//------------------------------------------------------------------------------
// logging function, default is to write to console
//------------------------------------------------------------------------------
FunctionTracer.logFunction = function(message) { console.log(message); };
 
//------------------------------------------------------------------------------
// print time in messages?
//------------------------------------------------------------------------------
FunctionTracer.printTime = false;
 
//------------------------------------------------------------------------------
// print arguments in messages?
//------------------------------------------------------------------------------
FunctionTracer.printArgs = false;
 
//------------------------------------------------------------------------------
// return a registered function
// example: say you have a function named "foo". You can trace it via:
// foo = FunctionTracer.traceFunction(foo, "foo");
//------------------------------------------------------------------------------
FunctionTracer.traceFunction = function(func, name) {
    if (!FunctionTracer.startTime) FunctionTracer.startTime = new Date().getTime();
 
    if (!FunctionTracer.indent) FunctionTracer.indent = "";
 
    //--------------------------------------------------------------------------
    // best effort printing an argument as a string
    //--------------------------------------------------------------------------
    function arg2string(argument) {
        if (argument === null) return "null";
        if (argument === undefined) return "undefined";
        try {
            return JSON.stringify(argument);
        }
        catch(e) {
            return argument.toString();
        }
    }
 
    //--------------------------------------------------------------------------
    // this function creates a wrapper for each function which traces itself,
    // then invokes the original function
    //--------------------------------------------------------------------------
    function getWrapper(func, name) {
        if (typeof func != "function") throw new Error(name + " is not a function");
        
        return function() {
            var oldIndent = FunctionTracer.indent;
            FunctionTracer.indent += " ";
            
            var message = oldIndent + name;
            
            if (FunctionTracer.printTime) {
                var time = new Date().getTime() - FunctionTracer.startTime;
                time = "" + time;
                while (time.length < 8) time = " " + time;
                
                message = time + ": " + message;
            }
            
            if (!FunctionTracer.printArgs) {
                message += "()";
            }
            else {
                message += "(";
                
                for (var i=0; i<arguments.length; i++) {
                    message += arg2string(arguments[i]);
                    if (i != arguments.length - 1) {
                        message += ", ";
                    }
                }
                
                message += ")";
            }
            
            try {
                (FunctionTracer.logFunction)(message);
                return func.apply(this, Array.prototype.splice.call(arguments, 0));
            }
            catch(e) {
                (FunctionTracer.logFunction)(message + "; exception: " + e);
                throw e;
            }
            finally {
                FunctionTracer.indent = oldIndent;
            }
        }
    }
    
    //--------------------------------------------------------------------------
    // main function logic
    //--------------------------------------------------------------------------
    (FunctionTracer.logFunction)("installed tracer: " + name + "()");
 
    return getWrapper(func, name);
}
 
//------------------------------------------------------------------------------
// register functions in an object to trace themselves
// example: say you have a 'class' called FooBar, that has a bunch of methods
// in it by virtual of having functions defined on FooBar.prototype. You can
// trace those methods via:
// FunctionTracer.traceFunctionsInObject(FooBar.prototype, "FooBar.");
//------------------------------------------------------------------------------
FunctionTracer.traceFunctionsInObject = function(object, prefix) {
    
    //--------------------------------------------------------------------------
    // process the functions in the object
    //--------------------------------------------------------------------------
    var functions = {};
    var getters = {};
    var setters = {};
    var isES5 = this.__lookupGetter__;
    
    for (var property in object) {
        //----------------------------------------------------------------------
        // ignore inherited properties
        //----------------------------------------------------------------------
        if (!object.hasOwnProperty(property))
            continue;
 
        //----------------------------------------------------------------------
        // check for getters/setters
        //----------------------------------------------------------------------
        var getter = undefined;
        var setter = undefined;
        
        if (isES5) {
            getter = object.__lookupGetter__(property);
            setter = object.__lookupSetter__(property);
        }
        
        var name = prefix + property;
        
        //----------------------------------------------------------------------
        // found a getter/setter?
        //----------------------------------------------------------------------
        if (getter || setter) {
            if (getter) {
                getters[property] = FunctionTracer.traceFunction(getter, name + "<get>");
            }
            
            if (setter) {
                setters[property] = FunctionTracer.traceFunction(setter, name + "<set>");
            }
        }
        
        //----------------------------------------------------------------------
        // found a function?
        //----------------------------------------------------------------------
        else {
            var func = object[property];
            if (typeof func != "function") continue;
            
            functions[property] = FunctionTracer.traceFunction(func, name);
        }
    }
    
    //--------------------------------------------------------------------------
    // install the wrappers; didn't do this in the original loop since we are
    // in some sense mutating the object while iterating on it's properties,
    // typically not a safe thing to do
    //--------------------------------------------------------------------------
    
    for (var property in functions) {
        object[property] = functions[property];
        (FunctionTracer.logFunction)("installed tracer: " + prefix + property + "()");
    }
    
    for (var property in getters) {
        object.__defineGetter__(property, getters[property]);
        (FunctionTracer.logFunction)("installed tracer: " + prefix + property + "<get>()");
    }
    
    for (var property in setters) {
        object.__defineSetter__(property, setters[property]);
        (FunctionTracer.logFunction)("installed tracer: " + prefix + property + "<set>()");
    }
}