summaryrefslogtreecommitdiff
path: root/scripts/lib/io.js
blob: 92170b14c96abffc935c0de3391b916cf53b5966 (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
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
(function () {
        var prefixMessage     = "\n==> DEBUG [MESSAGE]    : ";
        var prefixFile        = "\n==> DEBUG [FILE]       : ";
        var prefixError       = "\n==> DEBUG [ERROR]      : ";
        var prefixStack       = "\n==> DEBUG [STACK]      : ";
        var prefixArguments   = "\n==> DEBUG [ARGUMENTS]  : ";
        var prefixCaller      = "\n==> DEBUG [CALLER]";
        var prefixSource      = "\n==> DEBUG [SOURCE]\n";
        var prefixFunction = "\n------>";
        var regHasDwb = new RegExp("[^]*/\\*<dwb\\*/([^]*)/\\*dwb>\\*/[^]*");
        var prefixEditor    = "    ";
        var prefixHighlight = "--> ";
        var _formatLine = function(max, line) 
        {
                var size = max - Math.ceil(Math.log(line+1)/Math.log(10)) + 1; 
                return Array(size).join(" ") + line + " > ";
        };

        Object.defineProperties(io, {
            "debug" : 
            {
                value : function (params) 
                {
                    if (typeof params == "function" && this._arguments)
                        return params.bind(this);
                    var outMessage = new String();
                    params = params || {};
                    var offset = params.offset || 0;
                    var error, message;
                    var line = -1;
                    var showLine;
                    var caller, source;
                    var stack;

                    if (typeof params == "string")
                        message = params;
                    else if (params instanceof Error)
                        error = params;
                    else 
                    {
                        if (params.message) 
                            message = params.message;
                        if (params.error instanceof Error)
                            error = params.error;
                    }

                    if (this.path) 
                        outMessage += prefixFile + this.path;
                    if (message)
                        outMessage += prefixMessage + message;

                    if (error)
                    {
                        if (error.line || error.line === 0)
                            line = showLine = error.line;
                        else 
                            showLine = "?";
                        if (!error.stack) 
                        {
                            try 
                            {
                                throw new Error(error.message);
                            }
                            catch(e) 
                            { 
                                error = e;
                            }
                            offset += 1;
                        }
                        outMessage += prefixError + "Error in line " + showLine + ": " + error.message;
                        stack = "[" + error.stack.match(/[^\n]+/g).slice(offset).join("] [")+"]"; 
                    }
                    else 
                    {
                        try 
                        {
                            throw new Error();
                        }
                        catch(e) 
                        {
                            stack =  "[" + e.stack.match(/[^\n]+/g).slice(offset + 2).join("] [")+"]"; 
                        }
                    }
                    if (stack) 
                    {
                        outMessage += prefixStack + stack;
                    }

                    if (this._arguments && line >= 0)
                    {
                        caller = String(this._arguments.callee).replace(regHasDwb, "$1", "");
                        source = caller.split("\n");
                        var length = source.length;
                        var formatLine = _formatLine.bind(null, Math.ceil(Math.log(source.length+1)/Math.log(10)));

                        outMessage += prefixSource;
                        if (length >= line-2 && line-2 >= 0)
                        {
                            if (line > 2)
                                outMessage += prefixEditor + "...\n";
                            else 
                                outMessage += prefixEditor + "BOF\n";
                            outMessage += prefixEditor + formatLine(line-1) +  source[line-2] + "\n";
                        }
                        else 
                            outMessage += prefixEditor + "BOF\n";
                        if (length > line-1)
                            outMessage += prefixHighlight + formatLine(line) + source[line-1] + "\n";
                        if (length > line && length != line+1) 
                        {
                            outMessage += prefixEditor + formatLine(line+1) + source[line];
                            if (length > line + 2)
                                outMessage += "\n" + prefixEditor + "...";
                            else 
                                outMessage += "\n" + prefixEditor + "EOF";
                        }
                        else 
                            outMessage += prefixEditor + "EOF";
                    }
                    else if (params.arguments) 
                    {
                        outMessage += prefixArguments + JSON.stringify(params.arguments);
                        caller = String(params.arguments.callee.caller);
                        outMessage += prefixCaller;
                        outMessage += prefixFunction + "\n";
                        outMessage += caller.replace(regHasDwb, "$1").replace(/\n/gm, "\n  ");
                        outMessage += prefixFunction;
                    }
                    io.print(outMessage + "\n", "stderr");
                    return undefined;
                }

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