1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| // by dessy boris
// for test just include jQuery in a html page and this file(name it : "handler.js")
// sample like this :
/*
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="handler.js"></script>
*/
// welcome ^^
var handler = {
log : function(msg,type){
var p = type=='function'?'()':'';
console.log(type+' : '+handler.level+msg+p);
// logFile isn't used here, but you can use for send packet with all log to your server via ajax.
handler.logFile.push(type+':'+msg);
},
exception : function(err,o,arg,name,f){
handler.log(err,'## error');
// -- you can do lot of things to handle your error here
//-----------------
// fn.name = name;
// fn = f;
// fn.caller = arguments.callee.caller.caller
// object = o;
// arguments = arg;
// error = err;
//-----------------
},
create : function(f,name){
// return a new function to replace original
return function(){
// log function call
handler.log(name,'function');
// increment deep level of call
handler.level += '--';
// make the call and catch(+log) error
try{ var r = f.apply(this,arguments); }
catch(e){ handler.exception(e,this,arguments,name,f); }
// decrement deep level of call
handler.level = handler.level.substr(2);
// return value of call
return r;
};
},
init : function(o,restrict){
handler.log('_begin','handling');
// browse all functions of the object (except the restricted)
for(var i in o) if(typeof o[i] == 'function') if(!restrict || restrict[i]==undefined){
// overwrite each function with new
o[i] = handler.create(o[i],i);
}
handler.log('_end','handling');
},
logFile : [],
level : ''
};
myOwnFn = {};
$(document).ready(function() {
// create console if none is define
if(!window.console){
window.console = document.createElement('div');
console.setAttribute('style','position:absolute;top:0;right:0;height:150px;width:400px;border:solid black 1px;overflow:auto;background-color:white;');
console.innerHTML = '<b>CONSOLE:</b><br/>';
document.body.appendChild(console);
console.log = function(msg){this.innerHTML += '-'+msg+'<br/>';this.scrollTop=999999;};
}
console.log('console : initialized');
// SAMPLES :
// handling myOwnFn
handler.init(myOwnFn);
// handling jQuery.fn
handler.init(jQuery.fn,{init:1});
// sample of use (of jQuery)
$('<div id="test">hello world(click me)</div>').appendTo('body');
$('#test').click( function(){ $('#test').fadeOut();} );
}); |
Partager