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