Changeset 402 for binary-improvements2/WebServer/src/WebAPI/APIs/Log.cs
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- Location:
- binary-improvements2/WebServer/src/WebAPI/APIs
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/WebAPI/APIs/Log.cs
r401 r402 1 1 using System.Collections.Generic; 2 using AllocsFixes.JSON;3 2 using JetBrains.Annotations; 3 using Utf8Json; 4 4 5 namespace Webserver.WebAPI {5 namespace Webserver.WebAPI.APIs { 6 6 [UsedImplicitly] 7 public class GetLog : AbsWebAPI { 8 private const int MAX_COUNT = 1000; 7 public class Log : AbsRestApi { 8 private const int maxCount = 1000; 9 10 private static readonly byte[] jsonKeyEntries = JsonWriter.GetEncodedPropertyNameWithBeginObject ("entries"); 11 private static readonly byte[] jsonKeyFirstLine = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("firstLine"); 12 private static readonly byte[] jsonKeyLastLine = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lastLine"); 13 14 private static readonly byte[] jsonMsgKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("msg"); 15 private static readonly byte[] jsonTypeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("type"); 16 private static readonly byte[] jsonTraceKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("trace"); 17 private static readonly byte[] jsonIsotimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("isotime"); 18 private static readonly byte[] jsonUptimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("uptime"); 9 19 10 p ublic override void HandleRequest (RequestContext _context) {20 protected override void HandleRestGet (RequestContext _context) { 11 21 if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out int count)) { 12 22 count = 50; … … 17 27 } 18 28 19 if (count > MAX_COUNT) {20 count = MAX_COUNT;29 if (count > maxCount) { 30 count = maxCount; 21 31 } 22 32 23 if (count < - MAX_COUNT) {24 count = - MAX_COUNT;33 if (count < -maxCount) { 34 count = -maxCount; 25 35 } 26 36 … … 28 38 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine; 29 39 } 30 31 JsonObject result = new JsonObject (); 32 40 41 PrepareEnvelopedResult (out JsonWriter writer); 42 43 writer.WriteRaw (jsonKeyEntries); 44 33 45 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine); 34 46 35 JsonArray entries = new JsonArray (); 47 writer.WriteBeginArray (); 48 49 bool first = true; 36 50 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); 51 if (!first) { 52 writer.WriteValueSeparator (); 53 } 54 55 first = false; 56 57 writer.WriteRaw (jsonMsgKey); 58 writer.WriteString (logEntry.message); 59 60 writer.WriteRaw (jsonTypeKey); 61 writer.WriteString (logEntry.type.ToStringCached ()); 62 63 writer.WriteRaw (jsonTraceKey); 64 writer.WriteString (logEntry.trace); 65 66 writer.WriteRaw (jsonIsotimeKey); 67 writer.WriteString (logEntry.isoTime); 68 69 writer.WriteRaw (jsonUptimeKey); 70 writer.WriteString (logEntry.uptime.ToString ()); 71 72 writer.WriteEndObject (); 44 73 } 74 writer.WriteEndArray (); 45 75 46 result.Add ("firstLine", new JsonNumber (firstLine)); 47 result.Add ("lastLine", new JsonNumber (lastLine)); 48 result.Add ("entries", entries); 76 writer.WriteRaw (jsonKeyFirstLine); 77 writer.WriteInt32 (firstLine); 78 79 writer.WriteRaw (jsonKeyLastLine); 80 writer.WriteInt32 (lastLine); 81 82 writer.WriteEndObject (); 49 83 50 WebUtils.WriteJson (_context.Response, result);84 SendEnvelopedResult (_context, ref writer); 51 85 } 52 86 }
Note:
See TracChangeset
for help on using the changeset viewer.