Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 20... Línea 20...
20
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
// OTHER DEALINGS IN THE SOFTWARE.
22
// OTHER DEALINGS IN THE SOFTWARE.
Línea 23... Línea 23...
23
 
23
 
24
// Description of import into Moodle:
24
// Description of import into Moodle:
25
// Download from https://github.com/pimterry/loglevel/tree/master/dist
25
// Download from https://github.com/pimterry/loglevel/tree/main/dist
26
// Copy loglevel.js into lib/amd/src/ in Moodle folder.
26
// Copy loglevel.js into lib/amd/src/ in Moodle folder, and use grunt to build the code
Línea 27... Línea 27...
27
// Add the license as a comment to the file and these instructions.
27
// Add the license as a comment to the file and these instructions.
28
 
28
 
29
/*! loglevel - v1.8.1 - https://github.com/pimterry/loglevel - (c) 2022 Tim Perry - licensed MIT */
29
/*! loglevel - v1.9.2 - https://github.com/pimterry/loglevel - (c) 2024 Tim Perry - licensed MIT */
30
(function (root, definition) {
30
(function (root, definition) {
31
    "use strict";
31
    "use strict";
32
    if (typeof define === 'function' && define.amd) {
32
    if (typeof define === 'function' && define.amd) {
Línea 52... Línea 52...
52
        "info",
52
        "info",
53
        "warn",
53
        "warn",
54
        "error"
54
        "error"
55
    ];
55
    ];
Línea -... Línea 56...
-
 
56
 
-
 
57
    var _loggersByName = {};
-
 
58
    var defaultLogger = null;
56
 
59
 
57
    // Cross-browser bind equivalent that works at least back to IE6
60
    // Cross-browser bind equivalent that works at least back to IE6
58
    function bindMethod(obj, methodName) {
61
    function bindMethod(obj, methodName) {
59
        var method = obj[methodName];
62
        var method = obj[methodName];
60
        if (typeof method.bind === 'function') {
63
        if (typeof method.bind === 'function') {
Línea 104... Línea 107...
104
        }
107
        }
105
    }
108
    }
Línea 106... Línea 109...
106
 
109
 
Línea 107... Línea 110...
107
    // These private functions always need `this` to be set properly
110
    // These private functions always need `this` to be set properly
108
 
111
 
-
 
112
    function replaceLoggingMethods() {
-
 
113
        /*jshint validthis:true */
-
 
114
        var level = this.getLevel();
109
    function replaceLoggingMethods(level, loggerName) {
115
 
110
        /*jshint validthis:true */
116
        // Replace the actual methods.
111
        for (var i = 0; i < logMethods.length; i++) {
117
        for (var i = 0; i < logMethods.length; i++) {
112
            var methodName = logMethods[i];
118
            var methodName = logMethods[i];
113
            this[methodName] = (i < level) ?
119
            this[methodName] = (i < level) ?
114
                noop :
120
                noop :
Línea 115... Línea 121...
115
                this.methodFactory(methodName, level, loggerName);
121
                this.methodFactory(methodName, level, this.name);
116
        }
122
        }
-
 
123
 
-
 
124
        // Define log.log as an alias for log.debug
-
 
125
        this.log = this.debug;
-
 
126
 
-
 
127
        // Return any important warnings.
117
 
128
        if (typeof console === undefinedType && level < this.levels.SILENT) {
Línea 118... Línea 129...
118
        // Define log.log as an alias for log.debug
129
            return "No console available for logging";
119
        this.log = this.debug;
130
        }
120
    }
131
    }
121
 
132
 
122
    // In old IE versions, the console isn't present until you first open it.
133
    // In old IE versions, the console isn't present until you first open it.
123
    // We build realMethod() replacements here that regenerate logging methods
134
    // We build realMethod() replacements here that regenerate logging methods
124
    function enableLoggingWhenConsoleArrives(methodName, level, loggerName) {
135
    function enableLoggingWhenConsoleArrives(methodName) {
125
        return function () {
136
        return function () {
126
            if (typeof console !== undefinedType) {
137
            if (typeof console !== undefinedType) {
127
                replaceLoggingMethods.call(this, level, loggerName);
138
                replaceLoggingMethods.call(this);
Línea 128... Línea 139...
128
                this[methodName].apply(this, arguments);
139
                this[methodName].apply(this, arguments);
129
            }
140
            }
130
        };
141
        };
131
    }
142
    }
132
 
143
 
133
    // By default, we use closely bound real methods wherever possible, and
144
    // By default, we use closely bound real methods wherever possible, and
134
    // otherwise we wait for a console to appear, and then try again.
145
    // otherwise we wait for a console to appear, and then try again.
Línea 135... Línea 146...
135
    function defaultMethodFactory(methodName, level, loggerName) {
146
    function defaultMethodFactory(methodName, _level, _loggerName) {
-
 
147
        /*jshint validthis:true */
136
        /*jshint validthis:true */
148
        return realMethod(methodName) ||
-
 
149
               enableLoggingWhenConsoleArrives.apply(this, arguments);
-
 
150
    }
-
 
151
 
-
 
152
    function Logger(name, factory) {
-
 
153
      // Private instance variables.
-
 
154
      var self = this;
-
 
155
      /**
-
 
156
       * The level inherited from a parent logger (or a global default). We
-
 
157
       * cache this here rather than delegating to the parent so that it stays
-
 
158
       * in sync with the actual logging methods that we have installed (the
-
 
159
       * parent could change levels but we might not have rebuilt the loggers
-
 
160
       * in this child yet).
-
 
161
       * @type {number}
-
 
162
       */
137
        return realMethod(methodName) ||
163
      var inheritedLevel;
-
 
164
      /**
138
               enableLoggingWhenConsoleArrives.apply(this, arguments);
165
       * The default level for this logger, if any. If set, this overrides
-
 
166
       * `inheritedLevel`.
-
 
167
       * @type {number|null}
-
 
168
       */
-
 
169
      var defaultLevel;
Línea 139... Línea 170...
139
    }
170
      /**
140
 
171
       * A user-specific level for this logger. If set, this overrides
141
    function Logger(name, defaultLevel, factory) {
172
       * `defaultLevel`.
142
      var self = this;
173
       * @type {number|null}
Línea 179... Línea 210...
179
 
210
 
180
          // Fallback to cookies if local storage gives us nothing
211
          // Fallback to cookies if local storage gives us nothing
181
          if (typeof storedLevel === undefinedType) {
212
          if (typeof storedLevel === undefinedType) {
182
              try {
213
              try {
183
                  var cookie = window.document.cookie;
214
                  var cookie = window.document.cookie;
184
                  var location = cookie.indexOf(
215
                  var cookieName = encodeURIComponent(storageKey);
185
                      encodeURIComponent(storageKey) + "=");
216
                  var location = cookie.indexOf(cookieName + "=");
186
                  if (location !== -1) {
217
                  if (location !== -1) {
-
 
218
                      storedLevel = /^([^;]+)/.exec(
-
 
219
                          cookie.slice(location + cookieName.length + 1)
187
                      storedLevel = /^([^;]+)/.exec(cookie.slice(location))[1];
220
                      )[1];
188
                  }
221
                  }
189
              } catch (ignore) {}
222
              } catch (ignore) {}
Línea 190... Línea 223...
190
          }
223
          }
Línea 201... Línea 234...
201
          if (typeof window === undefinedType || !storageKey) return;
234
          if (typeof window === undefinedType || !storageKey) return;
Línea 202... Línea 235...
202
 
235
 
203
          // Use localStorage if available
236
          // Use localStorage if available
204
          try {
237
          try {
205
              window.localStorage.removeItem(storageKey);
-
 
206
              return;
238
              window.localStorage.removeItem(storageKey);
Línea 207... Línea 239...
207
          } catch (ignore) {}
239
          } catch (ignore) {}
208
 
240
 
209
          // Use session cookie as fallback
241
          // Use session cookie as fallback
210
          try {
242
          try {
211
              window.document.cookie =
243
              window.document.cookie =
212
                encodeURIComponent(storageKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
244
                encodeURIComponent(storageKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
Línea -... Línea 245...
-
 
245
          } catch (ignore) {}
-
 
246
      }
-
 
247
 
-
 
248
      function normalizeLevel(input) {
-
 
249
          var level = input;
-
 
250
          if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
-
 
251
              level = self.levels[level.toUpperCase()];
-
 
252
          }
-
 
253
          if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
-
 
254
              return level;
-
 
255
          } else {
-
 
256
              throw new TypeError("log.setLevel() called with invalid level: " + input);
213
          } catch (ignore) {}
257
          }
214
      }
258
      }
215
 
259
 
216
      /*
260
      /*
217
       *
261
       *
Línea 225... Línea 269...
225
          "ERROR": 4, "SILENT": 5};
269
          "ERROR": 4, "SILENT": 5};
Línea 226... Línea 270...
226
 
270
 
Línea 227... Línea 271...
227
      self.methodFactory = factory || defaultMethodFactory;
271
      self.methodFactory = factory || defaultMethodFactory;
-
 
272
 
228
 
273
      self.getLevel = function () {
-
 
274
          if (userLevel != null) {
-
 
275
            return userLevel;
-
 
276
          } else if (defaultLevel != null) {
-
 
277
            return defaultLevel;
-
 
278
          } else {
229
      self.getLevel = function () {
279
            return inheritedLevel;
Línea 230... Línea 280...
230
          return currentLevel;
280
          }
231
      };
-
 
232
 
-
 
233
      self.setLevel = function (level, persist) {
-
 
234
          if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
-
 
235
              level = self.levels[level.toUpperCase()];
281
      };
236
          }
282
 
237
          if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
283
      self.setLevel = function (level, persist) {
238
              currentLevel = level;
-
 
239
              if (persist !== false) {  // defaults to true
-
 
240
                  persistLevelIfPossible(level);
-
 
241
              }
-
 
242
              replaceLoggingMethods.call(self, level, name);
-
 
243
              if (typeof console === undefinedType && level < self.levels.SILENT) {
-
 
244
                  return "No console available for logging";
-
 
245
              }
284
          userLevel = normalizeLevel(level);
-
 
285
          if (persist !== false) {  // defaults to true
-
 
286
              persistLevelIfPossible(userLevel);
-
 
287
          }
246
          } else {
288
 
Línea 247... Línea 289...
247
              throw "log.setLevel() called with invalid level: " + level;
289
          // NOTE: in v2, this should call rebuild(), which updates children.
248
          }
290
          return replaceLoggingMethods.call(self);
249
      };
291
      };
250
 
292
 
251
      self.setDefaultLevel = function (level) {
293
      self.setDefaultLevel = function (level) {
252
          defaultLevel = level;
294
          defaultLevel = normalizeLevel(level);
Línea 253... Línea 295...
253
          if (!getPersistedLevel()) {
295
          if (!getPersistedLevel()) {
254
              self.setLevel(level, false);
296
              self.setLevel(level, false);
255
          }
297
          }
-
 
298
      };
256
      };
299
 
Línea 257... Línea 300...
257
 
300
      self.resetLevel = function () {
258
      self.resetLevel = function () {
301
          userLevel = null;
259
          self.setLevel(defaultLevel, false);
302
          clearPersistedLevel();
Línea 260... Línea 303...
260
          clearPersistedLevel();
303
          replaceLoggingMethods.call(self);
261
      };
304
      };
262
 
305
 
Línea -... Línea 306...
-
 
306
      self.enableAll = function(persist) {
-
 
307
          self.setLevel(self.levels.TRACE, persist);
-
 
308
      };
-
 
309
 
-
 
310
      self.disableAll = function(persist) {
-
 
311
          self.setLevel(self.levels.SILENT, persist);
-
 
312
      };
-
 
313
 
-
 
314
      self.rebuild = function () {
-
 
315
          if (defaultLogger !== self) {
-
 
316
              inheritedLevel = normalizeLevel(defaultLogger.getLevel());
-
 
317
          }
-
 
318
          replaceLoggingMethods.call(self);
263
      self.enableAll = function(persist) {
319
 
-
 
320
          if (defaultLogger === self) {
-
 
321
              for (var childName in _loggersByName) {
-
 
322
                _loggersByName[childName].rebuild();
264
          self.setLevel(self.levels.TRACE, persist);
323
              }
265
      };
324
          }
266
 
325
      };
267
      self.disableAll = function(persist) {
326
 
268
          self.setLevel(self.levels.SILENT, persist);
327
      // Initialize all the internal levels.
269
      };
328
      inheritedLevel = normalizeLevel(
Línea 270... Línea 329...
270
 
329
          defaultLogger ? defaultLogger.getLevel() : "WARN"
271
      // Initialize with the right level
330
      );
272
      var initialLevel = getPersistedLevel();
331
      var initialLevel = getPersistedLevel();
273
      if (initialLevel == null) {
332
      if (initialLevel != null) {
274
          initialLevel = defaultLevel;
333
          userLevel = normalizeLevel(initialLevel);
Línea 275... Línea 334...
275
      }
334
      }
Línea 276... Línea -...
276
      self.setLevel(initialLevel, false);
-
 
277
    }
335
      replaceLoggingMethods.call(self);
278
 
336
    }
279
    /*
337
 
280
     *
338
    /*
Línea 281... Línea 339...
281
     * Top-level API
339
     *
282
     *
340
     * Top-level API
283
     */
341
     *
-
 
342
     */
284
 
343
 
-
 
344
    defaultLogger = new Logger();
285
    var defaultLogger = new Logger();
345
 
286
 
346
    defaultLogger.getLogger = function getLogger(name) {
287
    var _loggersByName = {};
347
        if ((typeof name !== "symbol" && typeof name !== "string") || name === "") {
Línea 288... Línea 348...
288
    defaultLogger.getLogger = function getLogger(name) {
348
            throw new TypeError("You must supply a name when creating a logger.");