Compare commits

...

5 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 14b732bf61 Gogga
- Completely finished
2024-04-09 17:45:22 +02:00
Tristan B. Velloza Kildaire e3b27f6504 Gogga
- Removed comment
2024-04-09 17:39:52 +02:00
Tristan B. Velloza Kildaire bfff52be39 Gogga
- More docs
2024-04-09 17:30:56 +02:00
Tristan B. Velloza Kildaire 646be85ee7 GoggaLogger
- Uses the new Dlog API
2024-04-09 17:27:00 +02:00
Tristan B. Velloza Kildaire 579340e785 Refactor
- Cleaned up naming etc
2024-04-09 16:59:40 +02:00
3 changed files with 548 additions and 845 deletions

View File

@ -1,361 +1,608 @@
/** /**
* Logging facilities * Core routines and types
*
* Authors: Tristan Brice Velloza Kildaire (deavmi)
*/ */
module gogga.core; module gogga.core;
import dlog; import dlog.core;
import dlog.basic;
import std.conv : to;
import dlog.utilities : flatten; import dlog.utilities : flatten;
import std.array : join; import std.array : join;
// import gogga.transform; /**
// import gogga.context; * 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
}
/**
* Information obtained during compilation time (if any)
*/
private struct GoggaCompInfo
{
/**
* compile time usage file
*/
public string fullFilePath;
/**
* compile time usage file (relative)
*/
public string file;
/**
* compile time usage line number
*/
public ulong line;
/**
* compile time usage module
*/
public string moduleName;
/**
* compile time usage function
*/
public string functionName;
/**
* compile time usage function (pretty)
*/
public string prettyFunctionName;
/**
* Constructs the compilation information with the provided
* parameters
*
* Params:
* __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)
*/
this(string fullFilePath, string file, ulong line, string moduleName, string functionName, string prettyFunctionName)
{
this.fullFilePath = fullFilePath;
this.file = file;
this.line = line;
this.moduleName = moduleName;
this.functionName = functionName;
this.prettyFunctionName = prettyFunctionName;
}
/**
* Flattens the known compile-time information into a string array
*
* Returns: a string[]
*/
public string[] toArray()
{
return [fullFilePath, file, to!(string)(line), moduleName, functionName, prettyFunctionName];
}
}
/**
* A `GoggaMessage` which comprises
* of a `GoggaCompInfo` for context
*/
private class GoggaMessage : BasicMessage
{
/**
* The line information
*/
private GoggaCompInfo ctx;
/**
* Sets the context
*
* Params:
* ctx = the context
*/
public void setContext(GoggaCompInfo ctx)
{
this.ctx = ctx;
}
/**
* Returns the context
*
* Returns: the context
*/
public GoggaCompInfo getContext()
{
return this.ctx;
}
}
/**
* The Gogga transformer which
* applies stylization to
* incoming `GoggaMessage`(s)
*/
private class GoggaTransform : Transform
{
/**
* Mode to use for stylization
*/
private GoggaMode mode;
/**
* Sets the stylization to
* use when transforming
* the message's text
*
* Params:
* mode = the `GoggaMode`
*/
public void setMode(GoggaMode mode)
{
this.mode = mode;
}
/**
* Transforms the incoming message
* to use the Gogga stylization. This
* will be a no-op if the incoming
* message is not a `GoggaMessage`.
*
* Params:
* message = the message to transform
* Returns: the transformed message,
* else the same exact one
*/
public Message transform(Message message)
{
// Only handle GoggaMessage(s)
GoggaMessage goggaMesg = cast(GoggaMessage)message;
if(goggaMesg is null)
{
return null;
}
/* The generated output string */
string finalOutput;
/* Extract the Level */
Level level = goggaMesg.getLevel();
// /** /* Extract the text */
// * The logging class which provides the logging print string text = goggaMesg.getText();
// * calls, controlling of style and whether to debug or
// * not
// */
// public class GoggaLogger : Logger
// {
// /**
// * The custom transformer
// */
// private GoggaTransform gTransform = new GoggaTransform();
// /** /* get the context data */
// * Whether debug prints are enabled or not string[] context = goggaMesg.getContext().toArray();
// */
// private bool debugEnabled = false;
// /**
// * Constructs a new GoggaLOgger
// */
// this()
// {
// super(gTransform);
// }
// /**
// * Our underlying logging implementation
// *
// * Params:
// * text = the text to write
// */
// public override void logImpl(string text)
// {
// import std.stdio : write;
// write(text);
// }
// /** /**
// * Set the style of print outs * Simple mode is just: `[<LEVEL>] <message>`
// * */
// * Params: if(this.mode == GoggaMode.SIMPLE)
// * mode = the GoggaMode wanted {
// */ finalOutput = cast(string)debugColor("["~to!(string)(level)~"] ", level);
// public void mode(GoggaMode mode)
// {
// gTransform.setMode(mode);
// }
// /** finalOutput ~= text~"\n";
// * Logs using the default context an arbitrary amount of arguments }
// * specifically setting the context's level to ERROR /**
// * * TwoKTwenty3 is: `[<file>] (<module>:<lineNumber>) <message>`
// * Params: */
// * segments = the arbitrary argumnets (alias sequence) else if(this.mode == GoggaMode.TwoKTwenty3)
// * __FILE_FULL_PATH__ = compile time usage file {
// * __FILE__ = compile time usage file (relative) /* Module information (and status debugColoring) */
// * __LINE__ = compile time usage line number string moduleInfo = cast(string)debugColor("["~context[1]~"]", level);
// * __MODULE__ = compile time usage module
// * __FUNCTION__ = compile time usage function /* Function and line number info */
// * __PRETTY_FUNCTION__ = compile time usage function (pretty) string funcInfo = cast(string)(colorSrc("("~context[4]~":"~context[2]~")"));
// */
// public void error(TextType...)(TextType segments,
// string c1 = __FILE_FULL_PATH__,
// string c2 = __FILE__, ulong c3 = __LINE__,
// string c4 = __MODULE__, string c5 = __FUNCTION__,
// string c6 = __PRETTY_FUNCTION__)
// {
// /* Build up the line information */
// GoggaCompInfo compilationInfo = GoggaCompInfo(c1, c2, c3, c4, c5, c6);
// /** finalOutput = moduleInfo~" "~funcInfo~" "~text~"\n";
// * Grab at compile-time all arguments and generate runtime code to add them to `components` }
// */ /**
// string[] components = flatten(segments); * Rustacean mode is: `[<LEVEL>] (<filePath>/<functionName>:<lineNumber>) <message>`
*/
else if(this.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(this.mode == GoggaMode.RUSTACEAN_SIMPLE)
{
finalOutput = cast(string)debugColor(to!(string)(level)~"\t", level);
finalOutput ~= cast(string)(colorSrc(context[4]~":"~context[2]~" "));
finalOutput ~= text~"\n";
}
// /* Join all `components` into a single string */ goggaMesg.setText(finalOutput);
// string messageOut = join(components, " "); return message;
}
}
// /* Call the log */ /**
// logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6); * A `GoggaLogger`
// } */
public final class GoggaLogger : BasicLogger
{
/**
* The Gogga text transformer
*/
private GoggaTransform gogTrans;
// /** /**
// * Logs using the default context an arbitrary amount of arguments * Whether debug prints are enabled or not
// * specifically setting the context's level to INFO */
// * private bool debugEnabled = false;
// * 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 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); * Constructs a new `GoggaLogger`
*/
this()
{
super();
this.gogTrans = new GoggaTransform();
addTransform(this.gogTrans);
}
// /* Set the line information in the context */ /**
// defaultContext.setLineInfo(compilationInfo); * Sets the style of logging
* to use
*
* Params:
* mode = the `GoggaMode`
*/
public void mode(GoggaMode mode)
{
this.gogTrans.setMode(mode);
}
// /* Set the level to INFO */ /**
// defaultContext.setLevel(Level.INFO); * Performs the actual logging
* by packing up everything before
* sending it to the `log(Message)`
* method
*
* Params:
* segments = the compile-time segments
* info = the context
* level = the log level to use
*/
private void doLog(TextType...)(TextType segments, GoggaCompInfo info, Level level)
{
/* Create a new GoggaMessage */
GoggaMessage message = new GoggaMessage();
// /** /* Set context to the line information */
// * Grab at compile-time all arguments and generate runtime code to add them to `components` message.setContext(info);
// */
// string[] components = flatten(segments);
// /* Join all `components` into a single string */ /* Set the level */
// string messageOut = join(components, multiArgJoiner); message.setLevel(level);
// /* Call the log */ /**
// logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6); * Grab all compile-time arguments and make them
// } * into an array, then join them together and
* set that text as the message's text
*/
message.setText(join(flatten(segments), " "));
// /** /* Log this message */
// * Logs using the default context an arbitrary amount of arguments log(message);
// * 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 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); * 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 void error(TextType...)(TextType segments,
string c1 = __FILE_FULL_PATH__,
string c2 = __FILE__, ulong c3 = __LINE__,
string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.ERROR);
}
// /* Set the line information in the context */ /**
// defaultContext.setLineInfo(compilationInfo); * 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 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__)
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.INFO);
}
// /* Set the level to WARN */ /**
// defaultContext.setLevel(Level.WARN); * 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 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__)
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.WARN);
}
// /** /**
// * Grab at compile-time all arguments and generate runtime code to add them to `components` * Logs using the default context an arbitrary amount of arguments
// */ * specifically setting the context's level to DEBUG and will
// string[] components = flatten(segments); * only print if debugging is enabled
*
* 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 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__)
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.DEBUG);
}
// /* Join all `components` into a single string */ /**
// string messageOut = join(components, multiArgJoiner); * Alias for debug_
*/
public alias dbg = debug_;
}
// /* Call the log */ /**
// logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6); * 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 */
// * Logs using the default context an arbitrary amount of arguments if(level == Level.INFO)
// * specifically setting the context's level to DEBUG and will {
// * only print if debugging is enabled messageBytes = cast(byte[])[27, '[','3','2','m'];
// * }
// * Params: /* If WARN, set yellow */
// * segments = the arbitrary argumnets (alias sequence) else if(level == Level.WARN)
// * __FILE_FULL_PATH__ = compile time usage file {
// * __FILE__ = compile time usage file (relative) messageBytes = cast(byte[])[27, '[','3','1', ';', '9', '3', 'm'];
// * __LINE__ = compile time usage line number }
// * __MODULE__ = compile time usage module /* If ERROR, set red */
// * __FUNCTION__ = compile time usage function else if(level == Level.ERROR)
// * __PRETTY_FUNCTION__ = compile time usage function (pretty) {
// */ messageBytes = cast(byte[])[27, '[','3','1','m'];
// public void debug_(TextType...)(TextType segments, }
// string c1 = __FILE_FULL_PATH__, /* If DEBUG, set pink */
// string c2 = __FILE__, ulong c3 = __LINE__, else
// string c4 = __MODULE__, string c5 = __FUNCTION__, {
// string c6 = __PRETTY_FUNCTION__) messageBytes = cast(byte[])[27, '[','3','5','m'];
// { }
// /* Only debug if debugging is enabled */
// if(debugEnabled)
// {
// /* Use the context `GoggaContext` */
// GoggaContext defaultContext = new GoggaContext();
// /* Build up the line information */ /* Add the message */
// CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6); messageBytes ~= cast(byte[])text;
// /* Set the line information in the context */ /* Switch back debugColor */
// defaultContext.setLineInfo(compilationInfo); messageBytes ~= cast(byte[])[27, '[', '3', '9', 'm'];
// /* Set the level to DEBUG */ /* Reset coloring */
// defaultContext.setLevel(Level.DEBUG); messageBytes ~= [27, '[', 'm'];
// /** return messageBytes;
// * 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); * 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;
// /* Call the log */ /* Reset coloring */
// logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6); messageBytes ~= [27, '[', 'm'];
// }
// }
// /** /* Color gray */
// * Alias for debug_ messageBytes ~= [27, '[', '3', '9', ';', '2', 'm'];
// */
// public alias dbg = debug_;
// /** /* Append the message */
// * Enables debug prints messageBytes ~= text;
// */
// public void enableDebug()
// {
// this.debugEnabled = true;
// }
// /** /* Reset coloring */
// * Disables debug prints messageBytes ~= [27, '[', 'm'];
// */
// public void disableDebug()
// {
// this.debugEnabled = false;
// }
// }
// version(unittest) return messageBytes;
// { }
// import std.stdio : writeln;
// }
// unittest version(unittest)
// { {
// GoggaLogger gLogger = new GoggaLogger(); import std.stdio : writeln, stdout;
}
// // Test the normal modes unittest
// gLogger.info("This is an info message"); {
// gLogger.warn("This is a warning message"); GoggaLogger gLogger = new GoggaLogger();
// gLogger.error("This is an error message"); gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
// // We shouldn't see anything as debug is off // Test the normal modes
// gLogger.dbg("This is a debug which is hidden", 1); gLogger.info("This is an info message");
gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// // Now enable debugging and you should see it // We shouldn't see anything as debug is off
// gLogger.enableDebug(); gLogger.dbg("This is a debug which is hidden", 1);
// gLogger.dbg("This is a VISIBLE debug", true);
// // Make space between unit tests // Now enable debugging and you should see it
// writeln(); gLogger.setLevel(Level.DEBUG);
// } gLogger.dbg("This is a VISIBLE debug", true);
// unittest // Make space between unit tests
// { writeln();
// GoggaLogger gLogger = new GoggaLogger(); }
// gLogger.mode(GoggaMode.TwoKTwenty3);
// // Test the normal modes unittest
// gLogger.info("This is an info message"); {
// gLogger.warn("This is a warning message"); GoggaLogger gLogger = new GoggaLogger();
// gLogger.error("This is an error message"); gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
// // We shouldn't see anything as debug is off gLogger.mode(GoggaMode.TwoKTwenty3);
// gLogger.dbg("This is a debug which is hidden", 1);
// // Now enable debugging and you should see it // Test the normal modes
// gLogger.enableDebug(); gLogger.info("This is an info message");
// gLogger.dbg("This is a VISIBLE debug", true); gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// // Make space between unit tests // We shouldn't see anything as debug is off
// writeln(); gLogger.dbg("This is a debug which is hidden", 1);
// }
// unittest // Now enable debugging and you should see it
// { gLogger.setLevel(Level.DEBUG);
// GoggaLogger gLogger = new GoggaLogger(); gLogger.dbg("This is a VISIBLE debug", true);
// gLogger.mode(GoggaMode.SIMPLE);
// // Test the normal modes // Make space between unit tests
// gLogger.info("This is an info message"); writeln();
// gLogger.warn("This is a warning message"); }
// gLogger.error("This is an error message");
// // We shouldn't see anything as debug is off unittest
// gLogger.dbg("This is a debug which is hidden", 1); {
GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
// // Now enable debugging and you should see it gLogger.mode(GoggaMode.SIMPLE);
// gLogger.enableDebug();
// gLogger.dbg("This is a VISIBLE debug", true);
// // Make space between unit tests // Test the normal modes
// writeln(); gLogger.info("This is an info message");
// } gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// unittest // We shouldn't see anything as debug is off
// { gLogger.dbg("This is a debug which is hidden", 1);
// GoggaLogger gLogger = new GoggaLogger();
// gLogger.mode(GoggaMode.RUSTACEAN);
// // Test the normal modes // Now enable debugging and you should see it
// gLogger.info("This is an info message"); gLogger.setLevel(Level.DEBUG);
// gLogger.warn("This is a warning message"); gLogger.dbg("This is a VISIBLE debug", true);
// gLogger.error("This is an error message");
// // We shouldn't see anything as debug is off // Make space between unit tests
// gLogger.dbg("This is a debug which is hidden", 1); writeln();
}
// // Now enable debugging and you should see it unittest
// gLogger.enableDebug(); {
// gLogger.dbg("This is a VISIBLE debug", true); GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
// // Make space between unit tests gLogger.mode(GoggaMode.RUSTACEAN);
// writeln();
// }
// unittest // Test the normal modes
// { gLogger.info("This is an info message");
// GoggaLogger gLogger = new GoggaLogger(); gLogger.warn("This is a warning message");
// gLogger.mode(GoggaMode.RUSTACEAN_SIMPLE); gLogger.error("This is an error message");
// // Test the normal modes // We shouldn't see anything as debug is off
// gLogger.info("This is an info message"); gLogger.dbg("This is a debug which is hidden", 1);
// gLogger.warn("This is a warning message");
// gLogger.error("This is an error message");
// // We shouldn't see anything as debug is off // Now enable debugging and you should see it
// gLogger.dbg("This is a debug which is hidden", 1); gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// // Now enable debugging and you should see it // Make space between unit tests
// gLogger.enableDebug(); writeln();
// gLogger.dbg("This is a VISIBLE debug", true); }
// // Make space between unit tests unittest
// writeln(); {
// } GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
gLogger.mode(GoggaMode.RUSTACEAN_SIMPLE);
// Test the normal modes
gLogger.info("This is an info message");
gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// We shouldn't see anything as debug is off
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
writeln();
}

