Skip to content

Instantly share code, notes, and snippets.

@0x4a
Last active August 29, 2015 13:57
Show Gist options
  • Save 0x4a/9469461 to your computer and use it in GitHub Desktop.
Save 0x4a/9469461 to your computer and use it in GitHub Desktop.
helper function for logging, debug-mode is saved to localstorage, copyright belongs to the two ppl from stackoverflow I stole this from, see comments #jQuery #debug #helper
$(function() {
debug.init();
//debug.load();
});
(function(debug, $, undefined) {
// https://stackoverflow.com/a/14223953
// https://stackoverflow.com/a/1216743
// Prevent errors in browsers without console.log
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function(){};
//console.group("meta-debug");
var console_log = console.log;
var debug_state = $(window).data("DEBUG");
var key = "debug.0x4a.net";
var n = 1;
//Public methods
debug.init = function initState() {};
debug.check = function checkConsole() { console.info("debug: " + debug_state + " - Console.log: " + console_log + n++); };
debug.disable = function disableLog() { console.log = function() {}; $(window).data("DEBUG", false); };
debug.enable = function enableLog() { console.log = console_log; $(window).data("DEBUG", true); };
debug.load = function loadStorage() { $(window).data("DEBUG", localStorage.getItem(key)); };
debug.save = function saveStorage() { localStorage.setItem(key, debug_state); };
debug.state = function showState() { alert("Debug = " + debug_state); };
debug.list = function listStorage() { for (var key in localStorage) { console.log(key + " => " + localStorage.getItem(key))};};
debug.off = function turnOff() {
var methods = ["log", "debug", "warn", "info"];
for(var i=0;i<methods.length;i++){ console[methods[i]] = function(){ }; }
};
debug.tools = function extools(mode) {
if (!debug_state) { alert("Debug Mode must be active"); return 0; }
else if (mode == "load")
{
jQuery.getScript("https://raw.github.com/doomhz/jQuery-Tweaks/master/jquery.doomTweaks.js")
.done(function() { console.info("doomTweaks loaded!"); return 1; })
.fail(function() { console.error("loading failed... "); return 0; });
}
else
{
console.log("$.l('message'); // log");
console.log("$.time(); // time");
console.log("$.lt(); // log time toggle");
console.log("$.bm('cmd();'); // benchmark");
}
};
}(window.debug = window.debug || {}, jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment