zoukankan      html  css  js  c++  java
  • Unity3d-Socket之龙一编年史network.dll分析(2)-> CNetLogger

    今天呢,我们来学习下network.dll里面的CNetLogger类。

    顾名思义,这个类主要是debug用的,也许有些人会问了,干嘛要自己封装个debug,直接用unity自带的debug不就好了吗。

    其实系统自带的有很多个缺点:

    1.不能统一的开关控制debug功能,假设当你想关闭的bebug,不想再打印的时候,发现unity没有这个接口,如果自己封装个开关,那么就方便管理,想开就开,想关就关,so easy!

    2.unity的debug他只能打印到控制台,当我们发布游戏的时候,那么debug就毫无意义了,所以我们把debug的内容统一追加到一个文本,那么这样就好管理许多。

    3.暂时想不到什么理由......

    so,综上所述:我们要自己动手封装个debug类。

    那么这个游戏dll的debug封装的还算可以,但是还是有许多不足的地方,相比较于其他游戏的debug,像暗黑。这里我不多说,怕被和谐。

    废话不多说,下面我们来看看代码怎么实现?

    前面我们讲到了自己的debug两个特点:1.统一开关 2.写入文件,这样我们就知道自己要定义什么变量。

        private FileStream fileStream = null;//文件流
        private string filePath;//文本目录
        private StreamWriter streamWriter = null;//写入的流对象
        private int lastSaveTime = 0;//保存时间(毫秒)
        private bool enable = false;//统一开关,默认为false
    

    然后我们在构造函数中初始化,

    public CNetLog(string dicPath, bool enable)//文件目录,不包括文件名和后缀
        {
            this.enable = enable;
            if (enable)
            {
                try
                {
                    string text = "network.txt";//文件名,包括后缀
                    if (0 == dicPath.Length)
                    {
                        this.filePath = text;//如果目录为空,那么就会在游戏的相对路径下,也就是跟Assets文件夹同级
                    }
                    else 
                    {
                        if (dicPath[dicPath.Length - 1] == '\' || dicPath[dicPath.Length-1] == '/')//这个是比如C://,或者C://Unity/
                        {
                            this.filePath = dicPath + text;
                        }
                        else
                        {
                            this.filePath = dicPath + "/" + text;//也就是dicPath后缀没有加斜杠的格式
                        }
                    }
                    this.fileStream = new FileStream(this.filePath, FileMode.Append, FileAccess.Write);//FileStream的构造方法,有好多种形式https://msdn.microsoft.com/zh-cn/library/tyhc0kft(v=vs.110).aspx去这个网站查api
                    this.streamWriter = new StreamWriter(this.fileStream, Encoding.Unicode);
                    this.lastSaveTime = Environment.TickCount;//自从系统开机到现在经过的毫秒数
                }
                catch (Exception e)
                {
                    this.enable = false;
                }
            }
        }
    

    ok,写完构造函数,我们发现什么事情都干不了,所以接下来我们来写工作方法,也就是LogInfo(),LogError()等。。。

    public void LogInfo(string info)
        {
            if (this.enable)
            {
                Monitor.Enter(this.streamWriter);//多线程安全,自己百度
                int tickCount = Environment.TickCount;
                this.streamWriter.WriteLine(string.Concat(System.DateTime.Now.ToString(), "[", tickCount.ToString(), "] [Info]:", info));//格式为9/3/2015 3:52:22 PM[14923508] [Info]:Hello,World
                this.Save();//刷新
                this.lastSaveTime = tickCount;
                Monitor.Exit(this.streamWriter);
            }
        }
    

     

    private void Save()
        {
            if (this.enable)
            {
                try
                {
                    this.streamWriter.Flush();
                    this.fileStream.Flush();
                }
                catch (Exception e)
                {
                    System.Diagnostics.Trace.WriteLine(e.Message);
                }
            }
        }
    

    ok,大致的东西已经写完,我们来测试一下,新建一个cs为Test.cs

    public class Test : MonoBehaviour {
    
        CNetLog netLog;
    	void Start () {
            netLog = new CNetLog("C://", true);
            netLog.LogInfo("Hello,World");
    	}
    	
    
    	void Update () {
    	
    	}
    }
    

      

    运行,找到c盘下面的network.txt文件,打开可以看到成功了!

  • 相关阅读:
    在 Windows 上测试 Redis Cluster的集群填坑笔记
    vmware安装黑苹果教程
    微信支付v3发布到iis时的证书问题
    Linux下安装SQL Server 2016(连接篇SQL Server on linux)
    Linux下安装SQL Server 2016(连接篇SQL Server on linux)
    Linux下安装SQL Server 2016(安装篇SQL Server on linux)
    Linux下安装SQL Server 2016(准备篇SQL Server on linux)
    客服端与服务端APP支付宝支付接口联调的那些坑
    ASP.NET MVC]WebAPI应用支持HTTPS的经验总结
    .net平台下C#socket通信(中)
  • 原文地址:https://www.cnblogs.com/CaomaoUnity3d/p/4780395.html
Copyright © 2011-2022 走看看