jellyfin/MediaBrowser.Common/Logging/ThreadedLogger.cs
LukePulverenti Luke Pulverenti luke pulverenti 2467ca9668 Moved some entities to the main project
2012-09-10 21:34:02 -04:00

74 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
namespace MediaBrowser.Common.Logging
{
public abstract class ThreadedLogger : BaseLogger
{
Thread loggingThread;
Queue<Action> queue = new Queue<Action>();
AutoResetEvent hasNewItems = new AutoResetEvent(false);
volatile bool terminate = false;
bool waiting = false;
public ThreadedLogger()
: base()
{
loggingThread = new Thread(new ThreadStart(ProcessQueue));
loggingThread.IsBackground = true;
loggingThread.Start();
}
void ProcessQueue()
{
while (!terminate)
{
waiting = true;
hasNewItems.WaitOne(10000, true);
waiting = false;
Queue<Action> queueCopy;
lock (queue)
{
queueCopy = new Queue<Action>(queue);
queue.Clear();
}
foreach (var log in queueCopy)
{
log();
}
}
}
protected override void LogEntry(LogRow row)
{
lock (queue)
{
queue.Enqueue(() => AsyncLogMessage(row));
}
hasNewItems.Set();
}
protected abstract void AsyncLogMessage(LogRow row);
protected override void Flush()
{
while (!waiting)
{
Thread.Sleep(1);
}
}
public override void Dispose()
{
Flush();
terminate = true;
hasNewItems.Set();
base.Dispose();
}
}
}