3 // Copyright (c) 2014 Tom Perry
\r
5 // Permission is hereby granted, free of charge, to any person obtaining
\r
6 // a copy of this software and associated documentation files (the
\r
7 // "Software"), to deal in the Software without restriction, including
\r
8 // without limitation the rights to use, copy, modify, merge, publish,
\r
9 // distribute, sublicense, and/or sell copies of the Software, and to
\r
10 // permit persons to whom the Software is furnished to do so, subject to
\r
11 // the following conditions:
\r
13 // The above copyright notice and this permission notice shall be
\r
14 // included in all copies or substantial portions of the Software.
\r
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
\r
19 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
\r
20 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
\r
21 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
\r
22 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
25 // Description of import into Moodle:
\r
26 // Download from https://github.com/pimterry/loglevel/releases
\r
27 // Copy loglevel.js into lib/amd/src/ in Moodle folder.
\r
28 // Add the license as a comment to the file and these instructions.
\r
29 // Add the jshint ignore:start and ignore:end comments.
\r
30 // Delete the jshint validthis:true comments.
\r
32 /* jshint ignore:start */
\r
33 /*! loglevel - v1.4.0 - https://github.com/pimterry/loglevel - (c) 2015 Tim Perry - licensed MIT */
\r
34 (function (root, definition) {
\r
36 if (typeof module === 'object' && module.exports && typeof require === 'function') {
\r
37 module.exports = definition();
\r
38 } else if (typeof define === 'function' && typeof define.amd === 'object') {
\r
41 root.log = definition();
\r
43 }(this, function () {
\r
45 var noop = function() {};
\r
46 var undefinedType = "undefined";
\r
48 function realMethod(methodName) {
\r
49 if (typeof console === undefinedType) {
\r
50 return false; // We can't build a real method without a console to log to
\r
51 } else if (console[methodName] !== undefined) {
\r
52 return bindMethod(console, methodName);
\r
53 } else if (console.log !== undefined) {
\r
54 return bindMethod(console, 'log');
\r
60 function bindMethod(obj, methodName) {
\r
61 var method = obj[methodName];
\r
62 if (typeof method.bind === 'function') {
\r
63 return method.bind(obj);
\r
66 return Function.prototype.bind.call(method, obj);
\r
68 // Missing bind shim or IE8 + Modernizr, fallback to wrapping
\r
70 return Function.prototype.apply.apply(method, [obj, arguments]);
\r
76 // these private functions always need `this` to be set properly
\r
78 function enableLoggingWhenConsoleArrives(methodName, level, loggerName) {
\r
79 return function () {
\r
80 if (typeof console !== undefinedType) {
\r
81 replaceLoggingMethods.call(this, level, loggerName);
\r
82 this[methodName].apply(this, arguments);
\r
87 function replaceLoggingMethods(level, loggerName) {
\r
88 for (var i = 0; i < logMethods.length; i++) {
\r
89 var methodName = logMethods[i];
\r
90 this[methodName] = (i < level) ?
\r
92 this.methodFactory(methodName, level, loggerName);
\r
96 function defaultMethodFactory(methodName, level, loggerName) {
\r
97 return realMethod(methodName) ||
\r
98 enableLoggingWhenConsoleArrives.apply(this, arguments);
\r
109 function Logger(name, defaultLevel, factory) {
\r
112 var storageKey = "loglevel";
\r
114 storageKey += ":" + name;
\r
117 function persistLevelIfPossible(levelNum) {
\r
118 var levelName = (logMethods[levelNum] || 'silent').toUpperCase();
\r
120 // Use localStorage if available
\r
122 window.localStorage[storageKey] = levelName;
\r
124 } catch (ignore) {}
\r
126 // Use session cookie as fallback
\r
128 window.document.cookie =
\r
129 encodeURIComponent(storageKey) + "=" + levelName + ";";
\r
130 } catch (ignore) {}
\r
133 function getPersistedLevel() {
\r
137 storedLevel = window.localStorage[storageKey];
\r
138 } catch (ignore) {}
\r
140 if (typeof storedLevel === undefinedType) {
\r
142 var cookie = window.document.cookie;
\r
143 var location = cookie.indexOf(
\r
144 encodeURIComponent(storageKey) + "=");
\r
146 storedLevel = /^([^;]+)/.exec(cookie.slice(location))[1];
\r
148 } catch (ignore) {}
\r
151 // If the stored level is not valid, treat it as if nothing was stored.
\r
152 if (self.levels[storedLevel] === undefined) {
\r
153 storedLevel = undefined;
\r
156 return storedLevel;
\r
165 self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3,
\r
166 "ERROR": 4, "SILENT": 5};
\r
168 self.methodFactory = factory || defaultMethodFactory;
\r
170 self.getLevel = function () {
\r
171 return currentLevel;
\r
174 self.setLevel = function (level, persist) {
\r
175 if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
\r
176 level = self.levels[level.toUpperCase()];
\r
178 if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
\r
179 currentLevel = level;
\r
180 if (persist !== false) { // defaults to true
\r
181 persistLevelIfPossible(level);
\r
183 replaceLoggingMethods.call(self, level, name);
\r
184 if (typeof console === undefinedType && level < self.levels.SILENT) {
\r
185 return "No console available for logging";
\r
188 throw "log.setLevel() called with invalid level: " + level;
\r
192 self.setDefaultLevel = function (level) {
\r
193 if (!getPersistedLevel()) {
\r
194 self.setLevel(level, false);
\r
198 self.enableAll = function(persist) {
\r
199 self.setLevel(self.levels.TRACE, persist);
\r
202 self.disableAll = function(persist) {
\r
203 self.setLevel(self.levels.SILENT, persist);
\r
206 // Initialize with the right level
\r
207 var initialLevel = getPersistedLevel();
\r
208 if (initialLevel == null) {
\r
209 initialLevel = defaultLevel == null ? "WARN" : defaultLevel;
\r
211 self.setLevel(initialLevel, false);
\r
216 * Package-level API
\r
220 var defaultLogger = new Logger();
\r
222 var _loggersByName = {};
\r
223 defaultLogger.getLogger = function getLogger(name) {
\r
224 if (typeof name !== "string" || name === "") {
\r
225 throw new TypeError("You must supply a name when creating a logger.");
\r
228 var logger = _loggersByName[name];
\r
230 logger = _loggersByName[name] = new Logger(
\r
231 name, defaultLogger.getLevel(), defaultLogger.methodFactory);
\r
236 // Grab the current global log variable in case of overwrite
\r
237 var _log = (typeof window !== undefinedType) ? window.log : undefined;
\r
238 defaultLogger.noConflict = function() {
\r
239 if (typeof window !== undefinedType &&
\r
240 window.log === defaultLogger) {
\r
244 return defaultLogger;
\r
247 return defaultLogger;
\r
249 /* jshint ignore:end */
\r