source: TFP-WebServer/WebServer/src/LogBuffer.cs@ 486

Last change on this file since 486 was 467, checked in by alloc, 15 months ago

21.1.16.2 WebServer release
Added "id"s to messages in log API / event
Added "baseUrl" to mods API for WebMods

File size: 3.2 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
[467]15 public static event Action<LogEntry> EntryAdded;
16
[391]17 public static void Init () {
18 instance ??= new LogBuffer ();
19 }
20
21 private LogBuffer () {
22 Log.LogCallbacksExtended += LogCallback;
23 }
24
25 public static LogBuffer 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) {
[467]64 lock (logEntries) {
65 LogEntry le = new LogEntry (listOffset + logEntries.Count, _timestamp, _plainMsg, _trace, _type, _uptime);
[391]66
67 logEntries.Add (le);
[467]68 EntryAdded?.Invoke (le);
69
[434]70 if (logEntries.Count <= maxEntries) {
71 return;
[391]72 }
[434]73
74 listOffset += logEntries.Count - maxEntries;
75 logEntries.RemoveRange (0, logEntries.Count - maxEntries);
[391]76 }
77 }
78
79 private readonly List<LogEntry> emptyList = new List<LogEntry> ();
80
81 public List<LogEntry> GetRange (ref int _start, int _count, out int _end) {
82 lock (logEntries) {
83 int index;
84
85 if (_count < 0) {
86 _count = -_count;
87
88 if (_start >= listOffset + logEntries.Count) {
89 _start = listOffset + logEntries.Count - 1;
90 }
91
92 _end = _start;
93
94 if (_start < listOffset) {
95 return emptyList;
96 }
97
98 _start -= _count - 1;
99
100 if (_start < listOffset) {
101 _start = listOffset;
102 }
103
104 index = _start - listOffset;
105 _end += 1;
106 _count = _end - _start;
107 } else {
108 if (_start < listOffset) {
109 _start = listOffset;
110 }
111
112 if (_start >= listOffset + logEntries.Count) {
113 _end = _start;
114 return emptyList;
115 }
116
117 index = _start - listOffset;
118
119 if (index + _count > logEntries.Count) {
120 _count = logEntries.Count - index;
121 }
122
123 _end = _start + _count;
124 }
125
126 return logEntries.GetRange (index, _count);
127 }
128 }
129
130
131 public class LogEntry {
[467]132 public readonly int MessageId;
[434]133 public readonly DateTime Timestamp;
134 public readonly string IsoTime;
135 public readonly string Message;
136 public readonly string Trace;
137 public readonly LogType Type;
138 public readonly long Uptime;
[391]139
[467]140 public LogEntry (int _messageId, DateTime _timestamp, string _message, string _trace, LogType _type, long _uptime) {
141 MessageId = _messageId;
142
[434]143 Timestamp = _timestamp;
144 IsoTime = _timestamp.ToString ("o");
[391]145
[434]146 Message = _message;
147 Trace = _trace;
148 Type = _type;
149 Uptime = _uptime;
[391]150 }
151 }
152 }
153}
Note: See TracBrowser for help on using the repository browser.