- Added the idea of a `Context`
- A `Context` default has `CompilationInfo` attached to it

Transform

- Switched to using `Context`

Core

- Added new `Context`-based functions, removed old
- Disabled some unit tests for now

Defaults

- Updated the defaults
This commit is contained in:
Tristan B. Velloza Kildaire 2023-03-01 16:30:49 +02:00
parent fbd45f1763
commit 494b96e4a0
4 changed files with 176 additions and 104 deletions

View File

@ -1,5 +1,7 @@
module dlog.context;
import std.conv : to;
/**
* Context that is passed in with the call to log
*/
@ -28,12 +30,12 @@ public class Context
*/
public struct CompilationInfo
{
private string fullFilePath;
private string file;
private ulong line;
private string moduleName;
private string functionName;
private string prettyFunctionName;
public string fullFilePath;
public string file;
public ulong line;
public string moduleName;
public string functionName;
public string prettyFunctionName;
this(string fullFilePath, string file, ulong line, string moduleName, string functionName, string prettyFunctionName)
{
@ -44,5 +46,11 @@ public struct CompilationInfo
this.functionName = functionName;
this.prettyFunctionName = prettyFunctionName;
}
public string[] toArray()
{
return [fullFilePath, file, to!(string)(line), moduleName, functionName, prettyFunctionName];
}
}

View File

