[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 ();
|
---|
| 21 | };
|
---|
| 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 |
|
---|
| 62 | public LogEntry this [int index] {
|
---|
| 63 | get {
|
---|
| 64 | lock (logEntries) {
|
---|
| 65 | if (index >= listOffset && index < listOffset + logEntries.Count) {
|
---|
| 66 | return logEntries [index];
|
---|
| 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 |
|
---|
[253] | 103 | public List<LogEntry> GetRange (ref int _start, int _count, out int _end) {
|
---|
[250] | 104 | lock (logEntries) {
|
---|
[253] | 105 | if (_count < 1) {
|
---|
| 106 | _end = _start;
|
---|
| 107 | return new List<LogEntry> ();
|
---|
[250] | 108 | }
|
---|
| 109 |
|
---|
| 110 | if (_start < listOffset) {
|
---|
| 111 | _start = listOffset;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if (_start >= listOffset + logEntries.Count) {
|
---|
| 115 | _end = _start;
|
---|
| 116 | return new List<LogEntry> ();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[270] | 119 | int index = _start - listOffset;
|
---|
| 120 |
|
---|
| 121 | if (index + _count > logEntries.Count) {
|
---|
| 122 | _count = logEntries.Count - index;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[253] | 125 | _end = _start + _count;
|
---|
[270] | 126 |
|
---|
| 127 | return logEntries.GetRange (index, _count);
|
---|
[250] | 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | public class LogEntry {
|
---|
| 133 | public string date;
|
---|
[325] | 134 | public string message;
|
---|
[250] | 135 | public string time;
|
---|
| 136 | public string trace;
|
---|
| 137 | public LogType type;
|
---|
[325] | 138 | public string uptime;
|
---|
[250] | 139 | }
|
---|
| 140 | }
|
---|
[325] | 141 | }
|
---|