zoukankan      html  css  js  c++  java
  • 打印log 保存log

    using UnityEngine;
    using System.Collections;
    using System.IO;
    using System;
    using System.Text;
    
    namespace SC.Debuger
    {
        /// <summary>
        /// log等级
        /// </summary>
        public enum LogLevel
        {
            None = 0,
            Debug = 1,
            Error = 2,
            Warning = 4,
            Exception = 8,
            All = LogLevel.Debug | LogLevel.Error | LogLevel.Warning | LogLevel.Exception
        }
    }
    
    namespace SC.Debuger
    {
        public class LogWriter
        {
    //        private string m_logPath = Application.persistentDataPath + "/log/";
            private string m_logPath = Environment.CurrentDirectory + "/log/";
            private string m_logFileName = "log_{0}.txt";
            private string m_logFilePath = string.Empty;
    
            public LogWriter()
            {
                if (!Directory.Exists(m_logPath))
                {
                    Directory.CreateDirectory(m_logPath);
                }
                this.m_logFilePath = this.m_logPath + string.Format(this.m_logFileName, DateTime.Today.ToString("yyyyMMdd"));
                Debug.Log (this.m_logFilePath);
            }
    
            public void ExcuteWrite(string content)
            {
                using (StreamWriter writer = new StreamWriter(m_logFilePath, true, Encoding.UTF8))
                {
                    writer.WriteLine(content);
                }
            }
        }
    }
    
    namespace SC.Debuger
    {
        public class LogHelper 
        {
            static public LogLevel m_logLevel = LogLevel.All;
            static LogWriter m_logWriter = new LogWriter();
    
            static LogHelper()
            {
                Application.logMessageReceived += ProcessExceptionReport;
            }
    
            private static void ProcessExceptionReport(string message, string stackTrace, LogType type)
            {
                LogLevel dEBUG = LogLevel.Debug;
                switch (type)
                {
                case LogType.Error:
                    dEBUG = LogLevel.Error;
                    break;
                case LogType.Assert:
                    dEBUG = LogLevel.Debug;
                    break;
                case LogType.Warning:
                    dEBUG = LogLevel.Warning;
                    break;
                case LogType.Log:
                    dEBUG = LogLevel.Debug;
                    break;
                case LogType.Exception:
                    dEBUG = LogLevel.Exception;
                    break;
                }
    
                if (dEBUG == (m_logLevel & dEBUG))
                {
                    Log(string.Concat(new object[] { " [", dEBUG, "]: ", message, '
    ', stackTrace }));
                }
            }
    
    
            /// <summary>
            /// 加上时间戳
            /// </summary>
            /// <param name="message"></param>
            private static void Log(string message)
            {
                string msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff") + message;
                m_logWriter.ExcuteWrite(msg);
            }
    
            static public void Log(object message)
            {
                Log(message, null);
            }
            static public void Log(object message, UnityEngine.Object context)
            {
                if (LogLevel.Debug == (m_logLevel & LogLevel.Debug))
                {
                    Debug.Log(message, context);
                }
            }
            static public void LogError(object message)
            {
                LogError(message, null);
            }
            static public void LogError(object message, UnityEngine.Object context)
            {
                if (LogLevel.Error == (m_logLevel & LogLevel.Error))
                {
                    Debug.LogError(message, context);
                }
            }
            static public void LogWarning(object message)
            {
                LogWarning(message, null);
            }
            static public void LogWarning(object message, UnityEngine.Object context)
            {
                if (LogLevel.Warning == (m_logLevel & LogLevel.Warning))
                {
                    Debug.LogWarning(message, context);
                }
            }
        }
    }
    //游戏上线之前把LogHelper.m_logLevel = LogLevel.None;Log就不显示了。
    //LogHelper.m_logLevel = LogLevel.Error;是显示Error。
    //LogHelper.m_logLevel = LogLevel.Debug | LogLevel.Error;同时显示Debug和Error。
    //private string m_logPath = Environment.CurrentDirectory + "/log/";
    //log文件是保存在 private string m_logPath = Application.persistentDataPath + "/log/";在手机上就是沙盒路径。
    //log文件是以天为单位,同一天的log会被记录到一个文件里面。
    
    //using UnityEngine;
    //using SC.Debuger;
    //
    //public class Test : MonoBehaviour
    //{
    //    void Start()
    //    {
    //        LogHelper.m_logLevel = LogLevel.All;
    //        LogHelper.Log("debug");
    //        LogHelper.LogError("error");
    //        LogHelper.LogWarning("warning");
    //
    //        GameObject go =  GameObject.Find("fsdfsd");
    //        GameObject.Instantiate(go);
    //    }
    //}
  • 相关阅读:
    Kettle Spoon 数据源连接中报ORA12505, TNS:listener does not currently know of SID given in connect descriptor的错误
    oracle创建用户和删除用户sql
    Kettle Spoon表输入使用变量
    Kettle Spoon 资源库连接后新建转换打不开,报Invalid byte 1 of 1byte UTF8 sequence异常
    oracle 误删除表/表中数据,恢复方法
    让ie兼容css3新属性backroundsize
    网页设计中为啥少用奇数字体?
    浏览器差异总结,可以用此判断浏览器版本(转)
    ie浏览器f12下调“浏览器模式”和“文档模式”的区别
    jquery的淡入淡出和隐藏,展示函数在ie中应用的bug
  • 原文地址:https://www.cnblogs.com/602147629/p/5344500.html
Copyright © 2011-2022 走看看