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

Last change on this file since 435 was 434, checked in by alloc, 19 months ago

Added permission management APIs

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);
[434]66 if (logEntries.Count <= maxEntries) {
67 return;
[391]68 }
[434]69
70 listOffset += logEntries.Count - maxEntries;
71 logEntries.RemoveRange (0, logEntries.Count - maxEntries);
[391]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 {
[434]128 public readonly DateTime Timestamp;
129 public readonly string IsoTime;
130 public readonly string Message;
131 public readonly string Trace;
132 public readonly LogType Type;
133 public readonly long Uptime;
[391]134
135 public LogEntry (DateTime _timestamp, string _message, string _trace, LogType _type, long _uptime) {
[434]136 Timestamp = _timestamp;
137 IsoTime = _timestamp.ToString ("o");
[391]138
[434]139 Message = _message;
140 Trace = _trace;
141 Type = _type;
142 Uptime = _uptime;
[391]143 }
144 }
145 }
146}
Note: See TracBrowser for help on using the repository browser.