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

Last change on this file since 474 was 471, checked in by alloc, 15 months ago

Fixed: Possibly multiple parsing of query string

File size: 3.2 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[] jsonIdKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id");
15 private static readonly byte[] jsonMsgKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("msg");
16 private static readonly byte[] jsonTypeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("type");
17 private static readonly byte[] jsonTraceKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("trace");
18 private static readonly byte[] jsonIsotimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("isotime");
19 private static readonly byte[] jsonUptimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("uptime");
20
21 public LogApi () : base ("Log") {
22 }
23
24 protected override void HandleRestGet (RequestContext _context) {
25 if (_context.QueryParameters ["count"] == null || !int.TryParse (_context.QueryParameters ["count"], out int count)) {
26 count = 50;
27 }
28
29 if (count == 0) {
30 count = 1;
31 }
32
33 if (count > maxCount) {
34 count = maxCount;
35 }
36
37 if (count < -maxCount) {
38 count = -maxCount;
39 }
40
41 if (_context.QueryParameters ["firstLine"] == null || !int.TryParse (_context.QueryParameters ["firstLine"], out int firstLine)) {
42 firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
43 }
44
45 PrepareEnvelopedResult (out JsonWriter writer);
46
47 writer.WriteRaw (jsonKeyEntries);
48
49 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine);
50
51 writer.WriteBeginArray ();
52
53 for (int i = 0; i < logEntries.Count; i++) {
54 LogBuffer.LogEntry logEntry = logEntries [i];
55
56 if (i > 0) {
57 writer.WriteValueSeparator ();
58 }
59
60 WriteLogMessageObject (ref writer, logEntry);
61 }
62
63 writer.WriteEndArray ();
64
65 writer.WriteRaw (jsonKeyFirstLine);
66 writer.WriteInt32 (firstLine);
67
68 writer.WriteRaw (jsonKeyLastLine);
69 writer.WriteInt32 (lastLine);
70
71 writer.WriteEndObject ();
72
73 SendEnvelopedResult (_context, ref writer);
74 }
75
76 public static void WriteLogMessageObject (ref JsonWriter _writer, LogBuffer.LogEntry _logEntry) {
77 _writer.WriteRaw (jsonIdKey);
78 _writer.WriteInt32 (_logEntry.MessageId);
79
80 _writer.WriteRaw (jsonMsgKey);
81 _writer.WriteString (_logEntry.Message);
82
83 _writer.WriteRaw (jsonTypeKey);
84 _writer.WriteString (_logEntry.Type.ToStringCached ());
85
86 _writer.WriteRaw (jsonTraceKey);
87 _writer.WriteString (_logEntry.Trace);
88
89 _writer.WriteRaw (jsonIsotimeKey);
90 _writer.WriteString (_logEntry.IsoTime);
91
92 _writer.WriteRaw (jsonUptimeKey);
93 _writer.WriteString (_logEntry.Uptime.ToString ());
94
95 _writer.WriteEndObject ();
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.