1 | using System.Collections.Generic;
|
---|
2 | using JetBrains.Annotations;
|
---|
3 | using Utf8Json;
|
---|
4 |
|
---|
5 | namespace Webserver.WebAPI.APIs {
|
---|
6 | [UsedImplicitly]
|
---|
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");
|
---|
19 |
|
---|
20 | protected override void HandleRestGet (RequestContext _context) {
|
---|
21 | if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out int count)) {
|
---|
22 | count = 50;
|
---|
23 | }
|
---|
24 |
|
---|
25 | if (count == 0) {
|
---|
26 | count = 1;
|
---|
27 | }
|
---|
28 |
|
---|
29 | if (count > maxCount) {
|
---|
30 | count = maxCount;
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (count < -maxCount) {
|
---|
34 | count = -maxCount;
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out int firstLine)) {
|
---|
38 | firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
|
---|
39 | }
|
---|
40 |
|
---|
41 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
42 |
|
---|
43 | writer.WriteRaw (jsonKeyEntries);
|
---|
44 |
|
---|
45 | List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine);
|
---|
46 |
|
---|
47 | writer.WriteBeginArray ();
|
---|
48 |
|
---|
49 | bool first = true;
|
---|
50 | foreach (LogBuffer.LogEntry logEntry in logEntries) {
|
---|
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 ();
|
---|
73 | }
|
---|
74 | writer.WriteEndArray ();
|
---|
75 |
|
---|
76 | writer.WriteRaw (jsonKeyFirstLine);
|
---|
77 | writer.WriteInt32 (firstLine);
|
---|
78 |
|
---|
79 | writer.WriteRaw (jsonKeyLastLine);
|
---|
80 | writer.WriteInt32 (lastLine);
|
---|
81 |
|
---|
82 | writer.WriteEndObject ();
|
---|
83 |
|
---|
84 | SendEnvelopedResult (_context, ref writer);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|