Compare commits

...

31 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 7e14ec68a8 Extras
- Fixed import issue
2024-04-11 08:52:35 +02:00
Tristan B. Velloza Kildaire 974e8cf019
Feature/args helper (#3)
* Core

- Work-in-progress mixin

* Core

- Fixed imports

Test

- Out of module test added

* Core

- Cleaned up mixins

* Core

- Documented

* Test

- Documented

* Test

- made private

* Core

- Renamed to `LoggingFuncs`

* Test

- Use new name `LoggingFuncs`

* Core

- Cleanedxc uop

* Extras

- Added new package

* Core

- Moved

* Test

- Moved

* Extras (unittests)

- Cleaned up tests

* Test

- Cleaned up tests
2024-04-11 08:25:58 +02:00
Tristan B. Velloza Kildaire 05c74b1aa4 Dub
- Updated tagline
2024-04-10 22:05:59 +02:00
Tristan B. Velloza Kildaire fd678eb9fd README
- Updated API docs
2024-04-10 14:21:50 +02:00
Tristan B. Velloza Kildaire 548b2714c5 Dub
- Upgraded `dlog` to version (atleast) `1.0.0`
2024-04-10 14:02:51 +02:00
Tristan B. Velloza Kildaire ea7cb2c70d
Merge pull request #2 from deavmi/feature/dlog2
Feature/dlog2
2024-04-10 14:02:28 +02:00
Tristan B. Velloza Kildaire b5bddc151c Dub
- Updated `dlog` to version `0.4.0` (at least)
2024-04-10 13:58:04 +02:00
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
Tristan B. Velloza Kildaire 69c98859de Core
- Removed `enableDebug()` and `disableDebug()`

Core (unittests)

- Updated tests to now switch the debugging mode based on their level
2024-04-09 15:14:10 +02:00
Tristan B. Velloza Kildaire 7836b7bf5d Core
- Refactored a lot
2024-04-09 15:08:59 +02:00
Tristan B. Velloza Kildaire 9c950561ac GoggaLogger
- Added new logger

GoggaLogger (unittests)

- Migrated unittests
2024-04-09 14:36:16 +02:00
Tristan B. Velloza Kildaire 5c99f3f3b4 Gogga (legacy)
- Disabled
2024-04-09 14:35:57 +02:00
Tristan B. Velloza Kildaire 7baa5dbaef Package
- Disabled
2024-04-09 14:35:41 +02:00
Tristan B. Velloza Kildaire 861f4bc8d3 Context
- Removed

Transform

- Removed
2024-04-09 14:34:26 +02:00
Tristan B. Velloza Kildaire 140eadc321 Dub
- Added `dlog` local version
2024-04-09 14:30:30 +02:00
Tristan B. Velloza Kildaire 7686ac9b40 GoggaLogger
- Any method which is templated AND marked as `final` will NOT be virtual.
- Undid the `final` so as to provide us with vtable support when using via the super type `Logger`. Prior to this it would use the ones declared directly in `Logger` and not the others as effectively no override was present.
2024-04-08 17:36:59 +02:00
Tristan B. Velloza Kildaire e9f45e27f4 README
- Fixed links
2023-11-22 11:54:39 +02:00
Tristan B. Velloza Kildaire c66689d056
Merge pull request #1 from deavmi/rustacena_simple
Add simpler logger type
2023-11-22 11:51:35 +02:00
Tristan B. Velloza Kildaire e74f906b12 Core (unit tests)
- Added a unit test for `RUSTACEAN_SIMPLE`
2023-11-22 11:50:48 +02:00
Tristan B. Velloza Kildaire 08aa1d6c0c GoggaMode
- Added new mode `RUSTACEAN_SIMPLE`

GoggaTransform

- Added support for `RUSTACEAN_SIMPLE` mode
2023-11-22 11:49:39 +02:00
Tristan B. Velloza Kildaire 034b4cebd4 Merge branch 'master' of github.com:deavmi/gogga 2023-06-18 13:29:06 +02:00
Tristan B. Velloza Kildaire 469a687bd4 Dub
- Use minimum version `0.3.19` of `dlog`
2023-06-18 13:28:32 +02:00
Tristan B. Velloza Kildaire 061680a452
Update README.md 2023-03-25 22:45:12 +02:00
Tristan B. Velloza Kildaire 4f6d8641fc
Create d.yml 2023-03-25 22:43:57 +02:00
Tristan B. Velloza Kildaire f672484f2e - Upgraded `dlog` to version `0.3.19` 2023-03-11 15:55:07 +02:00
Tristan B. Velloza Kildaire 9b99ce9738 GoggaTransform
- Updated coumentation
2023-03-03 16:18:45 +02:00
Tristan B. Velloza Kildaire f324399feb GoggaTransform
- `Level.WARN` is now yellow
- Debugging color is now pink
2023-03-03 16:17:37 +02:00
9 changed files with 584 additions and 365 deletions

32
.github/workflows/d.yml vendored Normal file
View File

@ -0,0 +1,32 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: D
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7
- name: 'Build & Test'
run: |
# Build the project, with its main file included, without unittests
dub build --compiler=$DC
# Build and run tests, as defined by `unittest` configuration
# In this mode, `mainSourceFile` is excluded and `version (unittest)` are included
# See https://dub.pm/package-format-json.html#configurations
dub test --compiler=$DC

View File

@ -11,6 +11,8 @@
<br>
<br
[![D](https://github.com/deavmi/gogga/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/gogga/actions/workflows/d.yml)
## Usage
The API is rather straight-forward, simply create a new logger and then you can use it as such:
@ -29,9 +31,9 @@ This should output something like the following:
![](example.png)
Various [styles](https://gogga.dpldocs.info/v2.1.15/gogga.transform.GoggaMode.html) are supported which can be set using `mode(GoggaMode)`.
Various [styles](https://gogga.dpldocs.info/v3.0.1/gogga.core.GoggaMode.html) are supported which can be set using `mode(GoggaMode)`.
---
Or you can also [View the full API](https://gogga.dpldocs.info/v2.1.15/index.html).
Or you can also [View the full API](https://gogga.dpldocs.info/v3.0.1/gogga.html).

View File

@ -5,10 +5,10 @@
],
"copyright": "Copyright © 2023, Tristan B. Kildaire",
"dependencies": {
"dlog": "0.3.8"
"dlog": ">=1.0.0"
},
"description": "Simple VT100 colourised pretty-printing logger",
"description": "Plug-and-play colorful VT100 logging for your terminal",
"license": "LGPL 3.0",
"name": "gogga",
"targetType": "library"
}
}

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,62 +1,318 @@
/**
* Logging facilities
* Core routines and types
*
* Authors: Tristan Brice Velloza Kildaire (deavmi)
*/
module gogga.core;
import dlog;
import dlog.core;
import dlog.basic;
import std.conv : to;
import dlog.utilities : flatten;
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)
*/
public 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];
}
}
/**
* The logging class which provides the logging print
* calls, controlling of style and whether to debug or
* not
* A `GoggaMessage` which comprises
* of a `GoggaCompInfo` for context
*/
public class GoggaLogger : Logger
private class GoggaMessage : BasicMessage
{
/**
* The custom transformer
*/
private GoggaTransform gTransform = new GoggaTransform();
/**
* 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 */
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;
}
}
/**
* A `GoggaLogger`
*/
public final class GoggaLogger : BasicLogger
{
/**
* The Gogga text transformer
*/
private GoggaTransform gogTrans;
/**
* Whether debug prints are enabled or not
*/
private bool debugEnabled = false;
/**
* Constructs a new GoggaLOgger
*/
/**
* 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);
super();
this.gogTrans = new GoggaTransform();
addTransform(this.gogTrans);
}
/**
* Set the style of print outs
*
* Params:
* mode = the GoggaMode wanted
*/
public void mode(GoggaMode mode)
{
gTransform.setMode(mode);
}
/**
* Sets the style of logging
* to use
*
* Params:
* mode = the `GoggaMode`
*/
public void mode(GoggaMode mode)
{
this.gogTrans.setMode(mode);
}
/**
* 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
*/
public 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
@ -71,34 +327,13 @@ public class GoggaLogger : Logger
* __FUNCTION__ = compile time usage function
* __PRETTY_FUNCTION__ = compile time usage function (pretty)
*/
public final void error(TextType...)(TextType segments,
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__)
{
/* 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);
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.ERROR);
}
/**
@ -114,34 +349,13 @@ public class GoggaLogger : Logger
* __FUNCTION__ = compile time usage function
* __PRETTY_FUNCTION__ = compile time usage function (pretty)
*/
public final void info(TextType...)(TextType segments,
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);
/* 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);
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.INFO);
}
/**
@ -157,34 +371,13 @@ public class GoggaLogger : Logger
* __FUNCTION__ = compile time usage function
* __PRETTY_FUNCTION__ = compile time usage function (pretty)
*/
public final void warn(TextType...)(TextType segments,
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);
/* 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);
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.WARN);
}
/**
@ -201,70 +394,106 @@ public class GoggaLogger : Logger
* __FUNCTION__ = compile time usage function
* __PRETTY_FUNCTION__ = compile time usage function (pretty)
*/
public final void debug_(TextType...)(TextType segments,
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__)
{
/* Only debug if debugging is enabled */
if(debugEnabled)
{
/* 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);
}
{
doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.DEBUG);
}
/**
/**
* Alias for debug_
*/
public alias dbg = debug_;
}
/**
* Enables debug prints
*/
public void enableDebug()
{
this.debugEnabled = true;
}
/**
* 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;
/**
* Disables debug prints
*/
public void disableDebug()
/* If INFO, set green */
if(level == Level.INFO)
{
this.debugEnabled = false;
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;
}
version(unittest)
{
import std.stdio : writeln;
import std.stdio : writeln, stdout;
}
unittest
{
GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
// Test the normal modes
gLogger.info("This is an info message");
@ -275,7 +504,7 @@ unittest
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.enableDebug();
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
@ -285,6 +514,9 @@ unittest
unittest
{
GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
gLogger.mode(GoggaMode.TwoKTwenty3);
// Test the normal modes
@ -296,7 +528,7 @@ unittest
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.enableDebug();
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
@ -306,6 +538,9 @@ unittest
unittest
{
GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
gLogger.mode(GoggaMode.SIMPLE);
// Test the normal modes
@ -317,7 +552,7 @@ unittest
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.enableDebug();
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
@ -327,6 +562,9 @@ unittest
unittest
{
GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.INFO);
gLogger.mode(GoggaMode.RUSTACEAN);
// Test the normal modes
@ -338,7 +576,31 @@ unittest
gLogger.dbg("This is a debug which is hidden", 1);
// Now enable debugging and you should see it
gLogger.enableDebug();
gLogger.setLevel(Level.DEBUG);
gLogger.dbg("This is a VISIBLE debug", true);
// Make space between unit tests
writeln();
}
unittest
{
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

91
source/gogga/extras.d Normal file
View File

@ -0,0 +1,91 @@
/**
* Extra helper utilities
*
* Authors: Tristan Brice Velloza Kildaire (deavmi)
*/
module gogga.extras;
import gogga.core : GoggaLogger;
/**
* Mixes in a set of easy helper methods
* for each log-level in `Level`, with
* matching nams
*
* Params:
* gLogger = the `GoggaLogger` identifier
*/
public mixin template LoggingFuncs(alias gLogger)
if(__traits(isSame, typeof(gLogger), GoggaLogger))
{
import std.meta : AliasSeq, aliasSeqOf;
import std.traits : ParameterDefaults;
import std.traits : EnumMembers;
import dlog.basic : Level;
/**
* Generatesd a function named after
* the given log level and using
* the log level provided with
* the correct default (call-site
* based) arguments containing
* line information
*
* Params:
* gLogger = the logger identifier
* level = the log level
*/
private mixin template MakeFuncFor(alias GoggaLogger gLogger, Level level)
{
import std.conv : to;
import gogga.core : GoggaCompInfo;
mixin(`
public void `~to!(string)(level)~`(Text...)
(
Text segments,
string c1 = __FILE_FULL_PATH__,
string c2 = __FILE__,
ulong c3 = __LINE__,
string c4 = __MODULE__,
string c5 = __FUNCTION__,
string c6 = __PRETTY_FUNCTION__
)
{
gLogger.doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.`~to!(string)(level)~`);
}
`);
}
// Generate methods per each log level
static foreach(level; EnumMembers!(Level))
{
mixin MakeFuncFor!(gLogger, level);
}
}
version(unittest)
{
import gogga.extras;
import std.stdio : writeln, stdout;
import dlog.basic : Level, FileHandler;
}
/**
* Tests using the mixin for method
* names
*/
unittest
{
GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.DEBUG);
mixin LoggingFuncs!(gLogger);
DEBUG("This is debug", 2.3, true, [2,2]);
ERROR("This is error", 2.3, true, [2,2]);
INFO("This is info", 2.3, true, [2,2]);
WARN("This is warn", 2.3, true, [2,2]);
writeln();
}

View File

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

30
source/gogga/test.d Normal file
View File

@ -0,0 +1,30 @@
/**
* Out-of-module tests
*
* Authors: Tristan Brice Velloza Kildaire (deavmi)
*/
module gogga.test;
version(unittest)
{
import gogga;
import gogga.extras;
import std.stdio : writeln, stdout;
import dlog.basic : Level, FileHandler;
}
private unittest
{
GoggaLogger gLogger = new GoggaLogger();
gLogger.addHandler(new FileHandler(stdout));
gLogger.setLevel(Level.DEBUG);
mixin LoggingFuncs!(gLogger);
DEBUG("This is debug", 2.3, true, [2,2]);
ERROR("This is error", 2.3, true, [2,2]);
INFO("This is info", 2.3, true, [2,2]);
WARN("This is warn", 2.3, true, [2,2]);
writeln();
}

View File

@ -1,182 +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
}
/**
* 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
{
finalOutput = cast(string)debugColor(to!(string)(level)~"\t", level);
finalOutput ~= cast(string)(colorSrc(context[1]~"/"~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 WARNING, set warning */
else if(level == Level.WARN)
{
messageBytes = cast(byte[])[27, '[','3','5','m']; /* TODO: FInd yllow */
}
/* If ERROR, set error */
else if(level == Level.ERROR)
{
messageBytes = cast(byte[])[27, '[','3','1','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;
}