Compare commits

...

10 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 6e48960022 BasicMessage
- Added `setText(string)`
2024-04-09 12:09:30 +02:00
Tristan B. Velloza Kildaire fbada61e90 BasicLogger
- Added `getLevel()`
2024-04-09 08:47:43 +02:00
Tristan B. Velloza Kildaire 5447f6dfbd ConsoleLogger
- Added new logger
2024-04-09 08:46:29 +02:00
Tristan B. Velloza Kildaire 39c2fe78fd BasicLogger
- Now stores current level in a filter of its own
- The `LevelFilter` is attached to the `BasicLogger` on construction

LevelFilter

- Now use a `Level*` rather
2024-04-09 08:44:30 +02:00
Tristan B. Velloza Kildaire 2658ed20b5 BasicMessage
- Now supports a `Level` setting

Level

- Added new enum type

LevelFilter

- Added implementation for level filtering
2024-04-09 08:41:08 +02:00
Tristan B. Velloza Kildaire b13621fd82 Logger
- You can now add and remove filters and handlers
2024-04-09 08:34:55 +02:00
Tristan B. Velloza Kildaire f6ebb070cc Logger
- Implemented `log(Message message)` which does filtering, transformation and lastly handling
2024-04-09 08:32:49 +02:00
Tristan B. Velloza Kildaire e35f6fba73 Core
- Added methods for manipulating transforms
2024-04-09 08:29:36 +02:00
Tristan B. Velloza Kildaire b6d002e711 Basic
- Setting out use cases
2024-04-09 08:24:34 +02:00
Tristan B. Velloza Kildaire d07cab3560 Core
- Setting out new API
2024-04-09 08:24:21 +02:00
2 changed files with 282 additions and 0 deletions

122
source/dlog/nu/basic.d Normal file
View File

@ -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));
}
}

160
source/dlog/nu/core.d Normal file
View File

@ -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);
}
}
}