mirror of https://github.com/deavmi/dlog.git
108 lines
2.0 KiB
D
108 lines
2.0 KiB
D
module dlog.nu.core;
|
|
|
|
public class Message
|
|
{
|
|
|
|
}
|
|
|
|
public interface Filter
|
|
{
|
|
public bool filter(Message message);
|
|
}
|
|
|
|
public interface Transform
|
|
{
|
|
public Message transform(Message message);
|
|
}
|
|
|
|
public interface Handler
|
|
{
|
|
public void handle(Message message);
|
|
}
|
|
|
|
import std.container.slist : SList;
|
|
// import std.range : in;
|
|
import core.sync.mutex : Mutex;
|
|
|
|
// private mixin template Ting(Mutex lock)
|
|
// {
|
|
// scope(exit)
|
|
// {
|
|
// lock.unlock();
|
|
// }
|
|
|
|
// lock.lock();
|
|
// }
|
|
|
|
public abstract class Logger
|
|
{
|
|
private SList!(Transform) transforms;
|
|
private SList!(Filter) filters;
|
|
private SList!(Handler) handlers;
|
|
private Mutex lock; // Lock for attached handlers, filters and transforms
|
|
|
|
this()
|
|
{
|
|
this.lock = new Mutex();
|
|
}
|
|
|
|
// TODO: Handle duplicate?
|
|
public final void addTransform(Transform transform)
|
|
{
|
|
scope(exit)
|
|
{
|
|
this.lock.unlock();
|
|
}
|
|
|
|
this.lock.lock();
|
|
|
|
this.transforms.insertAfter(this.transforms[], transform);
|
|
}
|
|
|
|
// TODO: Hanmdle not found explicitly?
|
|
public final void removeTransform(Transform transform)
|
|
{
|
|
scope(exit)
|
|
{
|
|
this.lock.unlock();
|
|
}
|
|
|
|
this.lock.lock();
|
|
|
|
this.transforms.linearRemoveElement(transform);
|
|
}
|
|
|
|
// Logs an actual message
|
|
//
|
|
// This first passes it over all filters
|
|
// then to a transform and then copies
|
|
// to each handler
|
|
public final void log(Message message)
|
|
{
|
|
scope(exit)
|
|
{
|
|
this.lock.unlock();
|
|
}
|
|
|
|
this.lock.lock();
|
|
|
|
foreach(Filter filter; this.filters)
|
|
{
|
|
if(!filter.filter(message))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
Message curMessage = message;
|
|
foreach(Transform transform; this.transforms)
|
|
{
|
|
curMessage = transform.transform(curMessage);
|
|
}
|
|
|
|
foreach(Handler handler; this.handlers)
|
|
{
|
|
handler.handle(curMessage);
|
|
}
|
|
}
|
|
} |