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