diff options
Diffstat (limited to 'node_modules/montage-user/core/logger.js')
-rwxr-xr-x | node_modules/montage-user/core/logger.js | 497 |
1 files changed, 497 insertions, 0 deletions
diff --git a/node_modules/montage-user/core/logger.js b/node_modules/montage-user/core/logger.js new file mode 100755 index 00000000..6f90fab3 --- /dev/null +++ b/node_modules/montage-user/core/logger.js | |||
@@ -0,0 +1,497 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | /** | ||
7 | @module montage/core/logger | ||
8 | @requires montage/core/core | ||
9 | */ | ||
10 | var Montage = require("montage").Montage, | ||
11 | Logger, | ||
12 | loggers, | ||
13 | consoleLog, | ||
14 | consoleLogMontage, | ||
15 | emptyLoggerFunction, | ||
16 | getFunctionName, | ||
17 | toTimeString, | ||
18 | LoggerUI, | ||
19 | localStorage; | ||
20 | |||
21 | loggers = exports.loggers = {}; | ||
22 | |||
23 | /** | ||
24 | @function | ||
25 | @param {Object} montageObject TODO | ||
26 | @returns aFunctionName | ||
27 | @private | ||
28 | */ | ||
29 | getFunctionName = function(montageObject) { | ||
30 | var aCaller = getFunctionName.caller.caller, | ||
31 | aFunctionName; | ||
32 | aFunctionName = aCaller.name; | ||
33 | if (aFunctionName === "") { | ||
34 | aFunctionName = "anonymous"; | ||
35 | } | ||
36 | return aFunctionName; | ||
37 | }; | ||
38 | |||
39 | /** | ||
40 | @function | ||
41 | @param {Date} date TODO | ||
42 | @returns length hours, minutes, seconds, date.getMilliseconds() | ||
43 | @private | ||
44 | */ | ||
45 | toTimeString = function(date) { | ||
46 | if (date.getHours) { | ||
47 | var hours = date.getHours(), | ||
48 | mins = date.getMinutes(), | ||
49 | secs = date.getSeconds(); | ||
50 | return (hours.length === 1 ? "0" + hours : hours) + ":" + (mins.length === 1 ? "0" + mins : mins) + ":" + (secs.length === 1 ? "0" + secs : secs) + "." + date.getMilliseconds(); | ||
51 | } | ||
52 | }; | ||
53 | /** | ||
54 | @function | ||
55 | @private | ||
56 | */ | ||
57 | emptyLoggerFunction = function() { | ||
58 | }; | ||
59 | /** | ||
60 | @function | ||
61 | @private | ||
62 | */ | ||
63 | consoleLog = function() { | ||
64 | console.log(arguments); | ||
65 | }; | ||
66 | /** | ||
67 | @function | ||
68 | @private | ||
69 | */ | ||
70 | consoleLogMontage = function() { | ||
71 | var firstArgument = arguments[0], | ||
72 | metadata = firstArgument._montage_metadata, | ||
73 | now = new Date(); | ||
74 | //[].unshift.call(arguments, toTimeString(now)); | ||
75 | if (metadata) { | ||
76 | [].shift.call(arguments); | ||
77 | [].unshift.call(arguments, metadata.objectName + "." + getFunctionName(firstArgument) + "()"); | ||
78 | if (this.buffered) { | ||
79 | this.buffer.push(arguments); | ||
80 | } else { | ||
81 | console.debug.apply(console, arguments); | ||
82 | } | ||
83 | } else { | ||
84 | if (this.buffered) { | ||
85 | this.buffer.push(arguments); | ||
86 | } else { | ||
87 | console.debug.apply(console, arguments); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | }; | ||
92 | |||
93 | /** | ||
94 | @class module:montage/core/logger.Logger | ||
95 | @extends module:montage/core/core.Montage | ||
96 | */ | ||
97 | Logger = exports.Logger = Montage.create(Montage,/** @lends module:montage/core/logger.Logger# */ { | ||
98 | /** | ||
99 | @function | ||
100 | @param {String} name The name to be logged. | ||
101 | @param {State} dontStoreState The state in which the name is to be stored. | ||
102 | @returns itself | ||
103 | */ | ||
104 | init: { | ||
105 | value: function(name, dontStoreState) { | ||
106 | this.name = name; | ||
107 | this._storeState = !dontStoreState; | ||
108 | if (this._storeState && localStorage) { | ||
109 | var storedState = localStorage.getItem("_montage_logger_" + name); | ||
110 | if (storedState) { | ||
111 | this.isDebug = storedState === "true"; | ||
112 | } | ||
113 | } | ||
114 | this.isError = true; | ||
115 | return this; | ||
116 | } | ||
117 | }, | ||
118 | /** | ||
119 | @type {Property} | ||
120 | @default {String} null | ||
121 | */ | ||
122 | name: { | ||
123 | value: null | ||
124 | }, | ||
125 | /** | ||
126 | @type {Property} | ||
127 | @default {Array} [] | ||
128 | */ | ||
129 | buffer: { | ||
130 | value: [], | ||
131 | distinct: true | ||
132 | }, | ||
133 | /** | ||
134 | @type {Property} | ||
135 | @default {Boolean} false | ||
136 | */ | ||
137 | buffered: { | ||
138 | value: false | ||
139 | }, | ||
140 | /** | ||
141 | @function | ||
142 | */ | ||
143 | flush: { | ||
144 | value: function() { | ||
145 | var buffer = this.buffer, | ||
146 | args, | ||
147 | i; | ||
148 | for (i = 0; (args = buffer[i]); i++) { | ||
149 | console.debug.apply(console, args); | ||
150 | } | ||
151 | } | ||
152 | }, | ||
153 | /** | ||
154 | @type {Function} | ||
155 | */ | ||
156 | isDebug: { | ||
157 | get: function() { | ||
158 | return this.debug !== emptyLoggerFunction; | ||
159 | }, | ||
160 | set: function(value) { | ||
161 | if (value) { | ||
162 | this.debug = consoleLogMontage; | ||
163 | } else { | ||
164 | this.debug = emptyLoggerFunction; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | }, | ||
169 | /** | ||
170 | @type {Function} | ||
171 | */ | ||
172 | isError: { | ||
173 | get: function() { | ||
174 | return this.error !== emptyLoggerFunction; | ||
175 | }, | ||
176 | set: function(value) { | ||
177 | if (value) { | ||
178 | this.error = consoleLogMontage; | ||
179 | } else { | ||
180 | this.error = emptyLoggerFunction; | ||
181 | } | ||
182 | } | ||
183 | }, | ||
184 | /** | ||
185 | @type {Property} | ||
186 | @default {Function} emptyLoggerFunction | ||
187 | */ | ||
188 | debug: { | ||
189 | value: emptyLoggerFunction | ||
190 | }, | ||
191 | /** | ||
192 | @type {Property} | ||
193 | @default {Function} emptyLoggerFunction | ||
194 | */ | ||
195 | error: { | ||
196 | value: emptyLoggerFunction | ||
197 | }, | ||
198 | /** | ||
199 | @type {Property} | ||
200 | @default {Function} toTimeString | ||
201 | */ | ||
202 | toTimeString: { | ||
203 | value: toTimeString | ||
204 | }, | ||
205 | /** | ||
206 | @private | ||
207 | */ | ||
208 | _storeState: { | ||
209 | value: null | ||
210 | } | ||
211 | }); | ||
212 | |||
213 | /** | ||
214 | @function module:montage/core/logger.#logger | ||
215 | */ | ||
216 | exports.logger = function(loggerName, dontStoreState) { | ||
217 | var logger; | ||
218 | if ((logger = loggers[loggerName]) == null) { | ||
219 | logger = Montage.create(Logger).init(loggerName, dontStoreState); | ||
220 | Montage.defineProperty(loggers, loggerName, { | ||
221 | value: logger | ||
222 | }); | ||
223 | } | ||
224 | return logger; | ||
225 | }; | ||
226 | |||
227 | /** | ||
228 | @class module:montage/core/logger.LoggerUI | ||
229 | */ | ||
230 | LoggerUI = Montage.create(Montage, /** @lends module:montage/core/logger.LoggerUI# */{ | ||
231 | /** | ||
232 | @function | ||
233 | @returns itself | ||
234 | */ | ||
235 | init: { | ||
236 | value: function() { | ||
237 | if (document.nativeAddEventListener) { | ||
238 | document.nativeAddEventListener("keyup", this, false); | ||
239 | document.nativeAddEventListener("keydown", this, false); | ||
240 | } else { | ||