/*
 * 调试工具类
 * 跟踪一个运行过程的每步运行时间，并输出。
 * @author robinz,wt
 */
function PerfTrace(name,disable,log4j) {
    this._init = 0;
    this._last = 0;
    this._maxCost = 0;
    this._name = name;
    this._log4j = log4j;
    this._disabled = disable;
}
PerfTrace.prototype.begin = function() {
	if (this._disabled){
		return;
	}
	this._init = new Date().getTime();
	this._last = this._init;
	this._print(this._name+", begin trace at " + this._last);
};

PerfTrace.prototype.step = function(desc) {
	if (this._disabled){
		return;
	}
	var cur = new Date().getTime();
	var cost= cur - this._last;
	this._print(this._name+", step: " + desc + ", elapsed: " + (cost) +" ms");
	this._last = cur;
	if(cost>this._maxCost){
		this._maxCost=cost;
	}
};
PerfTrace.prototype.end = function() {
	if (this._disabled){
		return;
	}
	var cur = new Date().getTime();
	this._print(this._name+", end trace, all elapsed: " + (cur - this._init) +" ms");
	this._last = cur;
};
PerfTrace.prototype.elpased = function() {
	return this._last - this._init;
};
PerfTrace.prototype._print = function(msg) {
	if (this._log4j) {
		this._log4j.debug(msg);
	}else {
		alert(msg);
	}
};
