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

Last change on this file since 388 was 387, checked in by alloc, 2 years ago

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

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