using System;
using System.Collections.Generic;
namespace TeamAAS.Logging
{
///
/// 内存日志总线:AppLogger / PluginLogger 写文件的同时把结构化条目发布到这里,
/// 实时日志控件(LogView)订阅 联动显示,并可用 回填历史。
/// 发布过程吞掉一切异常,绝不影响业务流程。
///
public static class LogStream
{
/// 环形缓冲容量(新控件加载时可回填的最近条目数)。
public const int BufferCapacity = 1000;
private static readonly object Gate = new object();
private static readonly Queue Buffer = new Queue();
/// 有新日志条目时触发(可能在任意后台线程触发,订阅方自行切 UI 线程)。
public static event Action Published;
/// 发布一条日志条目:入环形缓冲并通知订阅者。
public static void Publish(LogEntry entry)
{
if (entry == null) return;
lock (Gate)
{
Buffer.Enqueue(entry);
while (Buffer.Count > BufferCapacity)
Buffer.Dequeue();
}
try
{
Published?.Invoke(entry);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("[LogStream] publish failed: " + ex.Message);
}
}
/// 返回当前缓冲的快照(最旧→最新),供控件首次加载回填。
public static List Snapshot()
{
lock (Gate)
{
return new List(Buffer);
}
}
/// 清空缓冲(不影响已显示在控件里的条目)。
public static void Clear()
{
lock (Gate)
{
Buffer.Clear();
}
}
}
}