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