source: binary-improvements/MapRendering/Web/LogBuffer.cs@ 363

Last change on this file since 363 was 350, checked in by alloc, 6 years ago

Fixed #154

File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text.RegularExpressions;
4using UnityEngine;
5
6namespace AllocsFixes.NetConnections.Servers.Web {
7 public class LogBuffer {
8 private const int MAX_ENTRIES = 3000;
9 private static LogBuffer instance;
10
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
18 public static void Init () {
19 if (instance == null) {
20 instance = new LogBuffer ();
21 }
22 }
23
24 private LogBuffer () {
25 Logger.Main.LogCallbacks += LogCallback;
26 }
27
28 public static LogBuffer Instance {
29 get {
30 if (instance == null) {
31 instance = new LogBuffer ();
32 }
33
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 }
69
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
103 private readonly List<LogEntry> emptyList = new List<LogEntry> ();
104
105 public List<LogEntry> GetRange (ref int _start, int _count, out int _end) {
106 lock (logEntries) {
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
116 _end = _start;
117
118 if (_start < listOffset) {
119 return emptyList;
120 }
121
122 _start -= _count - 1;
123
124 if (_start < listOffset) {
125 _start = listOffset;
126 }
127
128 index = _start - listOffset;
129 _end += 1;
130 _count = _end - _start;
131 } else {
132 if (_start < listOffset) {
133 _start = listOffset;
134 }
135
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;
148 }
149
150 return logEntries.GetRange (index, _count);
151 }
152 }
153
154
155 public class LogEntry {
156 public string date;
157 public string message;
158 public string time;
159 public string trace;
160 public LogType type;
161 public string uptime;
162 }
163 }
164}
Note: See TracBrowser for help on using the repository browser.