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
RevLine 
[250]1using System.Collections.Generic;
[325]2using AllocsFixes.JSON;
[454]3using Webserver;
4using Webserver.WebAPI;
[250]5
[454]6namespace AllocsFixes.WebAPIs {
7 public class GetLog : AbsWebAPI {
[350]8 private const int MAX_COUNT = 1000;
9
[454]10 public override void HandleRequest (RequestContext _context) {
11 if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out var count)) {
[350]12 count = 50;
[250]13 }
14
[350]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
[454]27 if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out var firstLine)) {
[350]28 if (count > 0) {
29 firstLine = LogBuffer.Instance.OldestLine;
30 } else {
31 firstLine = LogBuffer.Instance.LatestLine;
32 }
33 }
34
[250]35 JSONObject result = new JSONObject ();
36
[454]37 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out var lastLine);
[250]38
39 JSONArray entries = new JSONArray ();
40 foreach (LogBuffer.LogEntry logEntry in logEntries) {
41 JSONObject entry = new JSONObject ();
[454]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 ()));
[250]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
[454]56 LegacyApiHelper.WriteJSON (_context.Response, result);
[250]57 }
58 }
[325]59}
Note: See TracBrowser for help on using the repository browser.