source: binary-improvements/MapRendering/API/GetLog.cs@ 454

Last change on this file since 454 was 454, checked in by alloc, 16 months ago

24_29_43
Switched over to vanilla Web infrastructure

File size: 1.9 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.JSON;
3using Webserver;
4using Webserver.WebAPI;
5
6namespace AllocsFixes.WebAPIs {
7 public class GetLog : AbsWebAPI {
8 private const int MAX_COUNT = 1000;
9
10 public override void HandleRequest (RequestContext _context) {
11 if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out var count)) {
12 count = 50;
13 }
14
15 if (count == 0) {
16 count = 1;
17 }
18
19 if (count > MAX_COUNT) {
20 count = MAX_COUNT;
21 }
22
23 if (count < -MAX_COUNT) {
24 count = -MAX_COUNT;
25 }
26
27 if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out var firstLine)) {
28 if (count > 0) {
29 firstLine = LogBuffer.Instance.OldestLine;
30 } else {
31 firstLine = LogBuffer.Instance.LatestLine;
32 }
33 }
34
35 JSONObject result = new JSONObject ();
36
37 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out var lastLine);
38
39 JSONArray entries = new JSONArray ();
40 foreach (LogBuffer.LogEntry logEntry in logEntries) {
41 JSONObject entry = new JSONObject ();
42 var logEntryTimestamp = logEntry.Timestamp;
43 entry.Add ("date", new JSONString ($"{logEntryTimestamp.Year:0000}-{logEntryTimestamp.Month:00}-{logEntryTimestamp.Day:00}"));
44 entry.Add ("time", new JSONString ($"{logEntryTimestamp.Hour:00}:{logEntryTimestamp.Minute:00}:{logEntryTimestamp.Second:00}"));
45 entry.Add ("uptime", new JSONString (logEntry.Uptime.ToString()));
46 entry.Add ("msg", new JSONString (logEntry.Message));
47 entry.Add ("trace", new JSONString (logEntry.Trace));
48 entry.Add ("type", new JSONString (logEntry.Type.ToStringCached ()));
49 entries.Add (entry);
50 }
51
52 result.Add ("firstLine", new JSONNumber (firstLine));
53 result.Add ("lastLine", new JSONNumber (lastLine));
54 result.Add ("entries", entries);
55
56 LegacyApiHelper.WriteJSON (_context.Response, result);
57 }
58 }
59}
Note: See TracBrowser for help on using the repository browser.