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

Last change on this file since 466 was 459, checked in by alloc, 16 months ago

Updated to dashboard/marker files 0.8.0
Added initial OpenAPI support

File size: 2.9 KB
Line 
1using System.Collections.Generic;
2using JetBrains.Annotations;
3using Utf8Json;
4
5namespace Webserver.WebAPI.APIs {
6 [UsedImplicitly]
7 public class LogApi : 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");
19
20 public LogApi () : base ("Log") {
21 }
22
23 protected override void HandleRestGet (RequestContext _context) {
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
32 if (count > maxCount) {
33 count = maxCount;
34 }
35
36 if (count < -maxCount) {
37 count = -maxCount;
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 }
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);
49
50 writer.WriteBeginArray ();
51
52 for (int i = 0; i < logEntries.Count; i++) {
53 LogBuffer.LogEntry logEntry = logEntries [i];
54
55 if (i > 0) {
56 writer.WriteValueSeparator ();
57 }
58
59 writer.WriteRaw (jsonMsgKey);
60 writer.WriteString (logEntry.Message);
61
62 writer.WriteRaw (jsonTypeKey);
63 writer.WriteString (logEntry.Type.ToStringCached ());
64
65 writer.WriteRaw (jsonTraceKey);
66 writer.WriteString (logEntry.Trace);
67
68 writer.WriteRaw (jsonIsotimeKey);
69 writer.WriteString (logEntry.IsoTime);
70
71 writer.WriteRaw (jsonUptimeKey);
72 writer.WriteString (logEntry.Uptime.ToString ());
73
74 writer.WriteEndObject ();
75 }
76
77 writer.WriteEndArray ();
78
79 writer.WriteRaw (jsonKeyFirstLine);
80 writer.WriteInt32 (firstLine);
81
82 writer.WriteRaw (jsonKeyLastLine);
83 writer.WriteInt32 (lastLine);
84
85 writer.WriteEndObject ();
86
87 SendEnvelopedResult (_context, ref writer);
88 }
89 }
90}
Note: See TracBrowser for help on using the repository browser.