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

Last change on this file since 325 was 325, checked in by alloc, 6 years ago

Code style cleanup (mostly whitespace changes, enforcing braces, using cleanup)

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