source: TFP-WebServer/WebServer/src/WebAPI/APIs/LogApi.cs@ 465

Last change on this file since 465 was 459, checked in by alloc, 15 months ago

Updated to dashboard/marker files 0.8.0
Added initial OpenAPI support

File size: 2.9 KB
RevLine 
[391]1using System.Collections.Generic;
2using JetBrains.Annotations;
[402]3using Utf8Json;
[391]4
[402]5namespace Webserver.WebAPI.APIs {
[391]6 [UsedImplicitly]
[459]7 public class LogApi : AbsRestApi {
[402]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");
[459]19
20 public LogApi () : base ("Log") {
21 }
22
[402]23 protected override void HandleRestGet (RequestContext _context) {
[391]24 if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out int count)) {
25 count = 50;
26 }
27
28 if (count == 0) {
29 count = 1;
30 }
31
[402]32 if (count > maxCount) {
33 count = maxCount;
[391]34 }
35
[402]36 if (count < -maxCount) {
37 count = -maxCount;
[391]38 }
39
40 if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out int firstLine)) {
41 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
42 }
[402]43
44 PrepareEnvelopedResult (out JsonWriter writer);
45
46 writer.WriteRaw (jsonKeyEntries);
47
48 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine);
[391]49
[402]50 writer.WriteBeginArray ();
[391]51
[408]52 for (int i = 0; i < logEntries.Count; i++) {
53 LogBuffer.LogEntry logEntry = logEntries [i];
54
55 if (i > 0) {
[402]56 writer.WriteValueSeparator ();
57 }
[391]58
[402]59 writer.WriteRaw (jsonMsgKey);
[434]60 writer.WriteString (logEntry.Message);
[408]61
[402]62 writer.WriteRaw (jsonTypeKey);
[434]63 writer.WriteString (logEntry.Type.ToStringCached ());
[408]64
[402]65 writer.WriteRaw (jsonTraceKey);
[434]66 writer.WriteString (logEntry.Trace);
[408]67
[402]68 writer.WriteRaw (jsonIsotimeKey);
[434]69 writer.WriteString (logEntry.IsoTime);
[408]70
[402]71 writer.WriteRaw (jsonUptimeKey);
[434]72 writer.WriteString (logEntry.Uptime.ToString ());
[408]73
[402]74 writer.WriteEndObject ();
[391]75 }
[408]76
[402]77 writer.WriteEndArray ();
[391]78
[402]79 writer.WriteRaw (jsonKeyFirstLine);
80 writer.WriteInt32 (firstLine);
81
82 writer.WriteRaw (jsonKeyLastLine);
83 writer.WriteInt32 (lastLine);
84
85 writer.WriteEndObject ();
[391]86
[402]87 SendEnvelopedResult (_context, ref writer);
[391]88 }
89 }
90}
Note: See TracBrowser for help on using the repository browser.