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

Last change on this file since 423 was 369, checked in by alloc, 3 years ago

Preparations for A20 release
Changes usage of "SteamID" to "UserID" in console commands
Also changes a bunch of the WebAPI stuff to show / use UserIDs

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