- Made very nice
This commit is contained in:
Tristan B. Velloza Kildaire 2023-02-21 17:48:15 +02:00
parent e551c2262d
commit 934a1f85d8
1 changed files with 133 additions and 24 deletions

View File

@ -3,6 +3,11 @@ module gogga;
import std.conv : to;
import std.stdio : write, stdout;
// TODO: Remove (just for testing)
import std.stdio : writeln;
import dlog;
public enum DebugType
{
INFO,
@ -10,7 +15,112 @@ public enum DebugType
ERROR
}
byte[] generateMessage(string message, DebugType debugType)
unittest
{
GoggaLogger gLogger = new GoggaLogger();
// gLogger.log("Bruh\n");
gLogger.print("Bruh\n", DebugType.INFO);
gLogger.enableDebug();
gLogger.dbg("Bruh debug\n", DebugType.INFO);
gLogger.disableDebug();
gLogger.dbg("Bruh debug\n", DebugType.ERROR);
}
unittest
{
GoggaLogger gLogger = new GoggaLogger();
alias debugTypes = __traits(allMembers, DebugType);
static foreach(debugType; debugTypes)
{
gLogger.print("Hello world\n", mixin("DebugType."~debugType));
}
}
public class GoggaLogger : Logger
{
private GoggaTransform gTransform = new GoggaTransform();
this()
{
super(gTransform);
}
public override void logImpl(string text)
{
import std.stdio : write;
write(text);
}
// public alias gprint = ginfo;
// public alias gprintln = ginfoln;
// public void ginfo(string message)
// {
// log(message);
// }
// public void ginfoln(string message)
// {
// ginfo(message~"\n");
// }
// TODO: Find out how to do a partial function using meta-programming in D
// public alias error() = print()
public void dbg(string message, DebugType debugType, string c1 = __FILE_FULL_PATH__,
string c2 = __FILE__, ulong c3 = __LINE__,
string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
if(debugEnabled)
{
print(message, debugType, c1, c2, c3, c4, c5, c6);
}
}
public void print(string message, DebugType debugType, string c1 = __FILE_FULL_PATH__,
string c2 = __FILE__, ulong c3 = __LINE__,
string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
string[] contextExtras = [to!(string)(debugType)];
log(message, c1, c2, c3, c4, c5, c6, contextExtras);
}
private bool debugEnabled = false;
// Any calls to debugPrint will actually occur
public void enableDebug()
{
this.debugEnabled = true;
}
// Any calls to debugPrint will not occur
public void disableDebug()
{
this.debugEnabled = false;
}
}
public class GoggaTransform : MessageTransform
{
private bool isSourceMode = false;
public override string transform(string text, string[] context)
{
/* Module information (and status debugColoring) */
string moduleInfo = cast(string)debugColor("["~context[1]~"]", to!(DebugType)(context[6]));
/* Function and line number info */
string funcInfo = cast(string)(colorSrc("("~context[4]~":"~context[2]~")"));
return moduleInfo~" "~funcInfo~" "~text;
}
}
private byte[] debugColor(string text, DebugType debugType)
{
/* The generated message */
byte[] messageBytes;
@ -31,41 +141,40 @@ byte[] generateMessage(string message, DebugType debugType)
messageBytes = cast(byte[])[27, '[','3','1','m'];
}
/* Write the message type */
messageBytes ~= "["~to!(string)(debugType)~"] ";
/* Add the message */
messageBytes ~= cast(byte[])text;
/* Switch back color */
/* Switch back debugColor */
messageBytes ~= cast(byte[])[27, '[', '3', '9', 'm'];
/* Append message */
messageBytes ~= message;
return messageBytes;
}
void gprint(messageT)(messageT message, DebugType debugType = DebugType.INFO)
private byte[] colorSrc(string text)
{
/* Generate the message */
byte[] messageBytes = generateMessage(message, debugType);
/* The generated message */
byte[] messageBytes;
/* Print the message */
write(cast(string)messageBytes);
}
/* Reset coloring */
messageBytes ~= [27, '[', 'm'];
void gprintln(messageT)(messageT message, DebugType debugType = DebugType.INFO)
{
/* Generate the string to print */
string printStr = to!(string)(message)~"\n";
/* Color gray */
messageBytes ~= [27, '[', '3', '9', ';', '2', 'm'];
/* Call `gprint` */
gprint(printStr, debugType);
/* Append the message */
messageBytes ~= text;
/* Reset coloring */
messageBytes ~= [27, '[', 'm'];
return messageBytes;
}
unittest
{
alias debugTypes = __traits(allMembers, DebugType);
static foreach(debugType; debugTypes)
{
gprintln("Hello world", mixin("DebugType."~debugType));
}
// alias debugTypes = __traits(allMembers, DebugType);
// static foreach(debugType; debugTypes)
// {
// gprintln("Hello world", mixin("DebugType."~debugType));
// }
}