Skip to content

Instantly share code, notes, and snippets.

@makepanic
Created September 10, 2012 18:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makepanic/3692918 to your computer and use it in GitHub Desktop.
Save makepanic/3692918 to your computer and use it in GitHub Desktop.
console log wrapper
var c = {
/*
* @reference: http://blog.rndm.de/p/console-log-wrapper
* @author: Christian http://rndm.de/
* @license: Licensed under Apache License v2.0. See http://www.apache.org/licenses/LICENSE-2.0
*
* example: c.log("Hello World")
* @param obj - variable to log
* @param trace - boolean display console.trace()
*/
types : {
LOG:0,
WARN:1,
ERR:2,
INFO:3
},
warn : function(){
this.main(this.types.WARN, arguments, arguments.callee.caller);
},
error : function(){
this.main(this.types.ERR, arguments, arguments.callee.caller);
},
log : function(){
this.main(this.types.LOG, arguments, arguments.callee.caller);
},
info : function(){
this.main(this.types.INFO, arguments, arguments.callee.caller);
},
mOut : function(args, from, fn){
var t = new Date().toTimeString();
fn.call(console,[t.substr(0,8), from].join(" "));
for(var i = 0; i< args.length;i++){
fn.call(console," [" + typeof(args[i]) + "] ", args[i]);
}
},
main : function(type, args, caller, trace){
trace = false;
if(window.console){
var from = "";
var fn = null;
var date = new Date().toTimeString();
from = caller ? "<" + typeof(caller) + ":" + caller.name + ">" : "<browser>";
switch(type){
case this.types.ERR:
fn = console.error;
if(fn.call)
args.length>1 ? this.mOut(args, from, fn) : fn.call(console,[date.substr(0,8), from, "["+typeof(args[0])+"]"].join(" "), args[0]);
break;
case this.types.WARN:
fn = console.warn;
if(fn.call)
args.length>1 ? this.mOut(args, from, fn) : fn.call(console,[date.substr(0,8), from, "["+typeof(args[0])+"]"].join(" "), args[0]);
break;
case this.types.LOG:
fn = console.log;
if(fn.call)
args.length>1 ? this.mOut(args, from, fn) : fn.call(console,[date.substr(0,8), from, "["+typeof(args[0])+"]"].join(" "), args[0]);
break;
case this.types.INFO:
fn = console.info;
if(fn.call)
args.length>1 ? this.mOut(args, from, fn) : fn.call(console,[date.substr(0,8), from, "["+typeof(args[0])+"]"].join(" "), args[0]);
break;
}
if(trace)console.trace(obj);
return true;
}else{
/* no console available */
return false;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment