source: binary-improvements2/WebServer/src/WebAPI/GetLog.cs@ 391

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

Major refactoring/cleanup

File size: 1.6 KB
RevLine 
[391]1using System.Collections.Generic;
2using AllocsFixes.JSON;
3using JetBrains.Annotations;
4
5namespace Webserver.WebAPI {
6 [UsedImplicitly]
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 int 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 int firstLine)) {
28 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
29 }
30
31 JsonObject result = new JsonObject ();
32
33 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine);
34
35 JsonArray entries = new JsonArray ();
36 foreach (LogBuffer.LogEntry logEntry in logEntries) {
37 JsonObject entry = new JsonObject ();
38 entry.Add ("isotime", new JsonString (logEntry.isoTime));
39 entry.Add ("uptime", new JsonString (logEntry.uptime.ToString ()));
40 entry.Add ("msg", new JsonString (logEntry.message));
41 entry.Add ("trace", new JsonString (logEntry.trace));
42 entry.Add ("type", new JsonString (logEntry.type.ToStringCached ()));
43 entries.Add (entry);
44 }
45
46 result.Add ("firstLine", new JsonNumber (firstLine));
47 result.Add ("lastLine", new JsonNumber (lastLine));
48 result.Add ("entries", entries);
49
50 WebUtils.WriteJson (_context.Response, result);
51 }
52 }
53}
Note: See TracBrowser for help on using the repository browser.