- Now you can use arbitrary amount of arguments as the message text

- Ability to set the joiner of arguments in multi-variate case
This commit is contained in:
Tristan B. Velloza Kildaire 2023-01-07 21:10:15 +02:00
parent 9c55fee725
commit 6e7ab612dc
2 changed files with 47 additions and 16 deletions

View File

@ -6,21 +6,26 @@
module dlog.core; module dlog.core;
import std.conv : to; import std.conv : to;
import std.range : join;
import dlog.defaults; import dlog.defaults;
/** /**
* DefaultTransform * DefaultTransform
* *
* Provides a transformation of the kind * Provides a transformation of the kind
* *
* [date+time] (srcFile:lineNumber): message `\n` * [date+time] (srcFile:lineNumber): message `\n`
*/ */
public final class DefaultTransform : MessageTransform public final class DefaultTransform : MessageTransform
{ {
/**
/** * Performs the default transformation
* Our transformation *
*/ * Params:
* text = text input to transform
* context = the context (if any)
* Returns: the transformed text
*/
public override string transform(string text, string[] context) public override string transform(string text, string[] context)
{ {
string message; string message;
@ -54,37 +59,51 @@ public class Logger
/* Starting transformation */ /* Starting transformation */
private MessageTransform messageTransform; private MessageTransform messageTransform;
/* The multiple argument joiner */
private string multiArgJoiner;
/** /**
* Constructs a new Logger with the default * Constructs a new Logger with the default
* MessageTransform, see TODO (ref module) * MessageTransform, see TODO (ref module)
*/ */
this() this(string multiArgJoiner = " ")
{ {
this(new DefaultTransform()); this(new DefaultTransform(), multiArgJoiner);
} }
/** /**
* Constructs a new Logger with the given * Constructs a new Logger with the given
* MessageTransform * MessageTransform
*/ */
this(MessageTransform messageTransform) this(MessageTransform messageTransform, string multiArgJoiner = " ")
{ {
this.messageTransform = messageTransform; this.messageTransform = messageTransform;
this.multiArgJoiner = multiArgJoiner;
} }
/** /**
* Log a message * Log a message
*/ */
public final void log(TextType)(TextType message, string c1 = __FILE_FULL_PATH__, public final void log(TextType...)(TextType message, string c1 = __FILE_FULL_PATH__,
string c2 = __FILE__, ulong c3 = __LINE__, string c2 = __FILE__, ulong c3 = __LINE__,
string c4 = __MODULE__, string c5 = __FUNCTION__, string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__, string[] contextExtras = null) string c6 = __PRETTY_FUNCTION__, string[] contextExtras = null)
{ {
/* Construct context array */ /* Construct context array */
string[] context = [c1, c2, to!(string)(c3), c4, c5, c6]~contextExtras; string[] context = [c1, c2, to!(string)(c3), c4, c5, c6]~contextExtras;
/* Grab at compile-time all arguments and generate runtime code to add them to `components` */
string[] components;
static foreach(messageComponent; message)
{
components ~= to!(string)(messageComponent);
}
/* Join all `components` into a single string */
string messageOut = join(components, multiArgJoiner);
/* Apply the transformation on the message */ /* Apply the transformation on the message */
string transformedMesage = messageTransform.execute(to!(string)(message), context); string transformedMesage = messageTransform.execute(messageOut, context);
/* Call the underlying logger implementation */ /* Call the underlying logger implementation */
logImpl(transformedMesage); logImpl(transformedMesage);
@ -157,5 +176,16 @@ unittest
logger.log(true); logger.log(true);
logger.log([1,2,3]); logger.log([1,2,3]);
logger.log('f'); logger.log('f');
logger.log("Bruh");
logger.log(logger); logger.log(logger);
} }
unittest
{
Logger logger = new DefaultLogger();
logger.log(1, 1.1, true, [1,2,3], 'f', "Bruh", logger);
logger = new DefaultLogger("(-)");
logger.log(1, 1.1, true, [1,2,3], 'f', "Bruh", logger);
}

View File

@ -12,9 +12,10 @@ import dlog.core : Logger;
*/ */
public final class DefaultLogger : Logger public final class DefaultLogger : Logger
{ {
this() this(string multiArgJoiner = " ")
{ {
/* Use the DefaultTransform */ /* Use the DefaultTransform */
super(multiArgJoiner);
} }
protected override void logImpl(string message) protected override void logImpl(string message)