[250] | 1 | using System.Collections.Generic;
|
---|
[325] | 2 | using AllocsFixes.JSON;
|
---|
[382] | 3 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
|
---|
| 4 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
|
---|
[250] | 5 |
|
---|
[325] | 6 | namespace AllocsFixes.NetConnections.Servers.Web.API {
|
---|
[250] | 7 | public class GetLog : WebAPI {
|
---|
[350] | 8 | private const int MAX_COUNT = 1000;
|
---|
| 9 |
|
---|
| 10 | public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
|
---|
| 11 | int _permissionLevel) {
|
---|
[382] | 12 | if (_req.QueryString ["count"] == null || !int.TryParse (_req.QueryString ["count"], out int count)) {
|
---|
[350] | 13 | count = 50;
|
---|
[250] | 14 | }
|
---|
| 15 |
|
---|
[350] | 16 | if (count == 0) {
|
---|
| 17 | count = 1;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | if (count > MAX_COUNT) {
|
---|
| 21 | count = MAX_COUNT;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | if (count < -MAX_COUNT) {
|
---|
| 25 | count = -MAX_COUNT;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[382] | 28 | if (_req.QueryString ["firstLine"] == null || !int.TryParse (_req.QueryString ["firstLine"], out int firstLine)) {
|
---|
| 29 | firstLine = count > 0 ? LogBuffer.Instance.OldestLine : LogBuffer.Instance.LatestLine;
|
---|
[350] | 30 | }
|
---|
| 31 |
|
---|
[250] | 32 | JSONObject result = new JSONObject ();
|
---|
| 33 |
|
---|
[382] | 34 | List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out int lastLine);
|
---|
[250] | 35 |
|
---|
| 36 | JSONArray entries = new JSONArray ();
|
---|
| 37 | foreach (LogBuffer.LogEntry logEntry in logEntries) {
|
---|
| 38 | JSONObject entry = new JSONObject ();
|
---|
| 39 | entry.Add ("date", new JSONString (logEntry.date));
|
---|
| 40 | entry.Add ("time", new JSONString (logEntry.time));
|
---|
[382] | 41 | entry.Add ("isotime", new JSONString (logEntry.isoTime));
|
---|
| 42 | entry.Add ("uptime", new JSONString (logEntry.uptime.ToString ()));
|
---|
[250] | 43 | entry.Add ("msg", new JSONString (logEntry.message));
|
---|
| 44 | entry.Add ("trace", new JSONString (logEntry.trace));
|
---|
[350] | 45 | entry.Add ("type", new JSONString (logEntry.type.ToStringCached ()));
|
---|
[250] | 46 | entries.Add (entry);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | result.Add ("firstLine", new JSONNumber (firstLine));
|
---|
| 50 | result.Add ("lastLine", new JSONNumber (lastLine));
|
---|
| 51 | result.Add ("entries", entries);
|
---|
| 52 |
|
---|
[350] | 53 | WriteJSON (_resp, result);
|
---|
[250] | 54 | }
|
---|
| 55 | }
|
---|
[325] | 56 | }
|
---|