-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogMemoryStorage.cs
More file actions
112 lines (95 loc) · 2.5 KB
/
Copy pathLogMemoryStorage.cs
File metadata and controls
112 lines (95 loc) · 2.5 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using UnityEngine;
public class LogMemoryStorage
{
private static LogMemoryStorage _instance;
public static LogMemoryStorage Instance
{
get
{
if (_instance == null)
_instance = new LogMemoryStorage();
return _instance;
}
}
public class LogEntity
{
public int Id;
public DateTime Time;
public string Condition;
public string StackTrace;
public LogType Type;
}
private bool _installed;
private int _lastLogId;
private int _maxLogCount;
private List<LogEntity> _logs = new List<LogEntity>();
public bool Installed
{
get { return _installed; }
}
public int LastLogId
{
get { return _lastLogId; }
}
public int MaxLogCount
{
get { return _maxLogCount; }
set { _maxLogCount = value; }
}
public LogMemoryStorage(int maxLogCount = 0)
{
_maxLogCount = maxLogCount;
}
public void Install()
{
if (_installed)
return;
_installed = true;
Application.logMessageReceivedThreaded += OnLogMessageReceive;
}
public void Uninstall()
{
if (_installed == false)
return;
_installed = false;
Application.logMessageReceivedThreaded -= OnLogMessageReceive;
}
public List<LogEntity> GetLogs(ref int baseLogId)
{
lock (_logs)
{
if (_logs.Count == 0)
return null;
var lastLogId = _logs[_logs.Count - 1].Id;
var newLogCount = lastLogId - baseLogId;
if (newLogCount <= 0)
return null;
baseLogId = lastLogId;
var count = Math.Min(_logs.Count, newLogCount);
return _logs.GetRange(_logs.Count - count, count);
}
}
private void OnLogMessageReceive(string condition, string stacktrace, LogType type)
{
lock (_logs)
{
_lastLogId += 1;
_logs.Add(new LogEntity
{
Id = _lastLogId,
Time = DateTime.Now,
Condition = condition,
StackTrace = stacktrace,
Type = type
});
if (_maxLogCount > 0)
{
var removeCount = _logs.Count - _maxLogCount;
if (removeCount > 0)
_logs.RemoveRange(0, removeCount);
}
}
}
}