logger.js
8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
(function(window) {
'use strict';
var MODULE_NAME = 'screens/player/log/logger';
/**
* @typedef LEVEL
*/
var LEVEL = Object.freeze({
DISABLED: 0,
DEBUG: 1,
INFO: 2,
WARN: 3,
ERROR: 4
});
var key;
/**
* @typedef LEVEL_MAP
*/
var LEVEL_MAP = {};
for (key in LEVEL) {
LEVEL_MAP[LEVEL[key]] = key;
}
/**
* console function mapping for levels
* @private
* @type {Object}
*/
var CONSOLE_FN = {};
for (key in LEVEL) {
CONSOLE_FN[LEVEL[key]] = key.toLowerCase();
}
/**
* @type {Logger}
* @private
*/
var _catchExceptionsLogger = null;
/**
* @type {Logger}
* @private
*/
var _patchConsoleLogger = null;
/**
* Indicates if the console object was patched
* @type {boolean}
* @private
*/
var _isPatched = false;
/**
* Indicates if global exceptions are caught
* @type {boolean}
* @private
*/
var _catchExceptions = false;
/**
* A potentially existing window.onerror handler
* @type {boolean}
* @private
*/
var _catchExceptionsOnErrorHandler = null;
/**
* The global catch exception error handler
* @param {Event} ev The event
* @private
*/
var _catchExceptionErrorHandler = function(ev) {
var message = ev.message ? ev.message : 'Error during loading ' + ev.srcElement.src;
_catchExceptionsLogger.error(message, ev.source || '', ev.lineno || 0, ev.colno || 0, ev);
};
/**
*
* @type {object}
* @private
*/
var _originalConsoleFn = null;
/**
* Logger
*
* @classdesc Logger
* @class Logger
* @param {String} [name] of the logger (visible in the output)
* @param {Logger.Writer} [writer] writer to process logs
*/
function Logger(name, writer) {
this.name = name;
this._logLevel = LEVEL.DEBUG;
this._print = false;
this._writer = writer || {
write: function(date, level, message) {
// nothing to do
}
};
}
/**
* Retrieves the console object
* @returns {Object} The console object
* @private
*/
function getConsole() {
if (_originalConsoleFn) {
return _originalConsoleFn;
}
return window.console;
}
/**
* Log the specified message.
* @param {Logger} logger The logger
* @param {Logger.LEVEL} level The log level
* @param {Object} args data to log
* @private
*/
function log(logger, level, args) {
var argsArr = Array.prototype.slice.call(args);
var message = logger.name ? '[' + logger.name + '] ' : '';
message += argsArr.join(', '); // @todo sprintf
if (logger._logLevel !== LEVEL.DISABLED && level >= logger._logLevel) {
logger._writer.write(Date.now(), level, message);
if (logger._print) {
var console = getConsole();
var fn = console[CONSOLE_FN[level]];
fn = fn || console.log;
fn.apply(window.console, args);
}
}
}
/**
* setting this to true will override
* the functions of the default console object
* which means all code using console.log(), console.debug(),
* console.info(), console.warn(), console.error(), console.trace()
* will automatically end up writing into the logs
* @param {Logger|false} [logger] Logger to write the console logs or false to deactivate
* @static
*/
Logger.patchConsole = function(logger) {
var willBePatched = logger === false ? false : true;
if (!_isPatched && willBePatched) { // enable console patch
_patchConsoleLogger = logger;
_originalConsoleFn = {
log: console.log,
debug: console.debug,
info: console.info,
warn: console.warn,
error: console.error,
trace: console.trace
};
// output is now disabled if print() is not set
console.log = _patchConsoleLogger.log.bind(_patchConsoleLogger);
console.debug = _patchConsoleLogger.debug.bind(_patchConsoleLogger);
console.info = _patchConsoleLogger.info.bind(_patchConsoleLogger);
console.warn = _patchConsoleLogger.warn.bind(_patchConsoleLogger);
console.error = _patchConsoleLogger.error.bind(_patchConsoleLogger);
console.trace = _patchConsoleLogger.trace.bind(_patchConsoleLogger);
} else if (_isPatched && !willBePatched) { // disable the previous patching
_patchConsoleLogger = null;
console.log = _originalConsoleFn.log;
console.debug = _originalConsoleFn.debug;
console.info = _originalConsoleFn.info;
console.warn = _originalConsoleFn.warn;
console.error = _originalConsoleFn.error;
console.trace = _originalConsoleFn.trace;
_originalConsoleFn = null;
}
_isPatched = willBePatched;
};
/**
* setting this to true will catch all uncaught exceptions
* and add them to the logs as error.
* @param {Logger|false} [logger] Logger to write the exceptions logs
* @static
*/
Logger.catchExceptions = function(logger) { // considered as LEVEL.ERROR
var willCatchExceptions = logger === false ? false : true;
if (!_catchExceptions && willCatchExceptions) {
_catchExceptionsLogger = logger;
window.addEventListener('error', _catchExceptionErrorHandler, true);
// Overwrite potential existing error handler
_catchExceptionsOnErrorHandler = window.onerror;
window.onerror = null;
} else {
_catchExceptionsLogger = null;
window.removeEventListener('error', _catchExceptionErrorHandler);
window.onerror = _catchExceptionsOnErrorHandler;
}
_catchExceptions = willCatchExceptions;
};
/**
* Sets the writer
* @param {Object} writer the writer to use
* @returns {Logger} the logger to allow for easy chaining
*/
Logger.prototype.setWriter = function(writer) {
this._writer = writer;
return this;
};
/**
* Sets the log level
* @param {Logger.LEVEL} level the log level
* @returns {Logger} the logger to allow for easy chaining
*/
Logger.prototype.setLevel = function(level) {
this._logLevel = level;
return this;
};
/**
* Setting this true will output the set log level
* messages into the browser's console
* @param {boolean} [cond] whether to print the logs or not
*/
Logger.prototype.print = function(cond) {
this._print = cond === false ? false : true;
};
Logger.prototype.log = function() {
log(this, LEVEL.DEBUG, arguments);
};
Logger.prototype.debug = function() {
log(this, LEVEL.DEBUG, arguments);
};
Logger.prototype.info = function() {
log(this, LEVEL.INFO, arguments);
};
Logger.prototype.warn = function() {
log(this, LEVEL.WARN, arguments);
};
Logger.prototype.error = function() {
log(this, LEVEL.ERROR, arguments);
};
Logger.prototype.trace = function() {
var e = new Error('tracer');
var stack;
if (e.stack) {
stack = e.stack.replace(/^[^\(]+?[\n$]/gm, '')
.replace(/^\s+at\s+/gm, '')
.replace(/^Object.<anonymous>\s*\(/gm, '{anonymous}()@')
.split('\n');
}
log(this, LEVEL.DEBUG, [stack]);
};
// expose constants
Logger.LEVEL = LEVEL;
Logger.LEVEL_MAP = LEVEL_MAP;
// register as a requirejs module if available
/* istanbul ignore next */
if (typeof define === 'function') {
define(MODULE_NAME, function() {
return Logger;
});
}
// expose on window for non require-js frames
// e.g. channel frame
window.screens = window.screens || {};
window.screens.logger = Logger;
}(this));