|
1 YUI.add('yui-log', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * Provides console log capability and exposes a custom event for |
|
5 * console implementations. This module is a `core` YUI module, |
|
6 * <a href="../classes/YUI.html#method_log">it's documentation is located under the YUI class</a>. |
|
7 * |
|
8 * @module yui |
|
9 * @submodule yui-log |
|
10 */ |
|
11 |
|
12 var INSTANCE = Y, |
|
13 LOGEVENT = 'yui:log', |
|
14 UNDEFINED = 'undefined', |
|
15 LEVELS = { debug: 1, |
|
16 info: 2, |
|
17 warn: 4, |
|
18 error: 8 }; |
|
19 |
|
20 /** |
|
21 * If the 'debug' config is true, a 'yui:log' event will be |
|
22 * dispatched, which the Console widget and anything else |
|
23 * can consume. If the 'useBrowserConsole' config is true, it will |
|
24 * write to the browser console if available. YUI-specific log |
|
25 * messages will only be present in the -debug versions of the |
|
26 * JS files. The build system is supposed to remove log statements |
|
27 * from the raw and minified versions of the files. |
|
28 * |
|
29 * @method log |
|
30 * @for YUI |
|
31 * @param {String} msg The message to log. |
|
32 * @param {String} cat The log category for the message. Default |
|
33 * categories are "info", "warn", "error", "debug". |
|
34 * Custom categories can be used as well. (opt). |
|
35 * @param {String} src The source of the the message (opt). |
|
36 * @param {boolean} silent If true, the log event won't fire. |
|
37 * @return {YUI} YUI instance. |
|
38 */ |
|
39 INSTANCE.log = function(msg, cat, src, silent) { |
|
40 var bail, excl, incl, m, f, minlevel, |
|
41 Y = INSTANCE, |
|
42 c = Y.config, |
|
43 publisher = (Y.fire) ? Y : YUI.Env.globalEvents; |
|
44 // suppress log message if the config is off or the event stack |
|
45 // or the event call stack contains a consumer of the yui:log event |
|
46 if (c.debug) { |
|
47 // apply source filters |
|
48 src = src || ""; |
|
49 if (typeof src !== "undefined") { |
|
50 excl = c.logExclude; |
|
51 incl = c.logInclude; |
|
52 if (incl && !(src in incl)) { |
|
53 bail = 1; |
|
54 } else if (incl && (src in incl)) { |
|
55 bail = !incl[src]; |
|
56 } else if (excl && (src in excl)) { |
|
57 bail = excl[src]; |
|
58 } |
|
59 |
|
60 // Set a default category of info if the category was not defined or was not |
|
61 // a real category. |
|
62 if ((typeof cat === 'undefined') || !(cat in LEVELS)) { |
|
63 cat = 'info'; |
|
64 } |
|
65 |
|
66 // Determine the current minlevel as defined in configuration |
|
67 Y.config.logLevel = Y.config.logLevel || 'debug'; |
|
68 minlevel = LEVELS[Y.config.logLevel.toLowerCase()]; |
|
69 |
|
70 if (cat in LEVELS && LEVELS[cat] < minlevel) { |
|
71 // Skip this message if the we don't meet the defined minlevel |
|
72 bail = 1; |
|
73 } |
|
74 } |
|
75 if (!bail) { |
|
76 if (c.useBrowserConsole) { |
|
77 m = (src) ? src + ': ' + msg : msg; |
|
78 if (Y.Lang.isFunction(c.logFn)) { |
|
79 c.logFn.call(Y, msg, cat, src); |
|
80 } else if (typeof console !== UNDEFINED && console.log) { |
|
81 f = (cat && console[cat] && (cat in LEVELS)) ? cat : 'log'; |
|
82 console[f](m); |
|
83 } else if (typeof opera !== UNDEFINED) { |
|
84 opera.postError(m); |
|
85 } |
|
86 } |
|
87 |
|
88 if (publisher && !silent) { |
|
89 |
|
90 if (publisher === Y && (!publisher.getEvent(LOGEVENT))) { |
|
91 publisher.publish(LOGEVENT, { |
|
92 broadcast: 2 |
|
93 }); |
|
94 } |
|
95 |
|
96 publisher.fire(LOGEVENT, { |
|
97 msg: msg, |
|
98 cat: cat, |
|
99 src: src |
|
100 }); |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 return Y; |
|
106 }; |
|
107 |
|
108 /** |
|
109 * Write a system message. This message will be preserved in the |
|
110 * minified and raw versions of the YUI files, unlike log statements. |
|
111 * @method message |
|
112 * @for YUI |
|
113 * @param {String} msg The message to log. |
|
114 * @param {String} cat The log category for the message. Default |
|
115 * categories are "info", "warn", "error", "debug". |
|
116 * Custom categories can be used as well. (opt). |
|
117 * @param {String} src The source of the the message (opt). |
|
118 * @param {boolean} silent If true, the log event won't fire. |
|
119 * @return {YUI} YUI instance. |
|
120 */ |
|
121 INSTANCE.message = function() { |
|
122 return INSTANCE.log.apply(INSTANCE, arguments); |
|
123 }; |
|
124 |
|
125 |
|
126 }, '@VERSION@', {"requires": ["yui-base"]}); |