gogga/source/gogga.d

318 lines
9.0 KiB
D
Raw Normal View History

2020-10-29 09:19:12 +00:00
module gogga;
import std.conv : to;
import std.stdio : write, stdout;
2023-02-21 15:48:15 +00:00
// TODO: Remove (just for testing)
import std.stdio : writeln;
import dlog;
import dlog.utilities : flatten;
import std.array : join;
2023-02-21 15:48:15 +00:00
2023-03-02 11:01:29 +00:00
2020-10-29 09:19:12 +00:00
2023-02-21 15:48:15 +00:00
unittest
{
GoggaLogger gLogger = new GoggaLogger();
// TODO: Somehow re-enable this please
2023-02-21 15:48:15 +00:00
// gLogger.log("Bruh\n");
gLogger.info("This is an info message");
gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
2023-03-02 11:01:29 +00:00
// TODO: Add debug stuff
2023-02-21 15:48:15 +00:00
}
public final class GoggaContext : Context
{
// TODO: Put more advanced stuff here
2023-02-21 15:48:15 +00:00
}
public class GoggaLogger : Logger
{
private GoggaTransform gTransform = new GoggaTransform();
this()
{
super(gTransform);
}
public override void logImpl(string text)
{
import std.stdio : write;
write(text);
}
/**
* Logs using the default context an arbitrary amount of arguments
* specifically setting the context's level to ERROR
*
* 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 error(TextType...)(TextType segments,
string c1 = __FILE_FULL_PATH__,
2023-02-21 15:48:15 +00:00
string c2 = __FILE__, ulong c3 = __LINE__,
string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
/* Use the context `GoggaContext` */
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 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 c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
/* Use the context `GoggaContext` */
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 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 c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
/* Use the context `GoggaContext` */
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 WARN */
defaultContext.setLevel(Level.WARN);
/**
* 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 c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
/* Use the context `GoggaContext` */
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_;
2023-02-21 15:48:15 +00:00
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, Context ctx)
2023-02-21 15:48:15 +00:00
{
/* Get the GoggaContext */
GoggaContext gCtx = cast(GoggaContext)ctx;
/* Extract the line information */
CompilationInfo compInfo = gCtx.getLineInfo();
string[] context = compInfo.toArray();
2023-02-21 15:48:15 +00:00
/* Module information (and status debugColoring) */
string moduleInfo = cast(string)debugColor("["~context[1]~"]", gCtx.getLevel());
2023-02-21 15:48:15 +00:00
/* Function and line number info */
string funcInfo = cast(string)(colorSrc("("~context[4]~":"~context[2]~")"));
return moduleInfo~" "~funcInfo~" "~text~"\n";
2023-02-21 15:48:15 +00:00
}
}
private byte[] debugColor(string text, Level level)
2020-10-29 09:19:12 +00:00
{
/* The generated message */
byte[] messageBytes;
2020-10-29 09:19:12 +00:00
/* If INFO, set green */
if(level == Level.INFO)
2020-10-29 09:19:12 +00:00
{
messageBytes = cast(byte[])[27, '[','3','2','m'];
2020-10-29 09:19:12 +00:00
}
/* If WARNING, set warning */
else if(level == Level.WARN)
2020-10-29 09:19:12 +00:00
{
messageBytes = cast(byte[])[27, '[','3','5','m']; /* TODO: FInd yllow */
2020-10-29 09:19:12 +00:00
}
/* If ERROR, set error */
else if(level == Level.ERROR)
2020-10-29 09:19:12 +00:00
{
messageBytes = cast(byte[])[27, '[','3','1','m'];
2020-10-29 09:19:12 +00:00
}
2023-02-21 15:48:15 +00:00
/* Add the message */
messageBytes ~= cast(byte[])text;
2020-10-29 09:19:12 +00:00
2023-02-21 15:48:15 +00:00
/* Switch back debugColor */
messageBytes ~= cast(byte[])[27, '[', '3', '9', 'm'];
2020-10-29 09:19:12 +00:00
return messageBytes;
}
2023-02-21 15:48:15 +00:00
private byte[] colorSrc(string text)
{
2023-02-21 15:48:15 +00:00
/* The generated message */
byte[] messageBytes;
2020-10-29 09:19:12 +00:00
2023-02-21 15:48:15 +00:00
/* Reset coloring */
messageBytes ~= [27, '[', 'm'];
2020-10-29 09:19:12 +00:00
2023-02-21 15:48:15 +00:00
/* Color gray */
messageBytes ~= [27, '[', '3', '9', ';', '2', 'm'];
2020-10-29 09:19:12 +00:00
2023-02-21 15:48:15 +00:00
/* Append the message */
messageBytes ~= text;
/* Reset coloring */
messageBytes ~= [27, '[', 'm'];
return messageBytes;
}
unittest
{
2023-02-21 15:48:15 +00:00
// alias debugTypes = __traits(allMembers, DebugType);
// static foreach(debugType; debugTypes)
// {
// gprintln("Hello world", mixin("DebugType."~debugType));
// }
2020-10-29 09:19:12 +00:00
}