-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cs
More file actions
94 lines (82 loc) · 2.93 KB
/
Copy pathLog.cs
File metadata and controls
94 lines (82 loc) · 2.93 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using UnityEngine;
using System.Collections;
namespace dpull
{
public class Log : MonoBehaviour
{
private System.IO.StreamWriter LogWriter;
void Start ()
{
CleanOldLog();
Application.RegisterLogCallback(HandleLog);
}
void OnDestory()
{
Application.RegisterLogCallback(null);
}
string GetLogDirectory()
{
var path = System.IO.Path.Combine(Application.persistentDataPath, "logs");
if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
return path;
}
void CleanOldLog()
{
var dir = GetLogDirectory();
var files = System.IO.Directory.GetFiles(dir, "*.log", System.IO.SearchOption.AllDirectories);
var now = System.DateTime.Now;
var expirationTime = new System.TimeSpan(24 * 3, 0, 0);
foreach (var file in files)
{
var fileInfo = new System.IO.FileInfo(file);
var subTime = now - fileInfo.LastWriteTime;
if (subTime < expirationTime)
continue;
try
{
System.IO.File.Delete(file);
}
catch
{
}
}
}
void HandleLog(string logString, string stackTrace, LogType type)
{
if (type == LogType.Log && !logString.StartsWith("Lua:"))
return;
if (LogWriter == null)
{
lock(this)
{
if (LogWriter == null)
{
try
{
var logfile = GetLogDirectory();
logfile = System.IO.Path.Combine(logfile, string.Format("{0}.log", System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")));
LogWriter = new System.IO.StreamWriter(logfile);
}
catch
{
}
}
}
}
if (LogWriter != null)
{
this.SendMessage("CacheLog", new string[]{logString, stackTrace}, SendMessageOptions.DontRequireReceiver);
string log = string.Format("{0}<{1}>: {2}", System.DateTime.Now.ToString("MM-dd HH:mm:ss"), type.ToString(), logString);
LogWriter.WriteLine(log);
if (!string.IsNullOrEmpty(stackTrace))
{
var formatStackTrace = stackTrace.Replace("\n", "\n\t");
LogWriter.Write("\t");
LogWriter.WriteLine(formatStackTrace);
}
LogWriter.Flush();
}
}
}
}