1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-10-30 05:42:47 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Services/Instances/InstanceLogManager.cs
2022-10-07 17:21:39 +02:00

45 lines
1.3 KiB
C#

using System.Collections.Concurrent;
using System.Collections.Immutable;
using Phantom.Common.Logging;
using Phantom.Utils.Collections;
using Phantom.Utils.Events;
using Serilog;
namespace Phantom.Server.Services.Instances;
public sealed class InstanceLogManager {
private const int RetainedLines = 1000;
private readonly ConcurrentDictionary<Guid, ObservableInstanceLogs> logsByInstanceGuid = new ();
private ObservableInstanceLogs GetInstanceLogs(Guid instanceGuid) {
return logsByInstanceGuid.GetOrAdd(instanceGuid, static _ => new ObservableInstanceLogs(PhantomLogger.Create<InstanceManager, ObservableInstanceLogs>()));
}
internal void AddLines(Guid instanceGuid, ImmutableArray<string> lines) {
GetInstanceLogs(instanceGuid).Add(lines);
}
public EventSubscribers<RingBuffer<string>> GetSubs(Guid instanceGuid) {
return GetInstanceLogs(instanceGuid).Subs;
}
private sealed class ObservableInstanceLogs : ObservableState<RingBuffer<string>> {
private readonly RingBuffer<string> log = new (RetainedLines);
public ObservableInstanceLogs(ILogger logger) : base(logger) {}
public void Add(ImmutableArray<string> lines) {
foreach (var line in lines) {
log.Add(line);
}
Update();
}
protected override RingBuffer<string> GetData() {
return log;
}
}
}