source: binary-improvements/MapRendering/Web/API/GetLog.cs@ 426

Last change on this file since 426 was 350, checked in by alloc, 6 years ago

Fixed #154

File size: 1.7 KB
RevLine 
[250]1using System.Collections.Generic;
2using System.Net;
[325]3using AllocsFixes.JSON;
[250]4
[325]5namespace AllocsFixes.NetConnections.Servers.Web.API {
[250]6 public class GetLog : WebAPI {
[350]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;
[250]12
[350]13 if (_req.QueryString ["count"] == null || !int.TryParse (_req.QueryString ["count"], out count)) {
14 count = 50;
[250]15 }
16
[350]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
[250]37 JSONObject result = new JSONObject ();
38
[350]39 List<LogBuffer.LogEntry> logEntries = LogBuffer.Instance.GetRange (ref firstLine, count, out lastLine);
[250]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));
[350]49 entry.Add ("type", new JSONString (logEntry.type.ToStringCached ()));
[250]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
[350]57 WriteJSON (_resp, result);
[250]58 }
59 }
[325]60}
Note: See TracBrowser for help on using the repository browser.