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