using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.IO; using System.Threading; public class RunTimeExceptionUtility : Singleton { List logWriteToFileTemp = new List(); DateTime nextWriteFileTime = DateTime.Now; string logFileRoot = string.Empty; public void Init() { GlobalTimeEvent.Instance.secondEvent -= OnPerSecond; Application.logMessageReceivedThreaded -= AddLogRenderer; if (!DebugUtility.Instance.debugAccount) { return; } if (Application.platform == RuntimePlatform.Android) { logFileRoot = Path.GetDirectoryName(Application.persistentDataPath); } else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor) { logFileRoot = Path.GetDirectoryName(Application.dataPath) + "/RunTimeLog"; } else { return; } nextWriteFileTime = DateTime.Now + new TimeSpan(300 * TimeSpan.TicksPerSecond); GlobalTimeEvent.Instance.secondEvent += OnPerSecond; Application.logMessageReceivedThreaded += AddLogRenderer; } public void UnInit() { GlobalTimeEvent.Instance.secondEvent -= OnPerSecond; Application.logMessageReceivedThreaded -= AddLogRenderer; } private void AddLogRenderer(string _log, string _callStack, LogType _type) { logWriteToFileTemp.Add(string.Format("{0}--{1}:Thread:{2}-{3};{4}{5}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), _type, Thread.CurrentThread.ManagedThreadId, _log, _callStack, "\r\n")); } private void OnPerSecond() { if (DateTime.Now > nextWriteFileTime) { nextWriteFileTime = DateTime.Now + new TimeSpan(300 * TimeSpan.TicksPerSecond); if (logWriteToFileTemp.Count > 0) { if (!Directory.Exists(logFileRoot)) { Directory.CreateDirectory(logFileRoot); } var path = logFileRoot + "/" + "RunTimeLog_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm") + ".txt"; File.WriteAllLines(path, logWriteToFileTemp.ToArray()); logWriteToFileTemp.Clear(); } } } }