Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tim Perry
committed
Mar 18, 2014
1 parent
4e52bcb
commit c3c8cac
Showing
5 changed files
with
197 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,175 +1,191 @@ | ||
/*! loglevel - v0.5.0 - https://github.com/pimterry/loglevel - (c) 2013 Tim Perry - licensed MIT */ | ||
;(function (undefined) { | ||
var undefinedType = "undefined"; | ||
|
||
(function (name, definition) { | ||
if (typeof module !== 'undefined') { | ||
module.exports = definition(); | ||
} else if (typeof define === 'function' && typeof define.amd === 'object') { | ||
define(definition); | ||
} else { | ||
this[name] = definition(); | ||
} | ||
}('log', function () { | ||
var self = {}; | ||
var noop = function() {}; | ||
|
||
function realMethod(methodName) { | ||
if (typeof console === undefinedType) { | ||
return noop; | ||
} else if (console[methodName] === undefined) { | ||
if (console.log !== undefined) { | ||
return boundToConsole(console, 'log'); | ||
} else { | ||
return noop; | ||
} | ||
} else { | ||
return boundToConsole(console, methodName); | ||
} | ||
} | ||
|
||
function boundToConsole(console, methodName) { | ||
var method = console[methodName]; | ||
if (method.bind === undefined) { | ||
if (Function.prototype.bind === undefined) { | ||
return functionBindingWrapper(method, console); | ||
} else { | ||
try { | ||
return Function.prototype.bind.call(console[methodName], console); | ||
} catch (e) { | ||
// In IE8 + Modernizr, the bind shim will reject the above, so we fall back to wrapping | ||
return functionBindingWrapper(method, console); | ||
} | ||
} | ||
} else { | ||
return console[methodName].bind(console); | ||
} | ||
} | ||
|
||
function functionBindingWrapper(f, context) { | ||
return function() { | ||
Function.prototype.apply.apply(f, [context, arguments]); | ||
}; | ||
} | ||
|
||
var logMethods = [ | ||
"trace", | ||
"debug", | ||
"info", | ||
"warn", | ||
"error" | ||
]; | ||
|
||
function replaceLoggingMethods(methodFactory) { | ||
for (var ii = 0; ii < logMethods.length; ii++) { | ||
self[logMethods[ii]] = methodFactory(logMethods[ii]); | ||
} | ||
} | ||
|
||
function cookiesAvailable() { | ||
return (typeof window !== undefinedType && | ||
window.document !== undefined && | ||
window.document.cookie !== undefined); | ||
} | ||
|
||
function localStorageAvailable() { | ||
try { | ||
return (typeof window !== undefinedType && | ||
window.localStorage !== undefined); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
function persistLevelIfPossible(levelNum) { | ||
var levelName; | ||
|
||
for (var key in self.levels) { | ||
if (self.levels.hasOwnProperty(key) && self.levels[key] === levelNum) { | ||
levelName = key; | ||
break; | ||
} | ||
} | ||
|
||
if (localStorageAvailable()) { | ||
window.localStorage['loglevel'] = levelName; | ||
} else if (cookiesAvailable()) { | ||
window.document.cookie = "loglevel=" + levelName + ";"; | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
var cookieRegex = /loglevel=([^;]+)/; | ||
|
||
function loadPersistedLevel() { | ||
var storedLevel; | ||
|
||
if (localStorageAvailable()) { | ||
storedLevel = window.localStorage['loglevel']; | ||
} | ||
|
||
if (!storedLevel && cookiesAvailable()) { | ||
var cookieMatch = cookieRegex.exec(window.document.cookie) || []; | ||
storedLevel = cookieMatch[1]; | ||
} | ||
|
||
self.setLevel(self.levels[storedLevel] || self.levels.WARN); | ||
} | ||
|
||
/* | ||
* | ||
* Public API | ||
* | ||
*/ | ||
|
||
self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3, | ||
"ERROR": 4, "SILENT": 5}; | ||
|
||
self.setLevel = function (level) { | ||
if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) { | ||
persistLevelIfPossible(level); | ||
|
||
if (level === self.levels.SILENT) { | ||
replaceLoggingMethods(function () { | ||
return noop; | ||
}); | ||
return; | ||
} else if (typeof console === undefinedType) { | ||
replaceLoggingMethods(function (methodName) { | ||
return function () { | ||
if (typeof console !== undefinedType) { | ||
self.setLevel(level); | ||
self[methodName].apply(self, arguments); | ||
} | ||
}; | ||
}); | ||
return "No console available for logging"; | ||
} else { | ||
replaceLoggingMethods(function (methodName) { | ||
if (level <= self.levels[methodName.toUpperCase()]) { | ||
return realMethod(methodName); | ||
} else { | ||
return noop; | ||
} | ||
}); | ||
} | ||
} else if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) { | ||
self.setLevel(self.levels[level.toUpperCase()]); | ||
} else { | ||
throw "log.setLevel() called with invalid level: " + level; | ||
} | ||
}; | ||
|
||
self.enableAll = function() { | ||
self.setLevel(self.levels.TRACE); | ||
}; | ||
|
||
self.disableAll = function() { | ||
self.setLevel(self.levels.SILENT); | ||
}; | ||
|
||
loadPersistedLevel(); | ||
return self; | ||
})); | ||
})(); | ||
/*! loglevel - v0.6.0 - https://github.com/pimterry/loglevel - (c) 2014 Tim Perry - licensed MIT */ | ||
;(function (undefined) { | ||
var undefinedType = "undefined"; | ||
|
||
(function (name, definition) { | ||
if (typeof module !== 'undefined') { | ||
module.exports = definition(); | ||
} else if (typeof define === 'function' && typeof define.amd === 'object') { | ||
define(definition); | ||
} else { | ||
this[name] = definition(); | ||
} | ||
}('log', function () { | ||
var self = {}; | ||
var noop = function() {}; | ||
|
||
function realMethod(methodName) { | ||
if (typeof console === undefinedType) { | ||
return noop; | ||
} else if (console[methodName] === undefined) { | ||
if (console.log !== undefined) { | ||
return boundToConsole(console, 'log'); | ||
} else { | ||
return noop; | ||
} | ||
} else { | ||
return boundToConsole(console, methodName); | ||
} | ||
} | ||
|
||
function boundToConsole(console, methodName) { | ||
var method = console[methodName]; | ||
if (method.bind === undefined) { | ||
if (Function.prototype.bind === undefined) { | ||
return functionBindingWrapper(method, console); | ||
} else { | ||
try { | ||
return Function.prototype.bind.call(console[methodName], console); | ||
} catch (e) { | ||
// In IE8 + Modernizr, the bind shim will reject the above, so we fall back to wrapping | ||
return functionBindingWrapper(method, console); | ||
} | ||
} | ||
} else { | ||
return console[methodName].bind(console); | ||
} | ||
} | ||
|
||
function functionBindingWrapper(f, context) { | ||
return function() { | ||
Function.prototype.apply.apply(f, [context, arguments]); | ||
}; | ||
} | ||
|
||
var logMethods = [ | ||
"trace", | ||
"debug", | ||
"info", | ||
"warn", | ||
"error" | ||
]; | ||
|
||
function replaceLoggingMethods(methodFactory) { | ||
for (var ii = 0; ii < logMethods.length; ii++) { | ||
self[logMethods[ii]] = methodFactory(logMethods[ii]); | ||
} | ||
} | ||
|
||
function cookiesAvailable() { | ||
return (typeof window !== undefinedType && | ||
window.document !== undefined && | ||
window.document.cookie !== undefined); | ||
} | ||
|
||
function localStorageAvailable() { | ||
try { | ||
return (typeof window !== undefinedType && | ||
window.localStorage !== undefined); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
function persistLevelIfPossible(levelNum) { | ||
var localStorageFail = false, | ||
levelName; | ||
|
||
for (var key in self.levels) { | ||
if (self.levels.hasOwnProperty(key) && self.levels[key] === levelNum) { | ||
levelName = key; | ||
break; | ||
} | ||
} | ||
|
||
if (localStorageAvailable()) { | ||
/* | ||
* Setting localStorage can create a DOM 22 Exception if running in Private mode | ||
* in Safari, so even if it is available we need to catch any errors when trying | ||
* to write to it | ||
*/ | ||
try { | ||
window.localStorage['loglevel'] = levelName; | ||
} catch (e) { | ||
localStorageFail = true; | ||
} | ||
} else { | ||
localStorageFail = true; | ||
} | ||
|
||
if (localStorageFail && cookiesAvailable()) { | ||
window.document.cookie = "loglevel=" + levelName + ";"; | ||
} | ||
} | ||
|
||
var cookieRegex = /loglevel=([^;]+)/; | ||
|
||
function loadPersistedLevel() { | ||
var storedLevel; | ||
|
||
if (localStorageAvailable()) { | ||
storedLevel = window.localStorage['loglevel']; | ||
} | ||
|
||
if (storedLevel === undefined && cookiesAvailable()) { | ||
var cookieMatch = cookieRegex.exec(window.document.cookie) || []; | ||
storedLevel = cookieMatch[1]; | ||
} | ||
|
||
if (self.levels[storedLevel] === undefined) { | ||
storedLevel = "WARN"; | ||
} | ||
|
||
self.setLevel(self.levels[storedLevel]); | ||
} | ||
|
||
/* | ||
* | ||
* Public API | ||
* | ||
*/ | ||
|
||
self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3, | ||
"ERROR": 4, "SILENT": 5}; | ||
|
||
self.setLevel = function (level) { | ||
if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) { | ||
persistLevelIfPossible(level); | ||
|
||
if (level === self.levels.SILENT) { | ||
replaceLoggingMethods(function () { | ||
return noop; | ||
}); | ||
return; | ||
} else if (typeof console === undefinedType) { | ||
replaceLoggingMethods(function (methodName) { | ||
return function () { | ||
if (typeof console !== undefinedType) { | ||
self.setLevel(level); | ||
self[methodName].apply(self, arguments); | ||
} | ||
}; | ||
}); | ||
return "No console available for logging"; | ||
} else { | ||
replaceLoggingMethods(function (methodName) { | ||
if (level <= self.levels[methodName.toUpperCase()]) { | ||
return realMethod(methodName); | ||
} else { | ||
return noop; | ||
} | ||
}); | ||
} | ||
} else if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) { | ||
self.setLevel(self.levels[level.toUpperCase()]); | ||
} else { | ||
throw "log.setLevel() called with invalid level: " + level; | ||
} | ||
}; | ||
|
||
self.enableAll = function() { | ||
self.setLevel(self.levels.TRACE); | ||
}; | ||
|
||
self.disableAll = function() { | ||
self.setLevel(self.levels.SILENT); | ||
}; | ||
|
||
loadPersistedLevel(); | ||
return self; | ||
})); | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters