Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loglevel.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * loglevel - https://github.com/pimterry/loglevel
  3. *
  4. * Copyright (c) 2013 Tim Perry
  5. * Licensed under the MIT license.
  6. */
  7. (function (root, definition) {
  8. "use strict";
  9. if (typeof define === 'function' && define.amd) {
  10. define(definition);
  11. } else if (typeof module === 'object' && module.exports) {
  12. module.exports = definition();
  13. } else {
  14. root.log = definition();
  15. }
  16. }(this, function () {
  17. "use strict";
  18. // Slightly dubious tricks to cut down minimized file size
  19. var noop = function() {};
  20. var undefinedType = "undefined";
  21. var isIE = (typeof window !== undefinedType) && (typeof window.navigator !== undefinedType) && (
  22. /Trident\/|MSIE /.test(window.navigator.userAgent)
  23. );
  24. var logMethods = [
  25. "trace",
  26. "debug",
  27. "info",
  28. "warn",
  29. "error"
  30. ];
  31. // Cross-browser bind equivalent that works at least back to IE6
  32. function bindMethod(obj, methodName) {
  33. var method = obj[methodName];
  34. if (typeof method.bind === 'function') {
  35. return method.bind(obj);
  36. } else {
  37. try {
  38. return Function.prototype.bind.call(method, obj);
  39. } catch (e) {
  40. // Missing bind shim or IE8 + Modernizr, fallback to wrapping
  41. return function() {
  42. return Function.prototype.apply.apply(method, [obj, arguments]);
  43. };
  44. }
  45. }
  46. }
  47. // Trace() doesn't print the message in IE, so for that case we need to wrap it
  48. function traceForIE() {
  49. if (console.log) {
  50. if (console.log.apply) {
  51. console.log.apply(console, arguments);
  52. } else {
  53. // In old IE, native console methods themselves don't have apply().
  54. Function.prototype.apply.apply(console.log, [console, arguments]);
  55. }
  56. }
  57. if (console.trace) console.trace();
  58. }
  59. // Build the best logging method possible for this env
  60. // Wherever possible we want to bind, not wrap, to preserve stack traces
  61. function realMethod(methodName) {
  62. if (methodName === 'debug') {
  63. methodName = 'log';
  64. }
  65. if (typeof console === undefinedType) {
  66. return false; // No method possible, for now - fixed later by enableLoggingWhenConsoleArrives
  67. } else if (methodName === 'trace' && isIE) {
  68. return traceForIE;
  69. } else if (console[methodName] !== undefined) {
  70. return bindMethod(console, methodName);
  71. } else if (console.log !== undefined) {
  72. return bindMethod(console, 'log');
  73. } else {
  74. return noop;
  75. }
  76. }
  77. // These private functions always need `this` to be set properly
  78. function replaceLoggingMethods(level, loggerName) {
  79. /*jshint validthis:true */
  80. for (var i = 0; i < logMethods.length; i++) {
  81. var methodName = logMethods[i];
  82. this[methodName] = (i < level) ?
  83. noop :
  84. this.methodFactory(methodName, level, loggerName);
  85. }
  86. // Define log.log as an alias for log.debug
  87. this.log = this.debug;
  88. }
  89. // In old IE versions, the console isn't present until you first open it.
  90. // We build realMethod() replacements here that regenerate logging methods
  91. function enableLoggingWhenConsoleArrives(methodName, level, loggerName) {
  92. return function () {
  93. if (typeof console !== undefinedType) {
  94. replaceLoggingMethods.call(this, level, loggerName);
  95. this[methodName].apply(this, arguments);
  96. }
  97. };
  98. }
  99. // By default, we use closely bound real methods wherever possible, and
  100. // otherwise we wait for a console to appear, and then try again.
  101. function defaultMethodFactory(methodName, level, loggerName) {
  102. /*jshint validthis:true */
  103. return realMethod(methodName) ||
  104. enableLoggingWhenConsoleArrives.apply(this, arguments);
  105. }
  106. function Logger(name, defaultLevel, factory) {
  107. var self = this;
  108. var currentLevel;
  109. var storageKey = "loglevel";
  110. if (typeof name === "string") {
  111. storageKey += ":" + name;
  112. } else if (typeof name === "symbol") {
  113. storageKey = undefined;
  114. }
  115. function persistLevelIfPossible(levelNum) {
  116. var levelName = (logMethods[levelNum] || 'silent').toUpperCase();
  117. if (typeof window === undefinedType || !storageKey) return;
  118. // Use localStorage if available
  119. try {
  120. window.localStorage[storageKey] = levelName;
  121. return;
  122. } catch (ignore) {}
  123. // Use session cookie as fallback
  124. try {
  125. window.document.cookie =
  126. encodeURIComponent(storageKey) + "=" + levelName + ";";
  127. } catch (ignore) {}
  128. }
  129. function getPersistedLevel() {
  130. var storedLevel;
  131. if (typeof window === undefinedType || !storageKey) return;
  132. try {
  133. storedLevel = window.localStorage[storageKey];
  134. } catch (ignore) {}
  135. // Fallback to cookies if local storage gives us nothing
  136. if (typeof storedLevel === undefinedType) {
  137. try {
  138. var cookie = window.document.cookie;
  139. var location = cookie.indexOf(
  140. encodeURIComponent(storageKey) + "=");
  141. if (location !== -1) {
  142. storedLevel = /^([^;]+)/.exec(cookie.slice(location))[1];
  143. }
  144. } catch (ignore) {}
  145. }
  146. // If the stored level is not valid, treat it as if nothing was stored.
  147. if (self.levels[storedLevel] === undefined) {
  148. storedLevel = undefined;
  149. }
  150. return storedLevel;
  151. }
  152. /*
  153. *
  154. * Public logger API - see https://github.com/pimterry/loglevel for details
  155. *
  156. */
  157. self.name = name;
  158. self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3,
  159. "ERROR": 4, "SILENT": 5};
  160. self.methodFactory = factory || defaultMethodFactory;
  161. self.getLevel = function () {
  162. return currentLevel;
  163. };
  164. self.setLevel = function (level, persist) {
  165. if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
  166. level = self.levels[level.toUpperCase()];
  167. }
  168. if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
  169. currentLevel = level;
  170. if (persist !== false) { // defaults to true
  171. persistLevelIfPossible(level);
  172. }
  173. replaceLoggingMethods.call(self, level, name);
  174. if (typeof console === undefinedType && level < self.levels.SILENT) {
  175. return "No console available for logging";
  176. }
  177. } else {
  178. throw "log.setLevel() called with invalid level: " + level;
  179. }
  180. };
  181. self.setDefaultLevel = function (level) {
  182. if (!getPersistedLevel()) {
  183. self.setLevel(level, false);
  184. }
  185. };
  186. self.enableAll = function(persist) {
  187. self.setLevel(self.levels.TRACE, persist);
  188. };
  189. self.disableAll = function(persist) {
  190. self.setLevel(self.levels.SILENT, persist);
  191. };
  192. // Initialize with the right level
  193. var initialLevel = getPersistedLevel();
  194. if (initialLevel == null) {
  195. initialLevel = defaultLevel == null ? "WARN" : defaultLevel;
  196. }
  197. self.setLevel(initialLevel, false);
  198. }
  199. /*
  200. *
  201. * Top-level API
  202. *
  203. */
  204. var defaultLogger = new Logger();
  205. var _loggersByName = {};
  206. defaultLogger.getLogger = function getLogger(name) {
  207. if ((typeof name !== "symbol" && typeof name !== "string") || name === "") {
  208. throw new TypeError("You must supply a name when creating a logger.");
  209. }
  210. var logger = _loggersByName[name];
  211. if (!logger) {
  212. logger = _loggersByName[name] = new Logger(
  213. name, defaultLogger.getLevel(), defaultLogger.methodFactory);
  214. }
  215. return logger;
  216. };
  217. // Grab the current global log variable in case of overwrite
  218. var _log = (typeof window !== undefinedType) ? window.log : undefined;
  219. defaultLogger.noConflict = function() {
  220. if (typeof window !== undefinedType &&
  221. window.log === defaultLogger) {
  222. window.log = _log;
  223. }
  224. return defaultLogger;
  225. };
  226. defaultLogger.getLoggers = function getLoggers() {
  227. return _loggersByName;
  228. };
  229. // ES6 default export, for compatibility
  230. defaultLogger['default'] = defaultLogger;
  231. return defaultLogger;
  232. }));