Created
August 4, 2011 13:47
-
-
Save oyvindkinsey/1125181 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is the code available in the page | |
var DEBUG_LOG = (function () { | |
var timer = null, timeout = 5000; | |
var head; | |
return { | |
add: function (item) { | |
head = { value: item, next: head }; | |
if (timer) { | |
clearTimeout(timer); | |
} | |
timer = setTimeout(function () { | |
timer = null; | |
head = null; | |
}, timeout); | |
}, | |
get: function () { | |
var a = [], item = head; | |
if (!item) return a; | |
do { | |
a.push(item.value); | |
item = item.next; | |
} while (item); | |
return a; | |
}, | |
getStacktrace: function () { | |
var a = [], item = head; | |
if (!item) return a; | |
var toString = Object.prototype.toString; | |
do { | |
var args = [], arg, value, type; | |
// loop over the arguments | |
for (var i = 0, len = item.value.arguments.length; i < len; i++) { | |
value = item.value.arguments[i]; | |
type = toString.call(value).substring(8); | |
type = type.substring(0, type.length - 1); | |
switch (type) { | |
case "Function": | |
value = "fn:" + (value.name || /function\s([\w$]+)\(/.exec(value.toString())[1]); | |
break; | |
case "String": | |
value = "'" + value + "'"; | |
break; | |
case "Number": | |
case "Boolean": | |
case "RegExp": | |
break; | |
default: | |
value = type; | |
} | |
args.push(value); | |
} | |
a.push((item.value.fn.name || /function\s([\w$]+)\(/.exec(item.value.fn)[1]) + "(" + args.join(",") + ")"); | |
item = item.next; | |
} while (item); | |
return a; | |
} | |
}; | |
} ()); | |
// then the rewrite engine rewrites all /function [\w$]+\(.+?\{/ patterns adding 'DEBUG_LOG({fn:arguments.callee, arguments: arguments}); | |
function foo(bar){ | |
.. | |
// becomes | |
function foo(bar){DEBUG_LOG({fn:arguments.callee, arguments: arguments}) | |
... | |
// The listould easily be extended to have max number of items, but for my project I opted for using time and any sequence of actions lasting more than 5 seconds is recorded. | |
// When you want to use the stacktrace just call | |
DEBUG_LOG.getStacktrace(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment