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