- Upgraded to `dlog ` `v0.3.6`
- Override `warn`, `debug_`, `info` and `error` and instantiate a new `GoggaContext` within
- Fixed `debugColor(string, Level)`

GoggaContext

- Removed overriden methods
This commit is contained in:
Tristan B. Velloza Kildaire 2023-03-02 11:40:47 +02:00
parent 1cf5f36dfe
commit 8766e3bcc5
2 changed files with 163 additions and 72 deletions

View File

@ -4,7 +4,7 @@
], ],
"copyright": "Copyright © 2020, Tristan B. Kildaire", "copyright": "Copyright © 2020, Tristan B. Kildaire",
"dependencies": { "dependencies": {
"dlog": "0.2.1" "dlog": "0.3.6"
}, },
"description": "Simple VT100 colourised thread safe pretty printer for debug messages", "description": "Simple VT100 colourised thread safe pretty printer for debug messages",
"license": "LGPL", "license": "LGPL",

View File

@ -7,6 +7,8 @@ import std.stdio : write, stdout;
import std.stdio : writeln; import std.stdio : writeln;
import dlog; import dlog;
import dlog.utilities : flatten;
import std.array : join;
public enum DebugType public enum DebugType
{ {
@ -45,17 +47,7 @@ unittest
public final class GoggaContext : Context public final class GoggaContext : Context
{ {
private DebugType msgType; // TODO: Put more advanced stuff here
public DebugType getLevel()
{
return msgType;
}
public void setLevel(DebugType msgType)
{
this.msgType = msgType;
}
} }
public class GoggaLogger : Logger public class GoggaLogger : Logger
@ -73,81 +65,180 @@ public class GoggaLogger : Logger
write(text); write(text);
} }
// public alias gprint = ginfo; /**
// public alias gprintln = ginfoln; * Logs using the default context an arbitrary amount of arguments
* specifically setting the context's level to ERROR
// public void ginfo(string message) *
// { * Params:
// log(message); * segments = the arbitrary argumnets (alias sequence)
// } * __FILE_FULL_PATH__ = compile time usage file
* __FILE__ = compile time usage file (relative)
// public void ginfoln(string message) * __LINE__ = compile time usage line number
// { * __MODULE__ = compile time usage module
// ginfo(message~"\n"); * __FUNCTION__ = compile time usage function
// } * __PRETTY_FUNCTION__ = compile time usage function (pretty)
*/
// TODO: Find out how to do a partial function using meta-programming in D public final void error(TextType...)(TextType segments,
// public alias error() = print() string c1 = __FILE_FULL_PATH__,
public void dbg(string message, DebugType debugType, 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__)
{ {
if(debugEnabled) /* Use the context `GoggaContext` */
{ GoggaContext defaultContext = new GoggaContext();
print(message, debugType, c1, c2, c3, c4, c5, c6);
}
}
public void info(T...)(T message, string c1 = __FILE_FULL_PATH__, /* Build up the line information */
CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
/* Set the line information in the context */
defaultContext.setLineInfo(compilationInfo);
/* Set the level to ERROR */
defaultContext.setLevel(Level.ERROR);
/**
* Grab at compile-time all arguments and generate runtime code to add them to `components`
*/
string[] components = flatten(segments);
/* Join all `components` into a single string */
string messageOut = join(components, multiArgJoiner);
/* Call the log */
logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
}
/**
* Logs using the default context an arbitrary amount of arguments
* specifically setting the context's level to INFO
*
* Params:
* segments = the arbitrary argumnets (alias sequence)
* __FILE_FULL_PATH__ = compile time usage file
* __FILE__ = compile time usage file (relative)
* __LINE__ = compile time usage line number
* __MODULE__ = compile time usage module
* __FUNCTION__ = compile time usage function
* __PRETTY_FUNCTION__ = compile time usage function (pretty)
*/
public final void info(TextType...)(TextType segments,
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__)
{ {
/* Create a Gogga context with the correct level */ /* Use the context `GoggaContext` */
GoggaContext context = new GoggaContext(); GoggaContext defaultContext = new GoggaContext();
context.setLevel(DebugType.INFO);
/* Do a custom context log */ /* Build up the line information */
logc(context, message, c1=c1, c2=c2, c3=c3, c4=c4, c5=c5, c6=c6); CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
}
public void warn(T...)(T message, string c1 = __FILE_FULL_PATH__, /* Set the line information in the context */
defaultContext.setLineInfo(compilationInfo);
/* Set the level to INFO */
defaultContext.setLevel(Level.INFO);
/**
* Grab at compile-time all arguments and generate runtime code to add them to `components`
*/
string[] components = flatten(segments);
/* Join all `components` into a single string */
string messageOut = join(components, multiArgJoiner);
/* Call the log */
logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
}
/**
* Logs using the default context an arbitrary amount of arguments
* specifically setting the context's level to WARN
*
* Params:
* segments = the arbitrary argumnets (alias sequence)
* __FILE_FULL_PATH__ = compile time usage file
* __FILE__ = compile time usage file (relative)
* __LINE__ = compile time usage line number
* __MODULE__ = compile time usage module
* __FUNCTION__ = compile time usage function
* __PRETTY_FUNCTION__ = compile time usage function (pretty)
*/
public final void warn(TextType...)(TextType segments,
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__)
{ {
/* Create a Gogga context with the correct level */ /* Use the context `GoggaContext` */
GoggaContext context = new GoggaContext(); GoggaContext defaultContext = new GoggaContext();
context.setLevel(DebugType.WARNING);
/* Do a custom context log */ /* Build up the line information */
logc(context, message, c1, c2, c3, c4, c5, c6); CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
}
public void error(T...)(string c1 = __FILE_FULL_PATH__, /* Set the line information in the context */
string c2 = __FILE__, ulong c3 = __LINE__, defaultContext.setLineInfo(compilationInfo);
string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__, T message)
{
/* Create a Gogga context with the correct level */
GoggaContext context = new GoggaContext();
context.setLevel(DebugType.ERROR);
/* Do a custom context log */ /* Set the level to WARN */
logc(context, message, c1, c2, c3, c4, c5, c6); defaultContext.setLevel(Level.WARN);
}
// TODO: Alias/meta-programmed based println and dbgLn and yeah /**
public void print(string message, DebugType debugType, string c1 = __FILE_FULL_PATH__, * Grab at compile-time all arguments and generate runtime code to add them to `components`
*/
string[] components = flatten(segments);
/* Join all `components` into a single string */
string messageOut = join(components, multiArgJoiner);
/* Call the log */
logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
}
/**
* Logs using the default context an arbitrary amount of arguments
* specifically setting the context's level to DEBUG
*
* Params:
* segments = the arbitrary argumnets (alias sequence)
* __FILE_FULL_PATH__ = compile time usage file
* __FILE__ = compile time usage file (relative)
* __LINE__ = compile time usage line number
* __MODULE__ = compile time usage module
* __FUNCTION__ = compile time usage function
* __PRETTY_FUNCTION__ = compile time usage function (pretty)
*/
public final void debug_(TextType...)(TextType segments,
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__)
{ {
string[] contextExtras = [to!(string)(debugType)]; /* Use the context `GoggaContext` */
log(message, c1, c2, c3, c4, c5, c6, contextExtras); GoggaContext defaultContext = new GoggaContext();
}
/* Build up the line information */
CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
/* Set the line information in the context */
defaultContext.setLineInfo(compilationInfo);
/* Set the level to DEBUG */
defaultContext.setLevel(Level.DEBUG);
/**
* Grab at compile-time all arguments and generate runtime code to add them to `components`
*/
string[] components = flatten(segments);
/* Join all `components` into a single string */
string messageOut = join(components, multiArgJoiner);
/* Call the log */
logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
}
/* You can also call using `dbg` */
public alias dbg = debug_;
private bool debugEnabled = false; private bool debugEnabled = false;
@ -187,23 +278,23 @@ public class GoggaTransform : MessageTransform
} }
} }
private byte[] debugColor(string text, DebugType debugType) private byte[] debugColor(string text, Level level)
{ {
/* The generated message */ /* The generated message */
byte[] messageBytes; byte[] messageBytes;
/* If INFO, set green */ /* If INFO, set green */
if(debugType == DebugType.INFO) if(level == Level.INFO)
{ {
messageBytes = cast(byte[])[27, '[','3','2','m']; messageBytes = cast(byte[])[27, '[','3','2','m'];
} }
/* If WARNING, set warning */ /* If WARNING, set warning */
else if(debugType == DebugType.WARNING) else if(level == Level.WARN)
{ {
messageBytes = cast(byte[])[27, '[','3','5','m']; /* TODO: FInd yllow */ messageBytes = cast(byte[])[27, '[','3','5','m']; /* TODO: FInd yllow */
} }
/* If ERROR, set error */ /* If ERROR, set error */
else if(debugType == DebugType.ERROR) else if(level == Level.ERROR)
{ {
messageBytes = cast(byte[])[27, '[','3','1','m']; messageBytes = cast(byte[])[27, '[','3','1','m'];
} }