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

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

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 3.3 KB
Line 
1using System.Collections.Generic;
2using JetBrains.Annotations;
3using Utf8Json;
4using Webserver;
5using Webserver.WebAPI;
6
7namespace AllocsFixes.WebAPIs {
8 [UsedImplicitly]
9 public class GetLog : AbsWebAPI {
10 private const int MAX_COUNT = 1000;
11
12 private static readonly byte[] jsonKeyFirstLine = JsonWriter.GetEncodedPropertyNameWithBeginObject ("firstLine");
13 private static readonly byte[] jsonKeyLastLine = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lastLine");
14 private static readonly byte[] jsonKeyEntries = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("entries");
15
16 private static readonly byte[] jsonKeyEntDate = JsonWriter.GetEncodedPropertyNameWithBeginObject ("date");
17 private static readonly byte[] jsonKeyEntTime = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("time");
18 private static readonly byte[] jsonKeyEntUptime = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("uptime");
19 private static readonly byte[] jsonKeyEntMsg = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("msg");
20 private static readonly byte[] jsonKeyEntTrace = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("trace");
21 private static readonly byte[] jsonKeyEntType = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("type");
22
23 public override void HandleRequest (RequestContext _context) {
24 if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out var count)) {
25 count = 50;
26 }
27
28 if (count == 0) {
29 count = 1;
30 }
31
32 if (count > MAX_COUNT) {
33 count = MAX_COUNT;
34 }
35
36 if (count < -MAX_COUNT) {
37 count = -MAX_COUNT;
38 }
39
40 if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out var firstLine)) {
41 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
42 }
43
44 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out var lastLine);
45
46 JsonWriter writer = new JsonWriter ();
47
48 writer.WriteRaw (jsonKeyFirstLine);
49 writer.WriteInt32 (firstLine);
50
51 writer.WriteRaw (jsonKeyLastLine);
52 writer.WriteInt32 (lastLine);
53
54 writer.WriteRaw (jsonKeyEntries);
55 writer.WriteBeginArray ();
56
57 bool first = true;
58
59 foreach (LogBuffer.LogEntry logEntry in logEntries) {
60 if (!first) {
61 writer.WriteValueSeparator ();
62 }
63
64 first = false;
65
66 var logEntryTimestamp = logEntry.Timestamp;
67
68 writer.WriteRaw (jsonKeyEntDate);
69 writer.WriteString ($"{logEntryTimestamp.Year:0000}-{logEntryTimestamp.Month:00}-{logEntryTimestamp.Day:00}");
70
71 writer.WriteRaw (jsonKeyEntTime);
72 writer.WriteString ($"{logEntryTimestamp.Hour:00}:{logEntryTimestamp.Minute:00}:{logEntryTimestamp.Second:00}");
73
74 writer.WriteRaw (jsonKeyEntUptime);
75 writer.WriteString (logEntry.Uptime.ToString());
76
77 writer.WriteRaw (jsonKeyEntMsg);
78 writer.WriteString (logEntry.Message);
79
80 writer.WriteRaw (jsonKeyEntTrace);
81 writer.WriteString (logEntry.Trace);
82
83 writer.WriteRaw (jsonKeyEntType);
84 writer.WriteString (logEntry.Type.ToStringCached ());
85
86 writer.WriteEndObject ();
87 }
88
89 writer.WriteEndArray ();
90
91 writer.WriteEndObject ();
92 WebUtils.WriteJsonData (_context.Response, ref writer);
93 }
94 }
95}
Note: See TracBrowser for help on using the repository browser.