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

Last change on this file since 333 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

File size: 2.9 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 public List<LogEntry> GetRange (ref int _start, int _count, out int _end) {
104 lock (logEntries) {
105 if (_count < 1) {
106 _end = _start;
107 return new List<LogEntry> ();
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
119 int index = _start - listOffset;
120
121 if (index + _count > logEntries.Count) {
122 _count = logEntries.Count - index;
123 }
124
125 _end = _start + _count;
126
127 return logEntries.GetRange (index, _count);
128 }
129 }
130
131
132 public class LogEntry {
133 public string date;
134 public string message;
135 public string time;
136 public string trace;
137 public LogType type;
138 public string uptime;
139 }
140 }
141}
Note: See TracBrowser for help on using the repository browser.