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

Last change on this file since 455 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
RevLine 
[250]1using System.Collections.Generic;
[455]2using JetBrains.Annotations;
3using Utf8Json;
[454]4using Webserver;
5using Webserver.WebAPI;
[250]6
[454]7namespace AllocsFixes.WebAPIs {
[455]8 [UsedImplicitly]
[454]9 public class GetLog : AbsWebAPI {
[350]10 private const int MAX_COUNT = 1000;
11
[455]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
[454]23 public override void HandleRequest (RequestContext _context) {
24 if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out var count)) {
[350]25 count = 50;
[250]26 }
27
[350]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
[454]40 if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out var firstLine)) {
[455]41 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
[350]42 }
43
[454]44 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out var lastLine);
[250]45
[455]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
[250]59 foreach (LogBuffer.LogEntry logEntry in logEntries) {
[455]60 if (!first) {
61 writer.WriteValueSeparator ();
62 }
63
64 first = false;
65
[454]66 var logEntryTimestamp = logEntry.Timestamp;
[455]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 ();
[250]87 }
88
[455]89 writer.WriteEndArray ();
90
91 writer.WriteEndObject ();
92 WebUtils.WriteJsonData (_context.Response, ref writer);
[250]93 }
94 }
[325]95}
Note: See TracBrowser for help on using the repository browser.