mirror of https://github.com/deavmi/dlog.git
Compare commits
10 Commits
bb8f71c9db
...
6e48960022
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | 6e48960022 | |
Tristan B. Velloza Kildaire | fbada61e90 | |
Tristan B. Velloza Kildaire | 5447f6dfbd | |
Tristan B. Velloza Kildaire | 39c2fe78fd | |
Tristan B. Velloza Kildaire | 2658ed20b5 | |
Tristan B. Velloza Kildaire | b13621fd82 | |
Tristan B. Velloza Kildaire | f6ebb070cc | |
Tristan B. Velloza Kildaire | e35f6fba73 | |
Tristan B. Velloza Kildaire | b6d002e711 | |
Tristan B. Velloza Kildaire | d07cab3560 |
|
@ -0,0 +1,122 @@
|
|||
module dlog.nu.basic;
|
||||
|
||||
import dlog.nu.core;
|
||||
|
||||
public class BasicMessage : Message
|
||||
{
|
||||
private string text;
|
||||
private Level level;
|
||||
|
||||
this(string text, Level level = Level.INFO)
|
||||
{
|
||||
this.text = text;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
this()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void setText(string text)
|
||||
{
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public string getText()
|
||||
{
|
||||
return this.text;
|
||||
}
|
||||
|
||||
public Level getLevel()
|
||||
{
|
||||
return this.level;
|
||||
}
|
||||
}
|
||||
|
||||
public class Context
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class FileHandler : Handler
|
||||
{
|
||||
import std.stdio : File;
|
||||
private File file;
|
||||
this(File file)
|
||||
{
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public void handle(Message message)
|
||||
{
|
||||
// Only handle BasicMessage(s)
|
||||
BasicMessage bmesg = cast(BasicMessage)message;
|
||||
if(bmesg)
|
||||
{
|
||||
file.writeln(bmesg.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum Level
|
||||
{
|
||||
ERROR,
|
||||
WARNING,
|
||||
INFO,
|
||||
DEBUG
|
||||
}
|
||||
|
||||
private class LevelFilter : Filter
|
||||
{
|
||||
private Level* level;
|
||||
|
||||
this(Level* level)
|
||||
{
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public bool filter(Message message)
|
||||
{
|
||||
// Only handle BasicMessage(s)
|
||||
BasicMessage bmesg = cast(BasicMessage)message;
|
||||
if(bmesg)
|
||||
{
|
||||
return bmesg.getLevel() <= *this.level;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class BasicLogger : Logger
|
||||
{
|
||||
private Level level;
|
||||
|
||||
this()
|
||||
{
|
||||
// Attach a new level-filter
|
||||
// with access to our current
|
||||
// level
|
||||
addFilter(new LevelFilter(&level));
|
||||
}
|
||||
|
||||
public final void setLevel(Level level)
|
||||
{
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public final Level getLevel()
|
||||
{
|
||||
return this.level;
|
||||
}
|
||||
}
|
||||
|
||||
public class ConsoleLogger : BasicLogger
|
||||
{
|
||||
this()
|
||||
{
|
||||
import std.stdio;
|
||||
addHandler(new FileHandler(stdout));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,160 @@
|
|||
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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue