source: binary-improvements/MapRendering/Web/SSE/EventLog.cs@ 367

Last change on this file since 367 was 367, checked in by alloc, 3 years ago

Web:

  • Added SSE (ServerSentEvents) subsystem
  • Added log endpoint to SSE. Less heavy weight and more responsive way of watching the server log
  • Bunch of refactoring
File size: 1.5 KB
Line 
1using System;
2using System.Net;
3using System.Text;
4using System.Text.RegularExpressions;
5using AllocsFixes.JSON;
6using UnityEngine;
7
8namespace AllocsFixes.NetConnections.Servers.Web.SSE {
9 public class EventLog : EventBase {
10 private static readonly Regex logMessageMatcher =
11 new Regex (@"^([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2}) ([0-9]+[,.][0-9]+) [A-Z]+ (.*)$");
12
13 public EventLog (SseHandler _parent) : base (_parent, _name: "log") {
14 Logger.Main.LogCallbacks += LogCallback;
15 }
16
17
18 private void LogCallback (string _msg, string _trace, LogType _type) {
19 Match match = logMessageMatcher.Match (_msg);
20
21 string date;
22 string time;
23 string uptime;
24 string message;
25 if (match.Success) {
26 date = match.Groups [1].Value;
27 time = match.Groups [2].Value;
28 uptime = match.Groups [3].Value;
29 message = match.Groups [4].Value;
30 } else {
31 DateTime dt = DateTime.Now;
32 date = $"{dt.Year:0000}-{dt.Month:00}-{dt.Day:00}";
33 time = $"{dt.Hour:00}:{dt.Minute:00}:{dt.Second:00}";
34 uptime = "";
35 message = _msg;
36 }
37
38 JSONObject data = new JSONObject ();
39 data.Add ("msg", new JSONString (message));
40 data.Add ("type", new JSONString (_type.ToStringCached ()));
41 data.Add ("trace", new JSONString (_trace));
42 data.Add ("date", new JSONString (date));
43 data.Add ("time", new JSONString (time));
44 data.Add ("uptime", new JSONString (uptime));
45
46 SendData ("logLine", data);
47 }
48
49 }
50}
Note: See TracBrowser for help on using the repository browser.