- Removed

Transform

- Removed
This commit is contained in:
Tristan B. Velloza Kildaire 2024-04-09 14:34:26 +02:00
parent 140eadc321
commit 861f4bc8d3
2 changed files with 0 additions and 215 deletions

View File

@ -1,14 +0,0 @@
/**
* Custom context for gogga logging
*/
module gogga.context;
import dlog;
/**
* Pass in a custom context to be used for gogga
*/
public final class GoggaContext : Context
{
// TODO: Put more advanced stuff here
}

View File

@ -1,201 +0,0 @@
/**
* The custom text transformer that implements the gogga-stylised
* logging messages
*/
module gogga.transform;
import dlog;
import gogga.context;
import std.conv : to;
/**
* The gogga styles supported
*/
public enum GoggaMode
{
/**
* TwoKTwenty3 is: `[<file>] (<module>:<lineNumber>) <message>`
*/
TwoKTwenty3,
/**
* Simple mode is just: `[<LEVEL>] <message>`
*/
SIMPLE,
/**
* Rustacean mode is: `[<LEVEL>] (<filePath>/<functionName>:<lineNumber>) <message>`
*/
RUSTACEAN,
/**
* Simple rustacean mode is: `[<LEVEL>] (<functionName>:<lineNumber>) <message>`
*/
RUSTACEAN_SIMPLE
}
/**
* The custom gogga text transformer
*/
public class GoggaTransform : MessageTransform
{
/**
* Current style
*/
private GoggaMode mode;
/**
* Transforms the provided text
*
* Params:
* text = text to transform
* ctx = the context passed in
* Returns: a string of transformed text
*/
public override string transform(string text, Context ctx)
{
/* The generated output string */
string finalOutput;
/* Get the GoggaContext */
GoggaContext gCtx = cast(GoggaContext)ctx;
/* Extract the line information */
CompilationInfo compInfo = gCtx.getLineInfo();
string[] context = compInfo.toArray();
/* Extract the Level */
Level level = gCtx.getLevel();
/**
* Simple mode is just: `[<LEVEL>] <message>`
*/
if(mode == GoggaMode.SIMPLE)
{
finalOutput = cast(string)debugColor("["~to!(string)(level)~"] ", level);
finalOutput ~= text~"\n";
}
/**
* TwoKTwenty3 is: `[<file>] (<module>:<lineNumber>) <message>`
*/
else if(mode == GoggaMode.TwoKTwenty3)
{
/* Module information (and status debugColoring) */
string moduleInfo = cast(string)debugColor("["~context[1]~"]", level);
/* Function and line number info */
string funcInfo = cast(string)(colorSrc("("~context[4]~":"~context[2]~")"));
finalOutput = moduleInfo~" "~funcInfo~" "~text~"\n";
}
/**
* Rustacean mode is: `[<LEVEL>] (<filePath>/<functionName>:<lineNumber>) <message>`
*/
else if(mode == GoggaMode.RUSTACEAN)
{
finalOutput = cast(string)debugColor(to!(string)(level)~"\t", level);
finalOutput ~= cast(string)(colorSrc(context[1]~"/"~context[4]~":"~context[2]~" "));
finalOutput ~= text~"\n";
}
/**
* Simple rustacean mode is: `[<LEVEL>] (<functionName>:<lineNumber>) <message>`
*/
else if(mode == GoggaMode.RUSTACEAN_SIMPLE)
{
finalOutput = cast(string)debugColor(to!(string)(level)~"\t", level);
finalOutput ~= cast(string)(colorSrc(context[4]~":"~context[2]~" "));
finalOutput ~= text~"\n";
}
return finalOutput;
}
/**
* Set the gogga style
*
* Params:
* mode = the GoggaMode to use
*/
public void setMode(GoggaMode mode)
{
this.mode = mode;
}
}
/**
* Colorise the text provided accoridng to the level and then
* reset the colors at the end
*
* Params:
* text = the text to colorise
* level = the color to use
* Returns: the byte sequence of characters and controls
*/
private byte[] debugColor(string text, Level level)
{
/* The generated message */
byte[] messageBytes;
/* If INFO, set green */
if(level == Level.INFO)
{
messageBytes = cast(byte[])[27, '[','3','2','m'];
}
/* If WARN, set yellow */
else if(level == Level.WARN)
{
messageBytes = cast(byte[])[27, '[','3','1', ';', '9', '3', 'm'];
}
/* If ERROR, set red */
else if(level == Level.ERROR)
{
messageBytes = cast(byte[])[27, '[','3','1','m'];
}
/* If DEBUG, set pink */
else
{
messageBytes = cast(byte[])[27, '[','3','5','m'];
}
/* Add the message */
messageBytes ~= cast(byte[])text;
/* Switch back debugColor */
messageBytes ~= cast(byte[])[27, '[', '3', '9', 'm'];
/* Reset coloring */
messageBytes ~= [27, '[', 'm'];
return messageBytes;
}
/**
* Colors the provided text in a gray fashion and then
* resets back to normal
*
* Params:
* text = the text to gray color
* Returns: the byte sequence of characters and controls
*/
private byte[] colorSrc(string text)
{
/* The generated message */
byte[] messageBytes;
/* Reset coloring */
messageBytes ~= [27, '[', 'm'];
/* Color gray */
messageBytes ~= [27, '[', '3', '9', ';', '2', 'm'];
/* Append the message */
messageBytes ~= text;
/* Reset coloring */
messageBytes ~= [27, '[', 'm'];
return messageBytes;
}