|
1 YUI.add('yui-log-nodejs', function (Y, NAME) { |
|
2 |
|
3 var sys = require(process.binding('natives').util ? 'util' : 'sys'), |
|
4 hasColor = false; |
|
5 |
|
6 try { |
|
7 var stdio = require("stdio"); |
|
8 hasColor = stdio.isStderrATTY(); |
|
9 } catch (ex) { |
|
10 hasColor = true; |
|
11 } |
|
12 |
|
13 Y.config.useColor = hasColor; |
|
14 |
|
15 Y.consoleColor = function(str, num) { |
|
16 if (!this.config.useColor) { |
|
17 return str; |
|
18 } |
|
19 if (!num) { |
|
20 num = '32'; |
|
21 } |
|
22 return "\u001b[" + num +"m" + str + "\u001b[0m"; |
|
23 }; |
|
24 |
|
25 |
|
26 var logFn = function(str, t, m) { |
|
27 var id = '', lvl, mLvl; |
|
28 if (this.id) { |
|
29 id = '[' + this.id + ']:'; |
|
30 } |
|
31 t = t || 'info'; |
|
32 m = (m) ? this.consoleColor(' (' + m.toLowerCase() + '):', 35) : ''; |
|
33 |
|
34 if (str === null) { |
|
35 str = 'null'; |
|
36 } |
|
37 |
|
38 if ((typeof str === 'object') || str instanceof Array) { |
|
39 try { |
|
40 //Should we use this? |
|
41 if (str.tagName || str._yuid || str._query) { |
|
42 str = str.toString(); |
|
43 } else { |
|
44 str = sys.inspect(str); |
|
45 } |
|
46 } catch (e) { |
|
47 //Fail catcher |
|
48 } |
|
49 } |
|
50 |
|
51 lvl = '37;40'; |
|
52 mLvl = ((str) ? '' : 31); |
|
53 t = t+''; //Force to a string.. |
|
54 switch (t.toLowerCase()) { |
|
55 case 'error': |
|
56 lvl = mLvl = 31; |
|
57 break; |
|
58 case 'warn': |
|
59 lvl = 33; |
|
60 break; |
|
61 case 'debug': |
|
62 lvl = 34; |
|
63 break; |
|
64 } |
|
65 if (typeof str === 'string') { |
|
66 if (str && str.indexOf("\n") !== -1) { |
|
67 str = "\n" + str; |
|
68 } |
|
69 } |
|
70 |
|
71 // output log messages to stderr |
|
72 sys.error(this.consoleColor(t.toLowerCase() + ':', lvl) + m + ' ' + this.consoleColor(str, mLvl)); |
|
73 }; |
|
74 |
|
75 if (!Y.config.logFn) { |
|
76 Y.config.logFn = logFn; |
|
77 } |
|
78 |
|
79 |
|
80 |
|
81 }, '@VERSION@'); |