[250] | 1 | using System.Collections.Generic;
|
---|
[325] | 2 | using AllocsFixes.JSON;
|
---|
[454] | 3 | using Webserver;
|
---|
| 4 | using Webserver.WebAPI;
|
---|
[250] | 5 |
|
---|
[454] | 6 | namespace AllocsFixes.WebAPIs {
|
---|
| 7 | public class GetLog : AbsWebAPI {
|
---|
[350] | 8 | private const int MAX_COUNT = 1000;
|
---|
| 9 |
|
---|
[454] | 10 | public override void HandleRequest (RequestContext _context) {
|
---|
| 11 | if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out var count)) {
|
---|
[350] | 12 | count = 50;
|
---|
[250] | 13 | }
|
---|
| 14 |
|
---|
[350] | 15 | if (count == 0) {
|
---|
| 16 | count = 1;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | if (count > MAX_COUNT) {
|
---|
| 20 | count = MAX_COUNT;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | if (count < -MAX_COUNT) {
|
---|
| 24 | count = -MAX_COUNT;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
[454] | 27 | if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out var firstLine)) {
|
---|
[350] | 28 | if (count > 0) {
|
---|
| 29 | firstLine = LogBuffer.Instance.OldestLine;
|
---|
| 30 | } else {
|
---|
| 31 | firstLine = LogBuffer.Instance.LatestLine;
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[250] | 35 | JSONObject result = new JSONObject ();
|
---|
| 36 |
|
---|
[454] | 37 | List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out var lastLine);
|
---|
[250] | 38 |
|
---|
| 39 | JSONArray entries = new JSONArray ();
|
---|
| 40 | foreach (LogBuffer.LogEntry logEntry in logEntries) {
|
---|
| 41 | JSONObject entry = new JSONObject ();
|
---|
[454] | 42 | var logEntryTimestamp = logEntry.Timestamp;
|
---|
| 43 | entry.Add ("date", new JSONString ($"{logEntryTimestamp.Year:0000}-{logEntryTimestamp.Month:00}-{logEntryTimestamp.Day:00}"));
|
---|
| 44 | entry.Add ("time", new JSONString ($"{logEntryTimestamp.Hour:00}:{logEntryTimestamp.Minute:00}:{logEntryTimestamp.Second:00}"));
|
---|
| 45 | entry.Add ("uptime", new JSONString (logEntry.Uptime.ToString()));
|
---|
| 46 | entry.Add ("msg", new JSONString (logEntry.Message));
|
---|
| 47 | entry.Add ("trace", new JSONString (logEntry.Trace));
|
---|
| 48 | entry.Add ("type", new JSONString (logEntry.Type.ToStringCached ()));
|
---|
[250] | 49 | entries.Add (entry);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | result.Add ("firstLine", new JSONNumber (firstLine));
|
---|
| 53 | result.Add ("lastLine", new JSONNumber (lastLine));
|
---|
| 54 | result.Add ("entries", entries);
|
---|
| 55 |
|
---|
[454] | 56 | LegacyApiHelper.WriteJSON (_context.Response, result);
|
---|
[250] | 57 | }
|
---|
| 58 | }
|
---|
[325] | 59 | }
|
---|