source: binary-improvements2/WebServer/src/WebAPI/APIs/Log.cs@ 437

Last change on this file since 437 was 434, checked in by alloc, 18 months ago

Added permission management APIs

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