source: binary-improvements2/MapRendering/Web/LogBuffer.cs@ 386

Last change on this file since 386 was 382, checked in by alloc, 2 years ago

Switched to use SpaceWizards.HttpListener

File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
5namespace AllocsFixes.NetConnections.Servers.Web {
6 public class LogBuffer {
7 private const int maxEntries = 3000;
8
9 private static LogBuffer instance;
10
11 private readonly List<LogEntry> logEntries = new List<LogEntry> ();
12
13 private int listOffset;
14
15 public static void Init () {
16 if (instance == null) {
17 instance = new LogBuffer ();
18 }
19 }
20
21 private LogBuffer () {
22 Log.LogCallbacksExtended += LogCallback;
23 }
24
25 public static LogBuffer Instance => instance ?? (instance = new LogBuffer ());
26
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
51 public LogEntry this [int _index] {
52 get {
53 lock (logEntries) {
54 if (_index >= listOffset && _index < listOffset + logEntries.Count) {
55 return logEntries [_index];
56 }
57 }
58
59 return null;
60 }
61 }
62
63 private void LogCallback (string _formattedMsg, string _plainMsg, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
64 LogEntry le = new LogEntry (_timestamp, _plainMsg, _trace, _type, _uptime);
65
66 lock (logEntries) {
67 logEntries.Add (le);
68 if (logEntries.Count > maxEntries) {
69 listOffset += logEntries.Count - maxEntries;
70 logEntries.RemoveRange (0, logEntries.Count - maxEntries);
71 }
72 }
73 }
74
75 private readonly List<LogEntry> emptyList = new List<LogEntry> ();
76
77 public List<LogEntry> GetRange (ref int _start, int _count, out int _end) {
78 lock (logEntries) {
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
88 _end = _start;
89
90 if (_start < listOffset) {
91 return emptyList;
92 }
93
94 _start -= _count - 1;
95
96 if (_start < listOffset) {
97 _start = listOffset;
98 }
99
100 index = _start - listOffset;
101 _end += 1;
102 _count = _end - _start;
103 } else {
104 if (_start < listOffset) {
105 _start = listOffset;
106 }
107
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;
120 }
121
122 return logEntries.GetRange (index, _count);
123 }
124 }
125
126
127 public class LogEntry {
128 public readonly DateTime timestamp;
129 public readonly string date;
130 public readonly string time;
131 public readonly string isoTime;
132 public readonly string message;
133 public readonly string trace;
134 public readonly LogType type;
135 public readonly long uptime;
136
137 public LogEntry (DateTime _timestamp, string _message, string _trace, LogType _type, long _uptime) {
138 timestamp = _timestamp;
139 date = $"{_timestamp.Year:0000}-{_timestamp.Month:00}-{_timestamp.Day:00}";
140 time = $"{_timestamp.Hour:00}:{_timestamp.Minute:00}:{_timestamp.Second:00}";
141 isoTime = _timestamp.ToString ("o");
142
143 message = _message;
144 trace = _trace;
145 type = _type;
146 uptime = _uptime;
147 }
148 }
149 }
150}
Note: See TracBrowser for help on using the repository browser.