Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Script CallStack information in a method callback

0
0
Hi Andi! Thanks for your question.

If you're using JScript and you don't want to enable debugging, then callee/caller is your only option as far as we can tell. Here's a basic sample (with thanks to StackOverflow):
publicclass Log
{
    privatereadonlydynamic _getStack;
    public Log(ScriptEngine engine) {
        _getStack = engine.Evaluate(@"(function () {
            var stack = '';
            var func = arguments.callee.caller;
            while (func) {
                var scan = /\W*function\s+([\w\$]+)\(/.exec(func.toString());
                var name = scan ? scan[1] : '[anonymous]';
                stack += '  at ' + name + '\n';
                func = func.caller;
            }
            return stack;
        }).valueOf()");
    }
    publicvoid Info(string text) {
        Console.WriteLine(text);
        Console.WriteLine(_getStack());
    }
}
Good luck!

Viewing all articles
Browse latest Browse all 2297