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
Line 
1using System.Collections.Generic;
2using AllocsFixes.JSON;
3
4namespace AllocsFixes.NetConnections.Servers.Web.API {
5 public class GetLog : AbsWebAPI {
6 private const int MAX_COUNT = 1000;
7
8 public override void HandleRequest (RequestContext _context) {
9 if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out int count)) {
10 count = 50;
11 }
12
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
25 if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out int firstLine)) {
26 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
27 }
28
29 JSONObject result = new JSONObject ();
30
31 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine);
32
33 JSONArray entries = new JSONArray ();
34 foreach (LogBuffer.LogEntry logEntry in logEntries) {
35 JSONObject entry = new JSONObject ();
36 entry.Add ("isotime", new JSONString (logEntry.isoTime));
37 entry.Add ("uptime", new JSONString (logEntry.uptime.ToString ()));
38 entry.Add ("msg", new JSONString (logEntry.message));
39 entry.Add ("trace", new JSONString (logEntry.trace));
40 entry.Add ("type", new JSONString (logEntry.type.ToStringCached ()));
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
48 WebUtils.WriteJson (_context.Response, result);
49 }
50 }
51}
Note: See TracBrowser for help on using the repository browser.