- 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; module dlog.context;
import std.conv : to;
/** /**
* Context that is passed in with the call to log * Context that is passed in with the call to log
*/ */
@ -28,12 +30,12 @@ public class Context
*/ */
public struct CompilationInfo public struct CompilationInfo
{ {
private string fullFilePath; public string fullFilePath;
private string file; public string file;
private ulong line; public ulong line;
private string moduleName; public string moduleName;
private string functionName; public string functionName;
private string prettyFunctionName; public string prettyFunctionName;
this(string fullFilePath, string file, ulong line, string moduleName, string functionName, 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.functionName = functionName;
this.prettyFunctionName = prettyFunctionName; 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; this.multiArgJoiner = multiArgJoiner;
} }
// TODO: Working on this
public final void log2(TextType...)(TextType message, CompilationInfo compilationInfo = CompilationInfo( unittest
__FILE_FULL_PATH__,
__FILE__,
__LINE__,
__MODULE__,
__FUNCTION__,
__PRETTY_FUNCTION__))
{ {
// Create default context Logger logger = new DefaultLogger();
Context context = new Context();
// TODO: DO the same sort of grab for the CompilationInfo alias testParameters = AliasSeq!("This is a log message", 1.1, true, [1,2,3], 'f', logger);
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)
/* Set the line information in the provided Context */ // Same as above but with a custom joiner set
context.setLineInfo(compilationInfo); logger = new DefaultLogger(" ");
logger.log3(testParameters);
/* Grab at compile-time all arguments and generate runtime code to add them to `components` */ Context ctx = new Context();
string[] components; logger.log3Ctx(ctx, testParameters);
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
} }
/** import dlog.context;
* Log a message
*/ public final void log3(TextType...)(TextType segments, 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 c6 = __PRETTY_FUNCTION__)
{ {
/* Default context extras ios nothing */ /* Use the default context `Context` */
string[] contextExtras = null; Context defaultContext = new Context();
static if(__traits(isSame, typeof(message[$-1]), mixin(`string[]`)))
{ /* Build up the line information */
contextExtras = message[$-1]; CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
version(unittest)
{ /* Set the line information in the context */
pragma(msg, "meta: log: Found a custom string[] context array"); 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` */ /* Grab at compile-time all arguments and generate runtime code to add them to `components` */
string[] components; string[] components;
static foreach(messageComponent; message) static foreach(messageComponent; segments)
{ {
components ~= to!(string)(messageComponent); components ~= to!(string)(messageComponent);
} }
/* Join all `components` into a single string */ /* Join all `components` into a single string */
string messageOut = join(components, multiArgJoiner); string messageOut = join(components, multiArgJoiner);
/* Apply the transformation on the message */ /* Apply the transformation on the message */
string transformedMesage = messageTransform.execute(messageOut, context); string transformedMesage = messageTransform.execute(messageOut, context);
@ -134,6 +108,53 @@ public class Logger
logImpl(transformedMesage); 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 * Logging implementation, this is where the fina
* transformed text will be transferred to and finally * transformed text will be transferred to and finally
@ -186,25 +207,25 @@ unittest
writeln(); writeln();
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 // // Test various types one-by-one
static foreach(testParameter; testParameters) // static foreach(testParameter; testParameters)
{ // {
logger.log(testParameter, ["cool context"]); // logger.log(["cool context"], testParameter);
} // }
// Test various parameters (of various types) all at once // // Test various parameters (of various types) all at once
logger.log(testParameters, ["cool context"]); // logger.log(["cool context"], testParameters);
// Same as above but with a custom joiner set // // Same as above but with a custom joiner set
logger = new DefaultLogger("(-)"); // logger = new DefaultLogger("(-)");
logger.log(testParameters, ["cool context"]); // 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); 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 // Test various parameters (of various types) all at once
logger.log2(testParameters); // logger.log2(testParameters);
// Same as above but with a custom joiner set // // Same as above but with a custom joiner set
logger = new DefaultLogger("(-)"); // logger = new DefaultLogger("(-)");
logger.log2(testParameters); // 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); 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 // Test various parameters (of various types) all at once
logger.log2(testParameters); // logger.log2(testParameters);
// Same as above but with a custom joiner set // // Same as above but with a custom joiner set
logger = new DefaultLogger("(-)"); // logger = new DefaultLogger("(-)");
logger.log2(testParameters, new Context()); // 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); 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 // Test various parameters (of various types) all at once
logger.log2(testParameters); // logger.log2(testParameters);
// Same as above but with a custom joiner set // // Same as above but with a custom joiner set
logger = new DefaultLogger("(-)"); // logger = new DefaultLogger("(-)");
logger.log2(testParameters, CompilationInfo(__FILE_FULL_PATH__, __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__)); // 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 // Same as above but with a custom joiner set
logger = new DefaultLogger("(-)"); 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(); writeln();
} }

View File

@ -5,6 +5,8 @@ module dlog.defaults;
import dlog.core : Logger; import dlog.core : Logger;
import dlog.transform : MessageTransform; import dlog.transform : MessageTransform;
import dlog.context : Context, CompilationInfo;
import std.conv : to;
/** /**
* DefaultLogger * DefaultLogger
@ -43,8 +45,11 @@ public final class DefaultTransform : MessageTransform
* context = the context (if any) * context = the context (if any)
* Returns: the transformed text * 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; string message;
/* Date and time */ /* Date and time */

View File

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