View File

@ -1,542 +0,0 @@
module gogga.nu.core;
import dlog.nu.core;
import dlog.nu.basic;
import std.conv : to;
import dlog.utilities : flatten;
import std.array : join;
/**
* 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
}
/**
* Information obtained during compilation time (if any)
*/
private struct GoggaCompInfo
{
/**
* compile time usage file
*/
public string fullFilePath;
/**
* compile time usage file (relative)
*/
public string file;
/**
* compile time usage line number
*/
public ulong line;
/**
* compile time usage module
*/
public string moduleName;
/**
* compile time usage function
*/
public string functionName;
/**
* compile time usage function (pretty)
*/
public string prettyFunctionName;
/**
* Constructs the compilation information with the provided
* parameters
*
* Params:
* __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)
*/
this(string fullFilePath, string file, ulong line, string moduleName, string functionName, string prettyFunctionName)
{
this.fullFilePath = fullFilePath;
this.file = file;
this.line = line;
this.moduleName = moduleName;
this.functionName = functionName;
this.prettyFunctionName = prettyFunctionName;
}
/**
* Flattens the known compile-time information into a string array
*
* Returns: a string[]
*/
public string[] toArray()
{
return [fullFilePath, file, to!(string)(line), moduleName, functionName, prettyFunctionName];
}
}
private class GoggaMessage : BasicMessage
{
private GoggaCompInfo ctx;
this()
{
}
public void setContext(GoggaCompInfo ctx)
{
this.ctx = ctx;
}
public GoggaCompInfo getContext()
{
return this.ctx;
}
}
private class GoggaTransform2 : Transform
{
private GoggaMode mode;
this()
{
// this.mode = mode;
}
public void setMode(GoggaMode mode)
{
this.mode = mode;
}
public Message transform(Message message)
{
// Only handle GoggaMessage(s)
GoggaMessage goggaMesg = cast(GoggaMessage)message;
if(goggaMesg is null)
{
return null;
}
/* The generated output string */
string finalOutput;
/* Extract the Level */
Level level = goggaMesg.getLevel();
/* Extract the text */
string text = goggaMesg.getText();
/* get the context data */
string[] context = goggaMesg.getContext().toArray();
/**
* Simple mode is just: `[<LEVEL>] <message>`
*/
if(this.mode == GoggaMode.SIMPLE)
{
finalOutput = cast(string)debugColor("["~to!(string)(level)~"] ", level);
finalOutput ~= text~"\n";
}
/**
* TwoKTwenty3 is: `[<file>] (<module>:<lineNumber>) <message>`
*/
else if(this.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(this.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(this.mode == GoggaMode.RUSTACEAN_SIMPLE)
{
finalOutput = cast(string)debugColor(to!(string)(level)~"\t", level);
finalOutput ~= cast(string)(colorSrc(context[4]~":"~context[2]~" "));
finalOutput ~= text~"\n";
}
goggaMesg.setText(finalOutput);
return message;
}
}
public class GoggaLogger2 : BasicLogger
{
private GoggaTransform2 gogTrans;
/**
* Whether debug prints are enabled or not
*/
private bool debugEnabled = false;
this()
{
super();
this.gogTrans = new GoggaTransform2();
addTransform(this.gogTrans);
}
public void mode(GoggaMode mode)
{
this.gogTrans.setMode(mode);
}
private void doLog(TextType...)(TextType segments, GoggaCompInfo info, Level level)
{
/* Create a new GoggaMessage */
GoggaMessage message = new GoggaMessage();
/* Set context to the line information */
message.setContext(info);
/* Set the level */
message.setLevel(level);
/**
* Grab all compile-time arguments and make them
* into an array, then join them together and
* set that text as the message's text
*/
message.setText(join(flatten(segments), " "));
/* Log this message */
log(message);
}
/**
* 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 void error(TextType...)(TextType segments,
string c1 = __FILE_FULL_PATH__,
string c2 = __FILE__, ulong c3 = __LINE__,
string c4 = __MODULE__, string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__)
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.ERROR);
}
/**
* 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 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__)
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.INFO);
}
/**
* 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 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__)
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.WARNING);
}
/**
* Logs using the default context an arbitrary amount of arguments
* specifically setting the context's level to DEBUG and will
* only print if debugging is enabled
*
* 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 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__)
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.DEBUG);
}
/**
* Alias for debug_
*/
public alias dbg = debug_;
}
/**
* 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.WARNING)
{
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;
}
version(unittest)
{
import std.stdio : writeln, stdout;
}
unittest
{
GoggaLogger2 gLogger = new GoggaLogger2();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
// Test the normal modes
gLogger.info("This is an info message");
gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// We shouldn't see anything as debug is off
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
writeln();
}
unittest
{
GoggaLogger2 gLogger = new GoggaLogger2();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
gLogger.mode(GoggaMode.TwoKTwenty3);
// Test the normal modes
gLogger.info("This is an info message");
gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// We shouldn't see anything as debug is off
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
writeln();
}
unittest
{
GoggaLogger2 gLogger = new GoggaLogger2();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
gLogger.mode(GoggaMode.SIMPLE);
// Test the normal modes
gLogger.info("This is an info message");
gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// We shouldn't see anything as debug is off
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
writeln();
}
unittest
{
GoggaLogger2 gLogger = new GoggaLogger2();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
gLogger.mode(GoggaMode.RUSTACEAN);
// Test the normal modes
gLogger.info("This is an info message");
gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// We shouldn't see anything as debug is off
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
writeln();
}
unittest
{
GoggaLogger2 gLogger = new GoggaLogger2();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
gLogger.mode(GoggaMode.RUSTACEAN_SIMPLE);
// Test the normal modes
gLogger.info("This is an info message");
gLogger.warn("This is a warning message");
gLogger.error("This is an error message");
// We shouldn't see anything as debug is off
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
writeln();
}

View File

@ -1,16 +1,14 @@
/** /**
* Gogga logging facilities * Gogga logging facilities
*/ */
/**
* The Gogga logging library
*
* Authors: Tristan Brice Velloza Kildaire (deavmi)
*/
module gogga; module gogga;
/** /**
* The logging class which provides the logging print * Core routines and types
* calls, controlling of style and whether to debug or
* not
*/ */
// public import gogga.core : GoggaLogger; public import gogga.core;
// /**
// * The gogga styles supported
// */
// public import gogga.transform : GoggaMode;