source: Dev/trunk/d3/lib/env-js/envjs/console.js @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 5.6 KB
Line 
1
2/**
3 * @author envjs team
4 * @Console
5 */
6
7var Envjs = Envjs || require('./platform/core').Envjs;
8
9/*
10 * Envjs console.1.3.pre03
11 * Pure JavaScript Browser Environment
12 * By John Resig <http://ejohn.org/> and the Envjs Team
13 * Copyright 2008-2010 John Resig, under the MIT License
14 */
15
16//CLOSURE_START
17(function(){
18
19
20
21
22/**
23 * @author envjs team
24 * borrowed 99%-ish with love from firebug-lite
25 */
26
27//leaked globally on purpose;
28try{
29    console.log();
30}catch(e){
31
32
33function escapeHTML(value)
34{
35    return value;
36}
37
38function objectToString(object)
39{
40    try
41    {
42        return object+"";
43    }
44    catch (exc)
45    {
46        return null;
47    }
48}
49
50// ********************************************************************************************
51
52function appendText(object, html)
53{
54    html.push(escapeHTML(objectToString(object)));
55}
56
57function appendNull(object, html)
58{
59    html.push(escapeHTML(objectToString(object)));
60}
61
62function appendString(object, html)
63{
64    html.push(escapeHTML(objectToString(object)));
65}
66
67function appendInteger(object, html)
68{
69    html.push(escapeHTML(objectToString(object)));
70}
71
72function appendFloat(object, html)
73{
74    html.push(escapeHTML(objectToString(object)));
75}
76
77function appendFunction(object, html)
78{
79    var reName = /function ?(.*?)\(/;
80    var m = reName.exec(objectToString(object));
81    var name = m ? m[1] : "function";
82    html.push(escapeHTML(name));
83}
84
85function appendObjectFormatted(object, html)
86{
87    var text = objectToString(object);
88    var reObject = /\[object (.*?)\]/;
89
90    var m = reObject.exec(text);
91    html.push( m ? m[1] : text);
92}
93
94function appendSelector(object, html)
95{
96
97    html.push(escapeHTML(object.nodeName.toLowerCase()));
98    if (object.id) {
99        html.push(escapeHTML(object.id));
100    }
101    if (object.className) {
102        html.push(escapeHTML(object.className));
103    }
104}
105
106function appendNode(node, html)
107{
108    if (node.nodeType == 1)
109    {
110        html.push( node.nodeName.toLowerCase());
111
112        for (var i = 0; i < node.attributes.length; ++i)
113        {
114            var attr = node.attributes[i];
115            if (!attr.specified) {
116                continue;
117        }
118
119            html.push( attr.nodeName.toLowerCase(),escapeHTML(attr.nodeValue));
120        }
121
122        if (node.firstChild)
123        {
124            for (var child = node.firstChild; child; child = child.nextSibling) {
125                appendNode(child, html);
126        }
127
128            html.push( node.nodeName.toLowerCase());
129        }
130    }
131    else if (node.nodeType === 3)
132    {
133        html.push(escapeHTML(node.nodeValue));
134    }
135}
136
137function appendObject(object, html)
138{
139    try
140    {
141        if (object === undefined) {
142            appendNull("undefined", html);
143        } else if (object === null) {
144            appendNull("null", html);
145        } else if (typeof object == "string") {
146            appendString(object, html);
147    } else if (typeof object == "number") {
148            appendInteger(object, html);
149    } else if (typeof object == "function") {
150            appendFunction(object, html);
151        } else if (object.nodeType == 1) {
152            appendSelector(object, html);
153        } else if (typeof object == "object") {
154            appendObjectFormatted(object, html);
155        } else {
156            appendText(object, html);
157    }
158    }
159    catch (exc)
160    {
161    }
162}
163
164
165function parseFormat(format)
166{
167    var parts = [];
168
169    var reg = /((^%|[^\\]%)(\d+)?(\.)([a-zA-Z]))|((^%|[^\\]%)([a-zA-Z]))/;
170    var appenderMap = {s: appendText, d: appendInteger, i: appendInteger, f: appendFloat};
171
172    for (var m = reg.exec(format); m; m = reg.exec(format))
173    {
174        var type = m[8] ? m[8] : m[5];
175        var appender = type in appenderMap ? appenderMap[type] : appendObject;
176        var precision = m[3] ? parseInt(m[3], 10) : (m[4] == "." ? -1 : 0);
177
178        parts.push(format.substr(0, m[0][0] == "%" ? m.index : m.index+1));
179        parts.push({appender: appender, precision: precision});
180
181        format = format.substr(m.index+m[0].length);
182    }
183
184    parts.push(format);
185
186    return parts;
187}
188
189
190
191function logFormatted(objects, className)
192{
193    var html = [],
194        i= 0,
195        object;
196
197    var format = objects[0];
198    var objIndex = 0;
199
200    if (typeof(format) != "string")
201    {
202        format = "";
203        objIndex = -1;
204    }
205
206    var parts = parseFormat(format);
207    for (i = 0; i < parts.length; ++i)
208    {
209        var part = parts[i];
210        if (part && typeof(part) == "object")
211        {
212            object = objects[++objIndex];
213            part.appender(object, html);
214        }
215        else {
216            appendText(part, html);
217        }
218    }
219
220    for (i = objIndex+1; i < objects.length; ++i)
221    {
222        appendText(" ", html);
223
224        object = objects[i];
225        if (typeof(object) == "string") {
226            appendText(object, html);
227        } else {
228            appendObject(object, html);
229        }
230    }
231
232    Envjs.log(html.join(' '));
233}
234
235
236Console = function(module){
237    var $level,
238        $logger,
239        $null = function(){};
240    $logger = {
241        log: function(level){
242            logFormatted(arguments, "");
243        },
244        debug: function(level){
245            logFormatted(arguments, "DEBUG");
246        },
247        info:  function(level){
248            logFormatted(arguments, "INFO");
249        },
250        warn:  function(level){
251            logFormatted(arguments, "WARN");
252        },
253        error:  function(level){
254            logFormatted(arguments, "ERROR");
255        },
256        trace: function(){
257            Envjs.trace();
258        }
259    };
260
261    return $logger;
262};
263
264console = new Console();
265exports.console = console;
266
267}
268
269
270
271/**
272 * @author john resig & the envjs team
273 * @uri http://www.envjs.com/
274 * @copyright 2008-2010
275 * @license MIT
276 */
277//CLOSURE_END
278}());
Note: See TracBrowser for help on using the repository browser.