source: binary-improvements2/WebServer/src/LogBuffer.cs@ 415

Last change on this file since 415 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 3.0 KB
RevLine 
[391]1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
5namespace Webserver {
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 instance ??= new LogBuffer ();
17 }
18
19 private LogBuffer () {
20 Log.LogCallbacksExtended += LogCallback;
21 }
22
23 public static LogBuffer Instance => instance ??= new LogBuffer ();
24
25 public int OldestLine {
26 get {
27 lock (logEntries) {
28 return listOffset;
29 }
30 }
31 }
32
33 public int LatestLine {
34 get {
35 lock (logEntries) {
36 return listOffset + logEntries.Count - 1;
37 }
38 }
39 }
40
41 public int StoredLines {
42 get {
43 lock (logEntries) {
44 return logEntries.Count;
45 }
46 }
47 }
48
49 public LogEntry this [int _index] {
50 get {
51 lock (logEntries) {
52 if (_index >= listOffset && _index < listOffset + logEntries.Count) {
53 return logEntries [_index];
54 }
55 }
56
57 return null;
58 }
59 }
60
61 private void LogCallback (string _formattedMsg, string _plainMsg, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
62 LogEntry le = new LogEntry (_timestamp, _plainMsg, _trace, _type, _uptime);
63
64 lock (logEntries) {
65 logEntries.Add (le);
66 if (logEntries.Count > maxEntries) {
67 listOffset += logEntries.Count - maxEntries;
68 logEntries.RemoveRange (0, logEntries.Count - maxEntries);
69 }
70 }
71 }
72
73 private readonly List<LogEntry> emptyList = new List<LogEntry> ();
74
75 public List<LogEntry> GetRange (ref int _start, int _count, out int _end) {
76 lock (logEntries) {
77 int index;
78
79 if (_count < 0) {
80 _count = -_count;
81
82 if (_start >= listOffset + logEntries.Count) {
83 _start = listOffset + logEntries.Count - 1;
84 }
85
86 _end = _start;
87
88 if (_start < listOffset) {
89 return emptyList;
90 }
91
92 _start -= _count - 1;
93
94 if (_start < listOffset) {
95 _start = listOffset;
96 }
97
98 index = _start - listOffset;
99 _end += 1;
100 _count = _end - _start;
101 } else {
102 if (_start < listOffset) {
103 _start = listOffset;
104 }
105
106 if (_start >= listOffset + logEntries.Count) {
107 _end = _start;
108 return emptyList;
109 }
110
111 index = _start - listOffset;
112
113 if (index + _count > logEntries.Count) {
114 _count = logEntries.Count - index;
115 }
116
117 _end = _start + _count;
118 }
119
120 return logEntries.GetRange (index, _count);
121 }
122 }
123
124
125 public class LogEntry {
126 public readonly DateTime timestamp;
127 public readonly string isoTime;
128 public readonly string message;
129 public readonly string trace;
130 public readonly LogType type;
131 public readonly long uptime;
132
133 public LogEntry (DateTime _timestamp, string _message, string _trace, LogType _type, long _uptime) {
134 timestamp = _timestamp;
135 isoTime = _timestamp.ToString ("o");
136
137 message = _message;
138 trace = _trace;
139 type = _type;
140 uptime = _uptime;
141 }
142 }
143 }
144}
Note: See TracBrowser for help on using the repository browser.