Skip to content

Instantly share code, notes, and snippets.

@triple-j
Created July 14, 2014 19:16
Show Gist options
  • Save triple-j/8aa662068b7367818c31 to your computer and use it in GitHub Desktop.
Save triple-j/8aa662068b7367818c31 to your computer and use it in GitHub Desktop.
display debug info
(function( $, window, undefined ) {
"use strict";
/**
* Set DEBUG_LVL and Show/Hide debugging DOM Elements
*/
window.setDebugLevel = function( level ) {
level = parseInt( level, 10 );
window.DEBUG_LVL = isNaN(level) ? 0 : level;
if ( window.DEBUG_LVL > 0 ) {
$('.debug').show();
} else {
$('.debug').hide();
}
}
/**
* Display message to console.log if DEBUG_LVL is high enough
*/
window.debug = function( output, level ) {
if ( typeof level === "undefined" ) level = 1;
if ( typeof window.DEBUG_LVL !== "undefined" && window.DEBUG_LVL >= level ) {
try {
console.log( output );
} catch (e) {
// create a makeshift console
if ( $('textarea#debugConsole').length < 1 ) {
var debugConsoleBlock = $('<div/>').appendTo('body');
$('<a/>',{"href":"#","onclick":"$('textarea#debugConsole').toggle();","text":"[ DEBUG: toggle console ]","style":"font-size:small;"}).appendTo( debugConsoleBlock );
$('<textarea/>',{"id":"debugConsole","rows":24,"style":"display:none;width:99%;margin-bottom:3em;"}).appendTo( debugConsoleBlock );
}
if ( typeof output === "object" ) { output = JSON.stringify(output); }
$('textarea#debugConsole').val( $('textarea#debugConsole').val() + "\n" + output );
setTimeout(function() {
var textArea = document.getElementById('debugConsole');
textArea.scrollTop = textArea.scrollHeight;
}, 10);
}
}
};
})( jQuery, window );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment