Unity collects log tools

Source: Internet
Author: User
Tags deprecated log log

Reference

yusong:http://www.xuanyusong.com/archives/2477

Sandals: https://www.cnblogs.com/liangxiegame/p/Unity-you-xi-kuang-jia-da-jian-ba-jian-shao-jia-ba.html

According to the information on the Internet to integrate and change the whole

Using thread brushes

The generated files are in the Persistentdatapath directory

usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingSystem.Threading; Public classoutlog:monobehaviour{Private ObjectMloglock =NULL; PrivateThread Mfilelogthread =NULL; Staticlist<string> mlines =Newlist<string>(); Staticlist<string> mwritetxt =Newlist<string>(); Private stringOutpath; /// <summary>    ///You can also manually control this manual start/// </summary>    voidStart () {Mloglock=New Object(); //Application.persistentdatapath Unity is the only path that can be read or written. Outpath = Application.persistentdatapath +"/outlog.txt"; //log saved before each startup client is deleted        if(System.IO.File.Exists (Outpath)) {file.delete (Outpath); }        //do a log listener here (old method, deprecated)//Application.registerlogcallback (Handlelog); //using thread brushesapplication.logmessagereceivedthreaded + =Handlelog;  This. Mfilelogthread =NewThread (NewThreadStart (Writelog));  This. Mfilelogthread.start (); //an outputDebug.Log ("==============unity Client log log ========="); }    /// <summary>    ///thread Brushes/// </summary>    voidWritelog () { while(true)        {            //Thread Lock            Lock(mloglock) {if(Mwritetxt.count >0)                {                    string[] temp =Mwritetxt.toarray (); foreach(stringTinchtemp) {                        using(StreamWriter writer =NewStreamWriter (Outpath,true, Encoding.UTF8)) {writer.                        WriteLine (t);                    } mwritetxt.remove (t); }                }            }        }    }    /// <summary>    ///use Update to brush (deprecated)/// </summary>    voidUpdatenotuse () {//because the operation to write to the file must be done in the main thread, in the update, you write the file.         if(Mwritetxt.count >0)        {            string[] temp =Mwritetxt.toarray (); foreach(stringTinchtemp) {                using(StreamWriter writer =NewStreamWriter (Outpath,true, Encoding.UTF8)) {writer.                WriteLine (t);            } mwritetxt.remove (t); }        }    }     Public Static stringGetHead () {return "["+ System.DateTime.Now.ToString ("yyyy-mm-dd HH:mm:ss FFF") +"] "; }    voidHandlelog (stringLogstring,stringstackTrace, LogType type) {Mwritetxt.add (GetHead ()+logstring); if(type = = Logtype.error | | type = =logtype.exception) {//Log (logstring); //Log (stackTrace);Mwritetxt.add ("ERROR:"+stackTrace); }    }    //Here I save the wrong information for output on the phone screen (temporarily off)    Static  Public voidLog (params Object[] objs) {        stringText ="";  for(inti =0; I < Objs. Length; ++i) {if(i = =0) {text+=Objs[i].            ToString (); }            Else{text+=", "+Objs[i].            ToString (); }        }        if(application.isplaying) {if(Mlines.count > -) {mlines.removeat (0);        } mlines.add (text); }    }    voidOngui () {Gui.color=color.red;  for(inti =0, IMAX = Mlines.count; I < IMAX; ++i) {Guilayout.label (mlines[i]); }    }}

Unity collects log tools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.