1 | using System.Collections.Generic;
|
---|
2 | using AllocsFixes.JSON;
|
---|
3 | using Webserver;
|
---|
4 | using Webserver.WebAPI;
|
---|
5 |
|
---|
6 | namespace AllocsFixes.WebAPIs {
|
---|
7 | public class GetLog : AbsWebAPI {
|
---|
8 | private const int MAX_COUNT = 1000;
|
---|
9 |
|
---|
10 | public override void HandleRequest (RequestContext _context) {
|
---|
11 | if (_context.Request.QueryString ["count"] == null || !int.TryParse (_context.Request.QueryString ["count"], out var count)) {
|
---|
12 | count = 50;
|
---|
13 | }
|
---|
14 |
|
---|
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 |
|
---|
27 | if (_context.Request.QueryString ["firstLine"] == null || !int.TryParse (_context.Request.QueryString ["firstLine"], out var firstLine)) {
|
---|
28 | if (count > 0) {
|
---|
29 | firstLine = LogBuffer.Instance.OldestLine;
|
---|
30 | } else {
|
---|
31 | firstLine = LogBuffer.Instance.LatestLine;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | JSONObject result = new JSONObject ();
|
---|
36 |
|
---|
37 | List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out var lastLine);
|
---|
38 |
|
---|
39 | JSONArray entries = new JSONArray ();
|
---|
40 | foreach (LogBuffer.LogEntry logEntry in logEntries) {
|
---|
41 | JSONObject entry = new JSONObject ();
|
---|
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 ()));
|
---|
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 |
|
---|
56 | LegacyApiHelper.WriteJSON (_context.Response, result);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|