| 1 | using System; | 
|---|
| 2 | using System.Collections.Generic; | 
|---|
| 3 | using UnityEngine; | 
|---|
| 4 |  | 
|---|
| 5 | namespace AllocsFixes.NetConnections.Servers.Web { | 
|---|
| 6 | public class LogBuffer { | 
|---|
| 7 | private const int MAX_ENTRIES = 3000; | 
|---|
| 8 | private static LogBuffer instance; | 
|---|
| 9 |  | 
|---|
| 10 | private readonly List<LogEntry> logEntries = new List<LogEntry> (); | 
|---|
| 11 |  | 
|---|
| 12 | private int listOffset; | 
|---|
| 13 |  | 
|---|
| 14 | public static void Init () { | 
|---|
| 15 | if (instance == null) { | 
|---|
| 16 | instance = new LogBuffer (); | 
|---|
| 17 | } | 
|---|
| 18 | } | 
|---|
| 19 |  | 
|---|
| 20 | private LogBuffer () { | 
|---|
| 21 | Log.LogCallbacksExtended += LogCallback; | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 | public static LogBuffer Instance { | 
|---|
| 25 | get { | 
|---|
| 26 | if (instance == null) { | 
|---|
| 27 | instance = new LogBuffer (); | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | return instance; | 
|---|
| 31 | } | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | public int OldestLine { | 
|---|
| 35 | get { | 
|---|
| 36 | lock (logEntries) { | 
|---|
| 37 | return listOffset; | 
|---|
| 38 | } | 
|---|
| 39 | } | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | public int LatestLine { | 
|---|
| 43 | get { | 
|---|
| 44 | lock (logEntries) { | 
|---|
| 45 | return listOffset + logEntries.Count - 1; | 
|---|
| 46 | } | 
|---|
| 47 | } | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | public int StoredLines { | 
|---|
| 51 | get { | 
|---|
| 52 | lock (logEntries) { | 
|---|
| 53 | return logEntries.Count; | 
|---|
| 54 | } | 
|---|
| 55 | } | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | public LogEntry this [int _index] { | 
|---|
| 59 | get { | 
|---|
| 60 | lock (logEntries) { | 
|---|
| 61 | if (_index >= listOffset && _index < listOffset + logEntries.Count) { | 
|---|
| 62 | return logEntries [_index]; | 
|---|
| 63 | } | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | return null; | 
|---|
| 67 | } | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | private void LogCallback (string _formattedMsg, string _plainMsg, string _trace, LogType _type, DateTime _timestamp, long _uptime) { | 
|---|
| 71 | LogEntry le = new LogEntry (); | 
|---|
| 72 |  | 
|---|
| 73 | le.date = $"{_timestamp.Year:0000}-{_timestamp.Month:00}-{_timestamp.Day:00}"; | 
|---|
| 74 | le.time = $"{_timestamp.Hour:00}:{_timestamp.Minute:00}:{_timestamp.Second:00}"; | 
|---|
| 75 | le.uptime = _uptime.ToString (); | 
|---|
| 76 | le.message = _plainMsg; | 
|---|
| 77 | le.trace = _trace; | 
|---|
| 78 | le.type = _type; | 
|---|
| 79 |  | 
|---|
| 80 | lock (logEntries) { | 
|---|
| 81 | logEntries.Add (le); | 
|---|
| 82 | if (logEntries.Count > MAX_ENTRIES) { | 
|---|
| 83 | listOffset += logEntries.Count - MAX_ENTRIES; | 
|---|
| 84 | logEntries.RemoveRange (0, logEntries.Count - MAX_ENTRIES); | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | private readonly List<LogEntry> emptyList = new List<LogEntry> (); | 
|---|
| 90 |  | 
|---|
| 91 | public List<LogEntry> GetRange (ref int _start, int _count, out int _end) { | 
|---|
| 92 | lock (logEntries) { | 
|---|
| 93 | int index; | 
|---|
| 94 |  | 
|---|
| 95 | if (_count < 0) { | 
|---|
| 96 | _count = -_count; | 
|---|
| 97 |  | 
|---|
| 98 | if (_start >= listOffset + logEntries.Count) { | 
|---|
| 99 | _start = listOffset + logEntries.Count - 1; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | _end = _start; | 
|---|
| 103 |  | 
|---|
| 104 | if (_start < listOffset) { | 
|---|
| 105 | return emptyList; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | _start -= _count - 1; | 
|---|
| 109 |  | 
|---|
| 110 | if (_start < listOffset) { | 
|---|
| 111 | _start = listOffset; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | index = _start - listOffset; | 
|---|
| 115 | _end += 1; | 
|---|
| 116 | _count = _end - _start; | 
|---|
| 117 | } else { | 
|---|
| 118 | if (_start < listOffset) { | 
|---|
| 119 | _start = listOffset; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | if (_start >= listOffset + logEntries.Count) { | 
|---|
| 123 | _end = _start; | 
|---|
| 124 | return emptyList; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | index = _start - listOffset; | 
|---|
| 128 |  | 
|---|
| 129 | if (index + _count > logEntries.Count) { | 
|---|
| 130 | _count = logEntries.Count - index; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | _end = _start + _count; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | return logEntries.GetRange (index, _count); | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | public class LogEntry { | 
|---|
| 142 | public string date; | 
|---|
| 143 | public string message; | 
|---|
| 144 | public string time; | 
|---|
| 145 | public string trace; | 
|---|
| 146 | public LogType type; | 
|---|
| 147 | public string uptime; | 
|---|
| 148 | } | 
|---|
| 149 | } | 
|---|
| 150 | } | 
|---|