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