using System.Collections.Generic; using JetBrains.Annotations; using Utf8Json; namespace Webserver.WebAPI.APIs { [UsedImplicitly] public class Log : AbsRestApi { private const int maxCount = 1000; private static readonly byte[] jsonKeyEntries = JsonWriter.GetEncodedPropertyNameWithBeginObject ("entries"); private static readonly byte[] jsonKeyFirstLine = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("firstLine"); private static readonly byte[] jsonKeyLastLine = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lastLine"); private static readonly byte[] jsonMsgKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("msg"); private static readonly byte[] jsonTypeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("type"); private static readonly byte[] jsonTraceKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("trace"); private static readonly byte[] jsonIsotimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("isotime"); private static readonly byte[] jsonUptimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("uptime"); protected override void HandleRestGet (RequestContext _context) { if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out int count)) { count = 50; } if (count == 0) { count = 1; } if (count > maxCount) { count = maxCount; } if (count < -maxCount) { count = -maxCount; } if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out int firstLine)) { firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine; } PrepareEnvelopedResult (out JsonWriter writer); writer.WriteRaw (jsonKeyEntries); List logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine); writer.WriteBeginArray (); bool first = true; foreach (LogBuffer.LogEntry logEntry in logEntries) { if (!first) { writer.WriteValueSeparator (); } first = false; writer.WriteRaw (jsonMsgKey); writer.WriteString (logEntry.message); writer.WriteRaw (jsonTypeKey); writer.WriteString (logEntry.type.ToStringCached ()); writer.WriteRaw (jsonTraceKey); writer.WriteString (logEntry.trace); writer.WriteRaw (jsonIsotimeKey); writer.WriteString (logEntry.isoTime); writer.WriteRaw (jsonUptimeKey); writer.WriteString (logEntry.uptime.ToString ()); writer.WriteEndObject (); } writer.WriteEndArray (); writer.WriteRaw (jsonKeyFirstLine); writer.WriteInt32 (firstLine); writer.WriteRaw (jsonKeyLastLine); writer.WriteInt32 (lastLine); writer.WriteEndObject (); SendEnvelopedResult (_context, ref writer); } } }