dlog/source/dlog/core.d

253 lines
4.6 KiB
D
Raw Permalink Normal View History

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 17:59:25 +01:00
import std.container.slist : SList;
import core.sync.mutex : Mutex;
/**
* The base message type
*/
public abstract class Message
2021-12-23 10:14:36 +00:00
{
2021-12-23 10:14:36 +00:00
}
2024-04-09 17:59:25 +01:00
/**
* Defines the filtering
* interface
*/
2024-04-09 16:26:55 +01:00
public interface Filter
{
2024-04-09 17:59:25 +01:00
/**
* Filters the given message
* returning a verdict
* based on it
*
* Params:
* message = the message
* Returns: the verdct
*/
2024-04-09 16:26:55 +01:00
public bool filter(Message message);
}
2021-12-23 10:14:36 +00:00
2024-04-09 17:59:25 +01:00
/**
* Defines the message
* transformation interface
*/
2024-04-09 16:26:55 +01:00
public interface Transform
2021-12-23 10:14:36 +00:00
{
2024-04-09 17:59:25 +01:00
/**
* Transforms the given message
*
* Params:
* message = the message input
* Returns: the transformed
* message
*/
2024-04-09 16:26:55 +01:00
public Message transform(Message message);
}
2024-04-09 17:59:25 +01:00
/**
* Defines the interface
* for handling messages
*/
2024-04-09 16:26:55 +01:00
public interface Handler
{
2024-04-09 17:59:25 +01:00
/**
* Handles the given message
*
* Params:
* message = the message to
* handle
*/
2024-04-09 16:26:55 +01:00
public void handle(Message message);
}
2024-04-09 18:04:31 +01:00
/**
* Defines the base logger
* and functionality associated
* with it
*/
2024-04-09 16:26:55 +01:00
public abstract class Logger
{
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
2024-04-09 18:04:31 +01:00
/**
* Constructs a new logger
*/
2024-04-09 16:26:55 +01:00
this()
{
this.lock = new Mutex();
}
// TODO: Handle duplicate?
2024-04-09 18:04:31 +01:00
/**
* Adds the given transform
*
* Params:
* transform = the transform
* to add
*/
2024-04-09 16:26:55 +01:00
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?
2024-04-09 18:04:31 +01:00
/**
* Removes the given transform
*
* Params:
* transform = the transform
* to remove
*/
2024-04-09 16:26:55 +01:00
public final void removeTransform(Transform transform)
{
scope(exit)
{
this.lock.unlock();
}
this.lock.lock();
this.transforms.linearRemoveElement(transform);
}
// TODO: Handle duplicate?
2024-04-09 18:04:31 +01:00
/**
* Adds the given filter
*
* Params:
* filter = the filter
* to add
*/
2024-04-09 16:26:55 +01:00
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?
2024-04-09 18:04:31 +01:00
/**
* Removes the given filter
*
* Params:
* filter = the filter
* to remove
*/
2024-04-09 16:26:55 +01:00
public final void removeFilter(Filter filter)
{
scope(exit)
{
this.lock.unlock();
}
this.lock.lock();
this.filters.linearRemoveElement(filter);
}
// TODO: Handle duplicate?
2024-04-09 18:04:31 +01:00
/**
* Adds the given handler
*
* Params:
* handler = the handler
* to add
*/
2024-04-09 16:26:55 +01:00
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?
2024-04-09 18:04:31 +01:00
/**
* Removes the given handler
*
* Params:
* handler = the handler
* to remove
*/
2024-04-09 16:26:55 +01:00
public final void removeHandler(Handler handler)
{
scope(exit)
{
this.lock.unlock();
}
this.lock.lock();
this.handlers.linearRemoveElement(handler);
}
2024-04-09 18:04:31 +01:00
/**
* Logs the provided message by processing
* it through all the filters, and if
* the verdict is `true` then transforms
* the message via all transformers
* and finally dispatches said message
* to all attached handlers
*
* Params:
* message = the message
*/
2024-04-09 16:26:55 +01:00
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);
}
}
}