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

Last change on this file since 255 was 253, checked in by alloc, 9 years ago

Fixes 6_8_10

File size: 2.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text.RegularExpressions;
4
5using UnityEngine;
6
7namespace AllocsFixes.NetConnections.Servers.Web
8{
9 public class LogBuffer {
10 private const int MAX_ENTRIES = 30;
11 private static LogBuffer instance;
12
13 public static LogBuffer Instance {
14 get {
15 if (instance == null) {
16 instance = new LogBuffer ();
17 }
18 return instance;
19 }
20 }
21
22 private static Regex logMessageMatcher = new Regex (@"^([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2}) ([0-9]+[,.][0-9]+) [A-Z]+ (.*)$");
23 private List<LogEntry> logEntries = new List<LogEntry> ();
24 private int listOffset = 0;
25
26 public int OldestLine {
27 get {
28 lock (logEntries) {
29 return listOffset;
30 }
31 }
32 }
33
34 public int LatestLine {
35 get {
36 lock (logEntries) {
37 return listOffset + logEntries.Count - 1;
38 }
39 }
40 }
41
42 public int StoredLines {
43 get {
44 lock (logEntries) {
45 return logEntries.Count;
46 }
47 }
48 }
49
50 public LogEntry this [int index] {
51 get {
52 lock (logEntries) {
53 if (index >= listOffset && index < listOffset + logEntries.Count) {
54 return logEntries [index];
55 }
56 }
57 return null;
58 }
59 }
60
61 private LogBuffer () {
62 Logger.Main.LogCallbacks += LogCallback;
63 }
64
65 private void LogCallback (string _msg, string _trace, LogType _type) {
66 LogEntry le = new LogEntry ();
67
68 Match match = logMessageMatcher.Match (_msg);
69 if (match.Success) {
70 le.date = match.Groups [1].Value;
71 le.time = match.Groups [2].Value;
72 le.uptime = match.Groups [3].Value;
73 le.message = match.Groups [4].Value;
74 } else {
75 DateTime dt = DateTime.Now;
76 le.date = string.Format ("{0:0000}-{1:00}-{2:00}", dt.Year, dt.Month, dt.Day);
77 le.time = string.Format ("{0:00}:{1:00}:{2:00}", dt.Hour, dt.Minute, dt.Second);
78 le.uptime = "";
79 le.message = _msg;
80 }
81
82 le.trace = _trace;
83 le.type = _type;
84
85 lock (logEntries) {
86 logEntries.Add (le);
87 if (logEntries.Count > MAX_ENTRIES) {
88 listOffset += logEntries.Count - MAX_ENTRIES;
89 logEntries.RemoveRange (0, logEntries.Count - MAX_ENTRIES);
90 }
91 }
92 }
93
94 public List<LogEntry> GetRange (ref int _start, int _count, out int _end) {
95 lock (logEntries) {
96 if (_count < 1) {
97 _end = _start;
98 return new List<LogEntry> ();
99 }
100
101 if (_start < listOffset) {
102 _start = listOffset;
103 }
104
105 if (_start >= listOffset + logEntries.Count) {
106 _end = _start;
107 return new List<LogEntry> ();
108 }
109
110 _end = _start + _count;
111 return logEntries.GetRange (_start - listOffset, _count);
112 }
113 }
114
115
116 public class LogEntry {
117 public string date;
118 public string time;
119 public string uptime;
120 public string message;
121 public string trace;
122 public LogType type;
123 }
124 }
125}
126
Note: See TracBrowser for help on using the repository browser.