Laya-定义游戏里的输出类,方便游戏调试

Laya-定义游戏里的输出类,方便游戏调试

export class Debug{
    private static timeMap:Object = {};
    private static disableMap:Object = {};
    private static list:Array<string> = [];//日志
    private static levels:Array<string> = ["debug","log","info","warn","error"];//日志输出等级
    private static maxSize:number = 1000;//日志最大长度
    private static hookMap:Object = {};
    public static hook(level:string,handler:Laya.Handler):void{
        this.hookMap[level.toLowerCase()] = handler;
    }
    public static unHook(level:string = null):void{
        let handler:Laya.Handler;
        if(level){
            handler = this.hookMap[level];
            if(handler){
                delete this.hookMap[level];
                handler.recover();
            }
        }else{
            for(let key in this.hookMap){
                this.hookMap[key].recover();
            }
            this.hookMap = {};
        }
    }
    /* 获取日志列表 */
    public static getList():Array<string>{
        return this.list;
    }
    /* 获取日志最大长度 */
    public static getMaxSize():number{
        return this.maxSize;
    }
    /* 设置日志最大长度 */
    public static setMaxSize(value:number):void{
        this.maxSize = value;
        if(this.list.length > this.maxSize){
            this.list.splice(0,this.list.length - this.maxSize);
        }
    }
    /* 清空历史记录 */
    public static clear():void{
        this.list = [];
    }
    /* 激活所有输出 */
    public static eableAll():void{
        let level:string;
        for(let i:number = 0; i < this.levels.length; i++){
            this.disableMap[this.levels[i]] = false;
        }
    }
    /* 激活某个输出 */
    public static enableOnly(level:string):void{
        for(let i:number = 0; i < this.levels.length; i++){
            if(this.levels[i] != level){
                this.disableMap[this.levels[i]] = true;
            }else{
                this.disableMap[this.levels[i]] = false;
            }
        }
    }
    /* 激活或停止某个输出 */
    public static updateLevelEnable(level:string,value:boolean){
        if(this.levels.indexOf(level) != -1){
            this.disableMap[level] = value;
        }
    }
    public static show(key:string, ...rest):void{
        this.send(key,rest);
    }
    public static log( ...rest):void{
        this.send("log",rest);
    }
    public static debug( ...rest):void{
        this.send("debug",rest);
    }
    public static info( ...rest):void{
        this.send("info",rest);
    }
    public static warn( ...rest):void{
        this.send("warn",rest);
    }
    public static error( ...rest):void{
        this.send("error",rest);
    }
    public static time(name:string):void{
        this.send("time",[name]);
    }
    public static timeEnd(name:string):void{
        this.send("timeEnd",[name]);
    }
    private static send(level:string,rest:any){
        if(this.disableMap[level] == true){
            return;
        }
        if(this.list.length > this.maxSize){
            this.list.shift();
        }
        let txt:string;
        if(level == "time") {
            let start:string = rest[0].toString();
            this.timeMap[start] = Laya.Browser.now();
            txt = "[time] " + start + ": start";
        } else if(level == "timeEnd") {
            let end:string = rest[0].toString();
            let before:number = this.timeMap[end];
            if(!isNaN(before)) {
                delete this.timeMap[end];
                txt = "[time] " + end + ": " + (Laya.Browser.now() - before) + "ms";
            }
        } else{
            txt = "[" + level + "]";
            for(let i:number = 0; i < rest.length; i++) {
                if(i == 0) {
                    txt += rest[i];
                } else {
                    txt += ", " + rest[i];
                }
            }
        }
        if(txt != null) {
            let handler:Laya.Handler = this.hookMap[level.toLowerCase()];
            if(handler!=null) {
                handler.method.apply(handler.caller, [txt]);
            }
            this.list.push(txt);
            console.log(txt);
        }
    }
}

 

posted on 2021-01-09 00:57  行运茶快乐水  阅读(177)  评论(0编辑  收藏  举报

导航