Log.js 9.39 KB
var Network = require('Network');
var Common  = require('Common');
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _iLogCount:0,
        _oScene:null,
        
        
    },

    init: function (oScene) {
        try {
            this._oScene=oScene;
        } catch (error) {
            if (cc.sys.isNative){
                jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","init Error in Log==>"+error);
            }
        }
        
    },
    // use this for initialization
    onLoad: function () {
        try {
            //设置本身节点的宽高透明
            this.node.x=0;
            this.node.y=0;
            //this.node.width=Common.SCREEN_WIDTH-20;
            //this.node.height=Common.SCREEN_HEIGHT;


            this.node.width=Common.SCREEN_HEIGHT;
            this.node.height=Common.SCREEN_WIDTH;
            
            let nodeLogMask=new cc.Node('LogMask');
            nodeLogMask.x=0;
            nodeLogMask.y=0;

            this.node.addChild(nodeLogMask,10);
            
            //设置为全屏遮罩
            let strLoadImagePath = 'Texture/splash_black';
            Common.renderSpriteByLocalImage(strLoadImagePath,nodeLogMask.addComponent(cc.Sprite));

            //因为纹理2*2所以这里写死了
            //nodeLogMask.setScale(Common.SCREEN_WIDTH/2,Common.SCREEN_HEIGHT/2);
            nodeLogMask.setScale(Common.SCREEN_HEIGHT/2,Common.SCREEN_WIDTH/2);
            //nodeLogMask.opacity=100;
            nodeLogMask.opacity = 100;
            nodeLogMask.zIndex = 400;

            let nodeLogList=new cc.Node('LogList');
            let layoutLog = nodeLogList.addComponent(cc.Layout);
            layoutLog.type = cc.Layout.Type.VERTICAL;
            layoutLog.resizeMode = cc.Layout.ResizeMode.CONTAINER;
            layoutLog.spacingY = 0;
            //nodeLogList.x = 20 - Common.SCREEN_WIDTH/2;
            //nodeLogList.y = Common.SCREEN_HEIGHT/2 - 20;
            nodeLogList.x = -Common.SCREEN_WIDTH/2+290;
            nodeLogList.y = Common.SCREEN_HEIGHT/2+270;
            nodeLogList.setAnchorPoint(0,1);
            this.node.addChild(nodeLogList,10);
            nodeLogList.zIndex = 402;

            //this.node.color=new cc.Color(0, 0, 0); //黑色
            //this.node.cascadeOpacity=false;
            
            //this.node.parent=this._oScene.node;

            //this.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(Texture2D.);

            //parentNode.cascadeOpacity=false;
        } catch (error) {
            if (cc.sys.isNative){
                jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","onLoad Error in Log==>"+error);
            }
        }
        
    },

    //statics: {
        
        
        serverD:function(strInfo){
            
        },
        screenD:function(strInfo){
            try {
                this.flushLogToScreen(strInfo,'Debug');
            } catch (error) {
                if (cc.sys.isNative){
                    jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","screenD Error in Log==>"+error);
                }
            }
            
        },
        serverI:function(strInfo){

        },
        screenI:function(strInfo){
            try {
                if(strInfo){
                    this.flushLogToScreen(strInfo,'Info');
                    cc.log("Info:"+strInfo);
                    if (cc.sys.isNative){
                        jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V",strInfo);
                    }
                }
            } catch (error) {
                if (cc.sys.isNative){
                    jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","screenI Error in Log==>"+error);
                }
            }
            

        },
        serverW:function(strInfo){

        },
        screenW:function(strInfo){
            try {
                if(strInfo){
                    this.flushLogToScreen(strInfo,'Warning');
                    cc.log("Warning:"+strInfo);
                    if (cc.sys.isNative){
                        jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneW", "(Ljava/lang/String;)V",strInfo);
                    }
                }
            } catch (error) {
                if (cc.sys.isNative){
                    jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","screenW Error in Log==>"+error);
                }
            }
            

        },
        serverE:function(strInfo){

        },
        screenE:function(strInfo){
            try {
                if(strInfo){
                    this.flushLogToScreen(strInfo,'Error');
                    cc.log("strInfo:"+strInfo);
                    if (cc.sys.isNative){
                        jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneE", "(Ljava/lang/String;)V",strInfo);
                    }
                }
            } catch (error) {
                if (cc.sys.isNative){
                    jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","screenE Error in Log==>"+error);
                }
            }
            
        },

        flushLogToScreen:function(strInfo, strLevel){
            try {
                if(!Common.DEBUG_MODE){//todo:是否还有把日志显示在屏幕上的必要,目前是觉得没有的
                    return;
                }
                this._iLogCount++;
                let nodeLogList = this.node.getChildByName("LogList");
                //this._iLogCount = this._iLogCount%100;
                let nodeInfo=new cc.Node('log_info_'+this._iLogCount);
                let lblInfo=nodeInfo.addComponent(cc.Label);
                lblInfo.string="("+this._iLogCount+"):"+strLevel+':'+strInfo;//+(nodeInfo.width/2-Common.SCREEN_WIDTH/2);
                lblInfo.fontSize=10;
                lblInfo.lineHeight=10;
                //nodeInfo.parent=this.node;
                nodeInfo.parent=nodeLogList;
                switch(strLevel){
                    case 'Debug':
                        nodeInfo.color=new cc.color(51,255,255,255);
                        break;
                    case 'Info':
                        nodeInfo.color=new cc.color(51,255,51,255);
                        break;
                    case 'Warning':
                        nodeInfo.color=new cc.color(255,255,51,255);
                        break;
                    case 'Error':
                        nodeInfo.color=new cc.color(255,51,51,255);
                        break;
                    default: 
                        nodeInfo.color=new cc.color(255,255,255,255);
    
                }
                
                //nodeInfo.color=new cc.color(255,255,255,255);
                //nodeInfo.height=20;
                //nodeInfo.y = -(this._iLogCount-1)*20;
                nodeInfo.y = -(this._iLogCount-1)*10;
                nodeInfo.height=10;
                nodeInfo.setAnchorPoint(0,1);
                
                
                nodeInfo.zIndex = 401;
                //if(nodeLogList.height > Common.SCREEN_HEIGHT -20){
                if(nodeLogList.height > Common.SCREEN_WIDTH){
                    nodeLogList.children[0].destroy();
                }
            } catch (error) {
                if (cc.sys.isNative){
                    jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","flushLogToScreen Error in Log==>"+error);
                }
            }
            
        },
        clearLogOnScreen : function () {
            try {
                let nodeLogList = this.node.getChildByName("LogList");
                nodeLogList.destroy();
                this._iLogCount = 0;
                nodeLogList=new cc.Node('LogList');
                nodeLogList.x = 20 - Common.SCREEN_WIDTH/2;
                nodeLogList.y = Common.SCREEN_HEIGHT/2 - 20;
                nodeLogList.setAnchorPoint(0,1);
                this.node.addChild(nodeLogList,10);
                nodeLogList.zIndex = 402;
            } catch (error) {
                if (cc.sys.isNative){
                    jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "logInSceneI", "(Ljava/lang/String;)V","clearLogOnScreen Error in Log==>"+error);
                }
            }
            
        },
        flushLogToServer:function(){


        }
        //TODO:需要加一个清除(所有条目全删除)和删除日志(一条一条删除)的函数
    //}
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});