后来改进的debug.as,因为有个小bug会严重影响速度,详细见前文

上一篇详细介绍debug.as的文章发了以后,发现一个小问题。因为对textField对象的text属性直接作+=操作会严重影响速度(这应该是flash的bug),故对代码作了些改进。改动主要在textField的onSetFocus()函数中。修正后读入日志速度比原来快了两倍。

另外用Array.join()方法替代了原来循环连接字符串的操作,比原有代码速度提高了一倍。

经过测试,在P4 2.4上,现在可以处理20多万行日志而不会弹出速度慢的警告窗口。

// Debug.as
//
written by foreverflying
//
class Debug
{
    
//=========================================================
    //in player trace===============================Version 1.2
    static public function CreateTrace( depth, x, y, width, height, lineCount, mc )
    
{
        
if( _logObj != undefined ){
            
return;
        }

        
if( mc == undefined ){
            mc 
= _level0;
        }

        
if( lineCount == undefined ){
            lineCount 
= 12;
        }

        _logObj 
= new Array();
        _logObj.allowChange 
= true;
        _logObj.isPart 
= false;
        _logObj.lineCount 
= lineCount;
        mc.createTextField( 
'__traceText__', depth, x, y, width, height );
        _logObj.traceText 
= mc['__traceText__'];
        
var traceText = _logObj.traceText;
        
var str = ' ';
        
while( traceText.maxscroll < 2 ){
            traceText.text 
= str;
            str 
+= str;
        }

        traceText.maxRow 
= traceText.bottomScroll - traceText.scroll + 1;
        traceText.text 
= '';
        traceText._visible 
= _traceVisible;
        traceText.border 
= true;
        traceText.background 
= true;
        traceText.backgroundColor 
= 0xFFFFFF;
        traceText.onSetFocus 
= function( oldFocus )
        
{
            _logObj.allowChange 
= false;
            
if!_logObj.isPart ){
                
return;
            }

            
if( _logObj[0== undefined ){
                
return;
            }

            
var s = _logObj[_logObj.length-1];
            s 
= s.charAt( s.length-1 );
            s 
= s == ' ' ? '' : ' ';
            
forvar i=0; i < this.maxRow - lineCount; i++ ){
                s 
+= ' ';
            }

            
this.text = _logObj.join( ' ' ) + s;
            _logObj.isPart 
= false;
            
this.scroll = this.maxscroll;
        }

        traceText.onKillFocus 
= function( newFocus )
        
{
            _logObj.allowChange 
= true;
            
if( _logObj.isPart ){
                
var myText = _logObj.traceText;
                
if!_logObj.lineCount ){
                    myText.text 
= '';
                    
return;
                }

                
var i = _logObj.length > lineCount ? _logObj.length - lineCount : 0;
                
var str:String = _logObj[i];
                
for++i; i<_logObj.length; i++ ){
                    str 
+= ' ' + _logObj[i];
                }

                myText.text 
= str;
                myText.scroll 
= myText.maxscroll;
            }

        }

        SetTraceFunc( _traceFunc );
    }


    static public 
function SetTraceVisible( isVisible )
    
{
        _traceVisible 
= isVisible == true;
        _logObj.traceText._visible 
= _traceVisible;
        
if!_logObj.lineCount || !_logObj.allowChange || !_traceVisible || _logObj.length == 0 ){
            
return;
        }

        
var myText = _logObj.traceText;
        
var lineCount = _logObj.lineCount;
        
var i = _logObj.length > lineCount ? _logObj.length - lineCount : 0;
        
var str:String = _logObj[i];
        
for++i; i<_logObj.length; i++ ){
            str 
+= ' ' + _logObj[i];
        }

        myText.text 
= str;
        myText.scroll 
= myText.maxscroll;
    }


    static public 
function SetTraceFunc( traceFunc )
    
{
        _traceFunc 
= traceFunc == undefined ? Trace : traceFunc;
    }


    static public 
function xtrace( msg:Object )
    
{
        
var indent = '';
        
forvar i=0; i<_indent; i++ ){
            indent 
+= '|   ';
        }

        _traceFunc( indent 
+ msg );
    }


    static private 
function Trace( msg:String )
    
