Skip to content

Instantly share code, notes, and snippets.

@MarcDiethelm
Created September 11, 2012 20:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcDiethelm/3701785 to your computer and use it in GitHub Desktop.
Save MarcDiethelm/3701785 to your computer and use it in GitHub Desktop.
Global development and debugging helper (browser)
/**
* Global development and debugging helper
* Author: Marc Diethelm
*/
(function() {
var w = window
,c
,concatArgs
,initConcatArgs = function() {
return function concatArgs(args) {
for (var i = 0, str = ''; i < args.length; i++) {
if (args[i] !== undefined && args[i] !== null)
str += (args[i].toString() + ' ');
else
str += args[i];
}
return str;
}
};
if (w.console) {
c = console;
if ('dir' in c && 'apply' in c.dir) { // create global shortcuts
typeof c.log === 'function' && ( w.log = function() { c.log.apply(c, arguments) } );
typeof c.info === 'function' && ( w.info = function() { c.info.apply(c, arguments) } );
typeof c.debug === 'function' && ( w.debug = function() { c.debug.apply(c, arguments) } );
typeof c.error === 'function' && ( w.error = function() { c.error.apply(c, arguments) } );
typeof c.dir === 'function' && ( w.dir = function() { c.dir.apply(c, arguments) } );
typeof c.table === 'function' && ( w.table = function() { c.table.apply(c, arguments) } );
// A tiny jQuery plugin adding logging to any jQuery object
w.jQuery && (jQuery.fn.log = function(clear) {
clear && c.clear.call(c);
c.log.call(c, this);
return this;
});
}
else { // IE: we have console.log but it just accepts one param. let's fix that! :)
concatArgs = initConcatArgs();
w.log = function() {
c.log(concatArgs(arguments));
}
}
clear = c.clear; // supported in IE and Fx
}
else {
concatArgs = initConcatArgs();
w.log = function() { _log(arguments) };
_log = function(args) { alert(concatArgs(args)) };
}
})();
@MarcDiethelm
Copy link
Author

Ideas from Simon Mollweide:
Handle error objects, eg. display line number.
Create/use a "console" element for the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment