Changeset 455 for binary-improvements/MapRendering/API/GetLog.cs
- Timestamp:
- Jul 31, 2023, 4:06:13 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/API/GetLog.cs
r454 r455 1 1 using System.Collections.Generic; 2 using AllocsFixes.JSON; 2 using JetBrains.Annotations; 3 using Utf8Json; 3 4 using Webserver; 4 5 using Webserver.WebAPI; 5 6 6 7 namespace AllocsFixes.WebAPIs { 8 [UsedImplicitly] 7 9 public class GetLog : AbsWebAPI { 8 10 private const int MAX_COUNT = 1000; 9 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 10 23 public override void HandleRequest (RequestContext _context) { 11 24 if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out var count)) { … … 26 39 27 40 if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out var firstLine)) { 28 if (count > 0) { 29 firstLine = LogBuffer.Instance.OldestLine; 30 } else { 31 firstLine = LogBuffer.Instance.LatestLine; 32 } 41 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine; 33 42 } 34 35 JSONObject result = new JSONObject ();36 43 37 44 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out var lastLine); 38 45 39 JSONArray entries = new JSONArray (); 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 40 59 foreach (LogBuffer.LogEntry logEntry in logEntries) { 41 JSONObject entry = new JSONObject (); 60 if (!first) { 61 writer.WriteValueSeparator (); 62 } 63 64 first = false; 65 42 66 var logEntryTimestamp = logEntry.Timestamp; 43 entry.Add ("date", new JSONString ($"{logEntryTimestamp.Year:0000}-{logEntryTimestamp.Month:00}-{logEntryTimestamp.Day:00}")); 44 entry.Add ("time", new JSONString ($"{logEntryTimestamp.Hour:00}:{logEntryTimestamp.Minute:00}:{logEntryTimestamp.Second:00}")); 45 entry.Add ("uptime", new JSONString (logEntry.Uptime.ToString())); 46 entry.Add ("msg", new JSONString (logEntry.Message)); 47 entry.Add ("trace", new JSONString (logEntry.Trace)); 48 entry.Add ("type", new JSONString (logEntry.Type.ToStringCached ())); 49 entries.Add (entry); 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 (); 50 87 } 51 88 52 result.Add ("firstLine", new JSONNumber (firstLine)); 53 result.Add ("lastLine", new JSONNumber (lastLine)); 54 result.Add ("entries", entries); 55 56 LegacyApiHelper.WriteJSON (_context.Response, result); 89 writer.WriteEndArray (); 90 91 writer.WriteEndObject (); 92 WebUtils.WriteJsonData (_context.Response, ref writer); 57 93 } 58 94 }
Note:
See TracChangeset
for help on using the changeset viewer.