source: binary-improvements2/MapRendering/Web/API/GetLog.cs@ 384

Last change on this file since 384 was 383, checked in by alloc, 2 years ago

Fixed a bunch of warnings

File size: 1.9 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.JSON;
3using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
4using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
5
6namespace AllocsFixes.NetConnections.Servers.Web.API {
7 public class GetLog : WebAPI {
8 private const int MAX_COUNT = 1000;
9
10 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
11 int _permissionLevel) {
12 if (_req.QueryString ["count"] == null || !int.TryParse (_req.QueryString ["count"], out int count)) {
13 count = 50;
14 }
15
16 if (count == 0) {
17 count = 1;
18 }
19
20 if (count > MAX_COUNT) {
21 count = MAX_COUNT;
22 }
23
24 if (count < -MAX_COUNT) {
25 count = -MAX_COUNT;
26 }
27
28 if (_req.QueryString ["firstLine"] == null || !int.TryParse (_req.QueryString ["firstLine"], out int firstLine)) {
29 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
30 }
31
32 JSONObject result = new JSONObject ();
33
34 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine);
35
36 JSONArray entries = new JSONArray ();
37 foreach (LogBuffer.LogEntry logEntry in logEntries) {
38 JSONObject entry = new JSONObject ();
39 entry.Add ("date", new JSONString (logEntry.date));
40 entry.Add ("time", new JSONString (logEntry.time));
41 entry.Add ("isotime", new JSONString (logEntry.isoTime));
42 entry.Add ("uptime", new JSONString (logEntry.uptime.ToString ()));
43 entry.Add ("msg", new JSONString (logEntry.message));
44 entry.Add ("trace", new JSONString (logEntry.trace));
45 entry.Add ("type", new JSONString (logEntry.type.ToStringCached ()));
46 entries.Add (entry);
47 }
48
49 result.Add ("firstLine", new JSONNumber (firstLine));
50 result.Add ("lastLine", new JSONNumber (lastLine));
51 result.Add ("entries", entries);
52
53 WriteJSON (_resp, result);
54 }
55 }
56}
Note: See TracBrowser for help on using the repository browser.