yyl
17 小时以前 9c7efb57bf1b21954c7d81b705c8a8a34e7fb364
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Threading;
 
 
public class RunTimeExceptionUtility : Singleton<RunTimeExceptionUtility>
 
{
 
    List<string> logWriteToFileTemp = new List<string>();
    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();
            }
        }
    }
 
 
}