summaryrefslogtreecommitdiff
path: root/scripts/lib/io.js
blob: c4857711f5008fb2c4f883f915924dde2bf5c341 (plain)
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
(function () {
        var prefixMessage     = "\n==> DEBUG [MESSAGE]    : ";
        var prefixError       = "\n==> DEBUG [ERROR]      : ";
        var prefixStack       = "\n==> DEBUG [STACK]      : ";
        var prefixArguments   = "\n==> DEBUG [ARGUMENTS]  : ";
        var prefixCaller      = "\n==> DEBUG [CALLER]";
        var prefixFunction = "\n------>";
        var regHasDwb = new RegExp("[^]*/\\*<dwb\\*/([^]*)/\\*dwb>\\*/[^]*");

        Object.defineProperties(io, {
            "debug" : 
            {
                value : function (params) 
                {
                    var message = new String();
                    params = params || {};
                    var offset = params.offset || 0;
                    if (params.message) 
                    {
                        message += prefixMessage + params.message;
                    }
                    if (params.error && params.error instanceof Error) 
                    {
                        var line = params.error.line || params.error.line === 0 ? params.error.line : "?";
                        var error = params.error;
                        if (!error.stack) 
                        {
                            try 
                            {
                                throw new Error(error.message);
                            }
                            catch(e) 
                            { 
                                error = e;
                            }
                            offset += 1;
                        }
                        message += prefixError + "Exception in line " + line + ": " + error.message + 
                            prefixStack + "[" + error.stack.match(/[^\n]+/g).slice(offset).join("] [")+"]"; 
                    }
                    else 
                    {
                        try 
                        {
                            throw new Error();
                        }
                        catch(e) 
                        {
                            message += prefixStack + "[" + e.stack.match(/[^\n]+/g).slice(offset + 1).join("] [")+"]"; 
                        }
                    }
                    if (params.arguments) 
                    {
                        message += prefixArguments + JSON.stringify(params.arguments);
                        var caller = String(params.arguments.callee.caller);
                        message += prefixCaller;
                        message += prefixFunction + "\n";
                        message += caller.replace(regHasDwb, "$1").replace(/\n/gm, "\n  ");
                        message += prefixFunction;
                    }
                    io.print(message + "\n", "stderr");
                }

            }
        });
})();
Object.freeze(io);