Usually, in the FAT client B/s from the program, front-end JavaScript script is the most difficult to maintain, because of the diversity of the client's operating environment and the script to run the owner of the dependency, developers often can not reproduce the user's errors on the computer. So, when a lot of JavaScript exists, Monitoring the running state of the system, including logs and performance, is particularly important. When a client fails or is down, you can ask the user to send the system running history and current information to the developer for analysis.
However, too much intrusion into business logic code can negatively affect the stability and readability of your code. The following program can extend the functionality of a program without modifying the program code to easily monitor the stack and procedure of the call.
/**
*
*aopsupport 1.0
*written by Jarezwu
*last Updated on 2007-3-9
*
*/
aopsupport={
/**
* JavaScript method Agent, wrapping a method
* fn: Method name
* Obj: Method owner
* Roundfunc: Packing method
*/
Round:function (fn/*string*/,obj/*object*/,roundfunc/*function*/) {
var preserved=["alert"];
for (Var i=0,len=preserved.length;i<len;i++) {
if (PRESERVED[I]==FN) {
throw new Error ("Unable to monitor" +FN);
}
}
if (!obj) {//The default context is global
obj=self;
}
var of=obj[fn];
if (!of) {
Return
}
var nf=function () {
var args=arguments;
var invocation=new function () {
This.args=args;
This.method=of;
This.context=obj;
var self=this;
This.proceed=function () {
Return self.method.apply (Self.context,self.args);
}
}
Return Roundfunc.call (obj,invocation);
}
OBJ[FN]=NF;
}
}
An example is provided:
1. Add logic
function foo () {
Alert ("core business");
}
Foo ();
function Bar () {
Alert ("Value-added services");
}
Aopsupport.round ("foo", Null,function (invocation) {
Invocation.proceed ();
Bar ();
});
Foo ();
2. Record Log
logger={//a log recorder
Trace:function (info) {
Log stack Trace
},
Error:function (info) {
Log error
},
Debug:function (info) {
Log output
},
Dump:function () {
Output logged Info
}
};
Var obj=new object ();//Sample Object
Obj.doit=function () {//Sample method
Do something
}
Obj.doit ();
The process of wrapping the procedure below
Aopsupport.round ("DoIt", Obj,function (invocation) {
Logger.trace ("Start:" +invocation.method);/track Start
try{
Logger.debug (Invocation.args);//Record parameters
var result=invocation.proceed ()//Call
Logger.debug (result);//Record results
}catch (e) {
Logger.error (e);//Log exception
}
Logger.trace ("End:" +invocation.method);/close Trace
});
Obj.doit ()//Run
Logger.dump ()//print trace information