2020-10-29 11:19:12 +02:00
|
|
|
module gogga;
|
|
|
|
|
|
|
|
import std.conv : to;
|
|
|
|
import std.stdio : write, stdout;
|
|
|
|
|
2023-02-21 17:48:15 +02:00
|
|
|
// TODO: Remove (just for testing)
|
|
|
|
import std.stdio : writeln;
|
|
|
|
|
|
|
|
import dlog;
|
|
|
|
|
2020-10-29 11:19:12 +02:00
|
|
|
public enum DebugType
|
|
|
|
{
|
|
|
|
INFO,
|
|
|
|
WARNING,
|
|
|
|
ERROR
|
|
|
|
}
|
|
|
|
|
2023-02-21 17:48:15 +02:00
|
|
|
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)
|
2020-10-29 11:19:12 +02:00
|
|
|
{
|
2021-03-17 09:59:43 +02:00
|
|
|
/* The generated message */
|
|
|
|
byte[] messageBytes;
|
2020-10-29 11:19:12 +02:00
|
|
|
|
|
|
|
/* If INFO, set green */
|
|
|
|
if(debugType == DebugType.INFO)
|
|
|
|
{
|
2021-03-17 09:59:43 +02:00
|
|
|
messageBytes = cast(byte[])[27, '[','3','2','m'];
|
2020-10-29 11:19:12 +02:00
|
|
|
}
|
|
|
|
/* If WARNING, set warning */
|
|
|
|
else if(debugType == DebugType.WARNING)
|
|
|
|
{
|
2021-03-17 09:59:43 +02:00
|
|
|
messageBytes = cast(byte[])[27, '[','3','5','m']; /* TODO: FInd yllow */
|
2020-10-29 11:19:12 +02:00
|
|
|
}
|
|
|
|
/* If ERROR, set error */
|
|
|
|
else if(debugType == DebugType.ERROR)
|
|
|
|
{
|
2021-03-17 09:59:43 +02:00
|
|
|
messageBytes = cast(byte[])[27, '[','3','1','m'];
|
2020-10-29 11:19:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-21 17:48:15 +02:00
|
|
|
/* Add the message */
|
|
|
|
messageBytes ~= cast(byte[])text;
|
2020-10-29 11:19:12 +02:00
|
|
|
|
2023-02-21 17:48:15 +02:00
|
|
|
/* Switch back debugColor */
|
2021-03-17 09:59:43 +02:00
|
|
|
messageBytes ~= cast(byte[])[27, '[', '3', '9', 'm'];
|
2020-10-29 11:19:12 +02:00
|
|
|
|
2021-03-17 09:59:43 +02:00
|
|
|
return messageBytes;
|
|
|
|
}
|
|
|
|
|
2023-02-21 17:48:15 +02:00
|
|
|
private byte[] colorSrc(string text)
|
2021-03-17 09:59:43 +02:00
|
|
|
{
|
2023-02-21 17:48:15 +02:00
|
|
|
/* The generated message */
|
|
|
|
byte[] messageBytes;
|
2020-10-29 11:19:12 +02:00
|
|
|
|
2023-02-21 17:48:15 +02:00
|
|
|
/* Reset coloring */
|
|
|
|
messageBytes ~= [27, '[', 'm'];
|
2020-10-29 11:19:12 +02:00
|
|
|
|
2023-02-21 17:48:15 +02:00
|
|
|
/* Color gray */
|
|
|
|
messageBytes ~= [27, '[', '3', '9', ';', '2', 'm'];
|
2020-10-29 11:19:12 +02:00
|
|
|
|
2023-02-21 17:48:15 +02:00
|
|
|
/* Append the message */
|
|
|
|
messageBytes ~= text;
|
|
|
|
|
|
|
|
/* Reset coloring */
|
|
|
|
messageBytes ~= [27, '[', 'm'];
|
|
|
|
|
|
|
|
return messageBytes;
|
2023-01-07 18:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unittest
|
|
|
|
{
|
2023-02-21 17:48:15 +02:00
|
|
|
// alias debugTypes = __traits(allMembers, DebugType);
|
|
|
|
// static foreach(debugType; debugTypes)
|
|
|
|
// {
|
|
|
|
// gprintln("Hello world", mixin("DebugType."~debugType));
|
|
|
|
// }
|
2020-10-29 11:19:12 +02:00
|
|
|
}
|