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