@ -43,90 +43,64 @@ public class Logger
this.multiArgJoiner = multiArgJoiner;
}
// TODO: Working on this
public final void log2(TextType...)(TextType message, CompilationInfo compilationInfo = CompilationInfo(
__FILE_FULL_PATH__,
__FILE__,
__LINE__,
__MODULE__,
__FUNCTION__,
__PRETTY_FUNCTION__))
unittest
{
// Create default context
Context context = new Context();
Logger logger = new DefaultLogger();
// TODO: DO the same sort of grab for the CompilationInfo
static foreach(messageComponent; message)
{
static if(__traits(isSame, typeof(messageComponent), mixin(`CompilationInfo`)))
{
compilationInfo = messageComponent;
pragma(msg, "meta: log2 has a custom compilationInfo passed in");
}
else static if(__traits(isSame, typeof(messageComponent), mixin(`Context`)))
{
context = messageComponent;
pragma(msg, "meta: generated a log2 with a CUSTOM context");
}
}
// TODO: Gabd the context from the message[$] if it is of type Context (meta)
alias testParameters = AliasSeq!("This is a log message", 1.1, true, [1,2,3], 'f', logger);
/* Set the line information in the provided Context */
context.setLineInfo(compilationInfo);
/* 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);
// pragma(msg, styff);
// TODO: Implement suff here
// Same as above but with a custom joiner set
logger = new DefaultLogger(" ");
logger.log3(testParameters);
Context ctx = new Context();
logger.log3Ctx(ctx, testParameters);
}
/**
* Log a message
*/
public final void log(TextType...)(TextType message, string c1 = __FILE_FULL_PATH__,
import dlog.context;
public final void log3(TextType...)(TextType segments, string c1 = __FILE_FULL_PATH__,
string c2 = __FILE__, ulong c3 = __LINE__,
string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
/* Default context extras ios nothing */
string[] contextExtras = null;
static if(__traits(isSame, typeof(message[$-1]), mixin(`string[]`)))
{
contextExtras = message[$-1];
version(unittest)
{
pragma(msg, "meta: log: Found a custom string[] context array");
}
}
/* Use the default context `Context` */
Context defaultContext = new Context();
/* Build up the line information */
CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
/* Set the line information in the context */
defaultContext.setLineInfo(compilationInfo);
/* Call the log */
log3Ctx(defaultContext, segments);
}
public final void log3Ctx(Context, TextType...)(Context context, TextType segments, string c1 = __FILE_FULL_PATH__,
string c2 = __FILE__, ulong c3 = __LINE__,
string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
/* Build up the line information */
CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
/* Set the line information in the context */
context.setLineInfo(compilationInfo);
/* Construct context array */
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)
static foreach(messageComponent; segments)
{
components ~= to!(string)(messageComponent);
}
/* Join all `components` into a single string */
string messageOut = join(components, multiArgJoiner);
/* Apply the transformation on the message */
string transformedMesage = messageTransform.execute(messageOut, context);
@ -134,6 +108,53 @@ public class Logger
logImpl(transformedMesage);
}
alias log = log3;
alias logc = log3Ctx;
// /**
// * Log a message (default context))
// */
// public final void log(TextType...)(TextType message, string c1 = __FILE_FULL_PATH__,
// string c2 = __FILE__, ulong c3 = __LINE__,
// string c4 = __MODULE__, string c5 = __FUNCTION__,
// string c6 = __PRETTY_FUNCTION__)
// {
// /* Default context extras is nothing */
// string[] defaultContext = null;
// /* Call the log */
// logCtx(defaultContext, message);
// }
// /**
// * Log a message
// */
// public final void logCtx(string[] ctx, TextType...)(string[] contextExtras, TextType message, string c1 = __FILE_FULL_PATH__,
// string c2 = __FILE__, ulong c3 = __LINE__,
// string c4 = __MODULE__, string c5 = __FUNCTION__,
// string c6 = __PRETTY_FUNCTION__)
// {
// /* Construct context array */
// 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;
// int metaLen = message[].length;
// 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 */
// string transformedMesage = messageTransform.execute(messageOut, context);
// /* Call the underlying logger implementation */
// logImpl(transformedMesage);
// }
/**
* Logging implementation, this is where the fina
* transformed text will be transferred to and finally
@ -186,25 +207,25 @@ unittest
writeln();
writeln();
Logger logger = new DefaultLogger();
// Logger logger = new DefaultLogger();
alias testParameters = AliasSeq!("This is a log message", 1.1, true, [1,2,3], 'f', logger);
// alias testParameters = AliasSeq!("This is a log message", 1.1, true, [1,2,3], 'f', logger);
// Test various types one-by-one
static foreach(testParameter; testParameters)
{
logger.log(testParameter, ["cool context"]);
}
// // Test various types one-by-one
// static foreach(testParameter; testParameters)
// {
// logger.log(["cool context"], testParameter);
// }
// Test various parameters (of various types) all at once
logger.log(testParameters, ["cool context"]);
// // Test various parameters (of various types) all at once
// logger.log(["cool context"], testParameters);
// Same as above but with a custom joiner set
logger = new DefaultLogger("(-)");
logger.log(testParameters, ["cool context"]);
// // Same as above but with a custom joiner set
// logger = new DefaultLogger("(-)");
// logger.log(["cool context"], testParameters);
writeln();
// writeln();
}
/**
@ -221,13 +242,13 @@ unittest
alias testParameters = AliasSeq!("This is a log message", 1.1, true, [1,2,3], 'f', logger);
// Test various parameters (of various types) all at once
logger.log2(testParameters);
// logger.log2(testParameters);
// Same as above but with a custom joiner set
logger = new DefaultLogger("(-)");
logger.log2(testParameters);
// // Same as above but with a custom joiner set
// logger = new DefaultLogger("(-)");
// logger.log2(testParameters);
writeln();
// writeln();
}
/**
@ -244,13 +265,13 @@ unittest
alias testParameters = AliasSeq!("This is a log message", 1.1, true, [1,2,3], 'f', logger);
// Test various parameters (of various types) all at once
logger.log2(testParameters);
// logger.log2(testParameters);
// Same as above but with a custom joiner set
logger = new DefaultLogger("(-)");
logger.log2(testParameters, new Context());
// // Same as above but with a custom joiner set
// logger = new DefaultLogger("(-)");
// logger.log2(testParameters, new Context());
writeln();
// writeln();
}
/**
@ -267,13 +288,13 @@ unittest
alias testParameters = AliasSeq!("This is a log message", 1.1, true, [1,2,3], 'f', logger);
// Test various parameters (of various types) all at once
logger.log2(testParameters);
// logger.log2(testParameters);
// Same as above but with a custom joiner set
logger = new DefaultLogger("(-)");
logger.log2(testParameters, CompilationInfo(__FILE_FULL_PATH__, __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__));
// // Same as above but with a custom joiner set
// logger = new DefaultLogger("(-)");
// logger.log2(testParameters, CompilationInfo(__FILE_FULL_PATH__, __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__));
writeln();
// writeln();
}
/**
@ -292,7 +313,43 @@ unittest
// Same as above but with a custom joiner set
logger = new DefaultLogger("(-)");
logger.log2(testParameters, new Context(), CompilationInfo(__FILE_FULL_PATH__, __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__));
// logger.log2(testParameters, new Context(), CompilationInfo(__FILE_FULL_PATH__, __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__));
writeln();
}
/**
* Printing out some arrays, also using a DEFAULT context
*/
unittest
{
Logger logger = new DefaultLogger();
// Same as above but with a custom joiner set
logger = new DefaultLogger();
logger.log(["a", "b", "c", "d"], [1, 2], true);
writeln();
writeln();
}
/**
* Printing out some arrays, also using a CUSTOM context
*/
unittest
{
Logger logger = new DefaultLogger();
// Same as above but with a custom joiner set
logger = new DefaultLogger(" ");
// Create a noticeable custom context
// TODO: Actually make this somewhat noticeable
Context customContext = new Context();
// Log with the custom context
logger.logc(customContext, ["an", "array"], 1, "hello", true);
writeln();
writeln();
}

View File

@ -5,6 +5,8 @@ module dlog.defaults;
import dlog.core : Logger;
import dlog.transform : MessageTransform;
import dlog.context : Context, CompilationInfo;
import std.conv : to;
/**
* DefaultLogger
@ -43,8 +45,11 @@ public final class DefaultTransform : MessageTransform
* context = the context (if any)
* Returns: the transformed text
*/
public override string transform(string text, string[] context)
public override string transform(string text, Context ctx)
{
/* Extract the context */
string[] context = ctx.getLineInfo().toArray();
string message;
/* Date and time */

View File

@ -1,5 +1,7 @@
module dlog.transform;
import dlog.context : Context;
/**
* MessageTransform
*
@ -13,12 +15,12 @@ public abstract class MessageTransform
/* Next transformation (if any) */
private MessageTransform chainedTransform;
/**
/**
* The actual textual transformation.
*
* This is to be implemented by sub-classes
*/
public abstract string transform(string text, string[] context);
public abstract string transform(string text, Context context);
/**
* Chain a transform
@ -31,7 +33,7 @@ public abstract class MessageTransform
/**
* Perform the transformation
*/
public final string execute(string text, string[] context)
public final string execute(string text, Context context)
{
/* Perform the transformation */
string transformedText = transform(text, context);