[391] | 1 | using System;
|
---|
| 2 | using JetBrains.Annotations;
|
---|
| 3 | using UnityEngine;
|
---|
[402] | 4 | using Utf8Json;
|
---|
[391] | 5 | using Webserver.UrlHandlers;
|
---|
| 6 |
|
---|
| 7 | namespace Webserver.SSE {
|
---|
| 8 | [UsedImplicitly]
|
---|
| 9 | public class EventLog : AbsEvent {
|
---|
| 10 | public EventLog (SseHandler _parent) : base (_parent, _name: "log") {
|
---|
| 11 | Log.LogCallbacksExtended += LogCallback;
|
---|
| 12 | }
|
---|
| 13 |
|
---|
[402] | 14 | private static readonly byte[] jsonMsgKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("msg");
|
---|
| 15 | private static readonly byte[] jsonTypeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("type");
|
---|
| 16 | private static readonly byte[] jsonTraceKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("trace");
|
---|
| 17 | private static readonly byte[] jsonIsotimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("isotime");
|
---|
| 18 | private static readonly byte[] jsonUptimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("uptime");
|
---|
| 19 |
|
---|
[391] | 20 | private void LogCallback (string _formattedMsg, string _plainMsg, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
|
---|
| 21 | string isotime = _timestamp.ToString ("o");
|
---|
| 22 | string uptime = _uptime.ToString ();
|
---|
| 23 |
|
---|
[402] | 24 | JsonWriter writer = new JsonWriter ();
|
---|
| 25 |
|
---|
| 26 | writer.WriteRaw (jsonMsgKey);
|
---|
| 27 | writer.WriteString (_plainMsg);
|
---|
| 28 |
|
---|
| 29 | writer.WriteRaw (jsonTypeKey);
|
---|
| 30 | writer.WriteString (_type.ToStringCached ());
|
---|
| 31 |
|
---|
| 32 | writer.WriteRaw (jsonTraceKey);
|
---|
| 33 | writer.WriteString (_trace);
|
---|
| 34 |
|
---|
| 35 | writer.WriteRaw (jsonIsotimeKey);
|
---|
| 36 | writer.WriteString (isotime);
|
---|
| 37 |
|
---|
| 38 | writer.WriteRaw (jsonUptimeKey);
|
---|
| 39 | writer.WriteString (uptime);
|
---|
| 40 |
|
---|
| 41 | writer.WriteEndObject ();
|
---|
| 42 |
|
---|
| 43 | SendData ("logLine", writer.ToString ());
|
---|
[391] | 44 | }
|
---|
| 45 | }
|
---|
| 46 | }
|
---|