{
        
if( _logObj == undefined ){
            
return;
        }

        
var myText = _logObj.traceText;
        
var lineCount = _logObj.lineCount;
        _logObj.push( msg );
        _logObj.isPart 
= true;
        
if!_logObj.lineCount || !_logObj.allowChange || !_traceVisible ){
            
return;
        }

        
var i = _logObj.length > lineCount ? _logObj.length - lineCount : 0;
        
var str:String = _logObj[i];
        
for++i; i<_logObj.length; i++ ){
            str 
+= ' ' + _logObj[i];
        }

        myText.text 
= str;
        myText.scroll 
= myText.maxscroll;
    }


    static private 
var _traceFunc = Trace;
    static private 
var _traceVisible = false;
    static private 
var _indent = 0;
    static private 
var _logObj;

    
//=========================================================
    //TraceClass====================================Version 1.1
    static public function TraceClass( name, obj, info )
    
{
        
if( _classTraceOn == false ){
            
return;
        }

        xtrace( 
'' + name + ' ] class created -----' + ( info == undefined ? '' : info ) );
        _global.ASSetPropFlags( obj.__proto__, 
null61 );
        
var control;
        
forvar i in obj ){
            
if( i != 'constructor' && typeof(obj[i]) == 'function' ){
                
if( control == undefined ){
                    control 
= new Object();
                    control.exclusiveCount 
= 0;
                }

                
if( obj[i] != undefined && obj[i].control == undefined ){
                    obj[i] 
= createRecallFunc( obj[i], name + '.' + i, control, info );
                }

            }

        }

    }


    static public 
function SwitchClassTrace( classTraceOn )
    
{
        _classTraceOn 
= classTraceOn == true;
    }


    
// 1 : only trace func
    // 2 : not trace func
    // 4 : not print trace info for func, but indent changes
    static public function SetFunctionTag( func, tag )
    
{
        
if( func.tag == undefined ){
            
return;
        }

        func.tag 
|= tag;
        
if( ( tag & 1 ) != 0 ){
            func.control.exclusiveCount
++;
        }

    }


    
//clear the tag setted, if tag is 0, clear all tags
    static public function ClearFunctionTag( func, tag )
    
{
        
if( func.tag == undefined ){
            
return;
        }

        tag 
= tag == 0 ? func.tag : tag;
        func.tag 
&= ~tag;
        
if( ( tag & 1 ) != 0 ){
            func.control.exclusiveCount
--;
        }

    }


    static private 
function createRecallFunc( orgFunc, funcName, control, info ):Function
    
{
        
var ret = new Function(
            
function()
            
{
                
return TraceCall( this, arguments );
            }

        );
        ret.orgFunc 
= orgFunc;
        ret.funcName 
= funcName;
        ret.control 
= control;
        ret.info 
= info;
        ret.tag 
= 0;
        
return ret;
    }


    static private 
function TraceCall( thisObj, arg )
    
{
        
var callee = arg.callee;
        
var callTrace = true;
        
if( callee.control.exclusiveCount > 0  ){
            callTrace 
= ( callee.tag & 1 ) != 0;
        }
else if( ( callee.tag & 2 ) != 0 ){
            callTrace 
= false;
        }

        
if( callTrace ){
            
if( ( callee.tag & 4 ) != 0 ){
                _indent
++;
            }
else{
                OnCallBegin( callee.funcName, callee.info );
            }

        }

        
var ret = callee.orgFunc.apply( thisObj, arg );
        
if( callTrace ){
            
if( ( callee.tag & 4 ) != 0 ){
                _indent
--;
            }
else{
                OnCallEnd( callee.funcName, callee.info, ret );
            }

        }

        
return ret;
    }


    static private 
function OnCallBegin( funcName, info )
    
{
        xtrace( 
'/---' + funcName + '-----' + ( info == undefined ? '' : info ) );
        _indent
++;
    }


    static private 
function OnCallEnd( funcName, info, ret )
    
{
        _indent
--;
        
var type = typeof( ret );
        
if( type == 'string' || type == 'number' || type == 'boolean' ){
            xtrace( 
'|---( ' + ret + ' )' );
        }

        xtrace( 
'/---' + funcName + '-----' + ( info == undefined ? '' : info ) );
    }


    static private 
var _classTraceOn = false;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值