2024-04-09 17:56:33 +01:00
|
|
|
/**
|
|
|
|
* Core logging services
|
|
|
|
*
|
|
|
|
* Authors: Tristan Brice Velloza Kildaire (deavmi)
|
|
|
|
*/
|
2021-12-23 13:16:31 +00:00
|
|
|
module dlog.core;
|
2021-12-23 13:14:51 +00:00
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
public class Message
|
2021-12-23 10:14:36 +00:00
|
|
|
{
|
2023-03-02 09:11:57 +00:00
|
|
|
|
2021-12-23 10:14:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
public interface Filter
|
2023-03-01 07:15:10 +00:00
|
|
|
{
|
2024-04-09 16:26:55 +01:00
|
|
|
public bool filter(Message message);
|
2023-03-01 07:15:10 +00:00
|
|
|
}
|
2021-12-23 10:14:36 +00:00
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
public interface Transform
|
2021-12-23 10:14:36 +00:00
|
|
|
{
|
2024-04-09 16:26:55 +01:00
|
|
|
public Message transform(Message message);
|
2023-03-01 07:15:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
public interface Handler
|
2023-03-01 14:30:49 +00:00
|
|
|
{
|
2024-04-09 16:26:55 +01:00
|
|
|
public void handle(Message message);
|
2023-03-01 14:30:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
import std.container.slist : SList;
|
|
|
|
// import std.range : in;
|
|
|
|
import core.sync.mutex : Mutex;
|
2023-03-01 14:30:49 +00:00
|
|
|
|
2024-04-09 16:26:55 +01:00
|
|
|
public abstract class Logger
|
2023-03-02 09:09:28 +00:00
|
|
|
{
|
2024-04-09 16:26:55 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle duplicate?
|
|
|
|
public final void addFilter(Filter filter)
|
|
|
|
{
|
|
|
|
scope(exit)
|
|
|
|
{
|
|
|
|
this.lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lock.lock();
|
|
|
|
|
|
|
|
this.filters.insertAfter(this.filters[], filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Hanmdle not found explicitly?
|
|
|
|
public final void removeFilter(Filter filter)
|
|
|
|
{
|
|
|
|
scope(exit)
|
|
|
|
{
|
|
|
|
this.lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lock.lock();
|
|
|
|
|
|
|
|
this.filters.linearRemoveElement(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle duplicate?
|
|
|
|
public final void addHandler(Handler handler)
|
|
|
|
{
|
|
|
|
scope(exit)
|
|
|
|
{
|
|
|
|
this.lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lock.lock();
|
|
|
|
|
|
|
|
this.handlers.insertAfter(this.handlers[], handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Hanmdle not found explicitly?
|
|
|
|
public final void removeHandler(Handler handler)
|
|
|
|
{
|
|
|
|
scope(exit)
|
|
|
|
{
|
|
|
|
this.lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lock.lock();
|
|
|
|
|
|
|
|
this.handlers.linearRemoveElement(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2023-01-07 19:10:15 +00:00
|
|
|
}
|