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

Last change on this file since 321 was 270, checked in by alloc, 9 years ago

Fixed Web log

File size: 2.8 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 = 3000;
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 int index = _start - listOffset;
111
112 if (index + _count > logEntries.Count) {
113 _count = logEntries.Count - index;
114 }
115
116 _end = _start + _count;
117
118 return logEntries.GetRange (index, _count);
119 }
120 }
121
122
123 public class LogEntry {
124 public string date;
125 public string time;
126 public string uptime;
127 public string message;
128 public string trace;
129 public LogType type;
130 }
131 }
132}
133
Note: See TracBrowser for help on using the repository browser.