From 974e8cf019aea40cabe7b9ef4493e117b6f86150 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 11 Apr 2024 08:25:58 +0200 Subject: [PATCH] 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 --- source/gogga/core.d | 4 +- source/gogga/extras.d | 90 +++++++++++++++++++++++++++++++++++++++++++ source/gogga/test.d | 30 +++++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 source/gogga/extras.d create mode 100644 source/gogga/test.d diff --git a/source/gogga/core.d b/source/gogga/core.d index 551329b..d5f2ae7 100644 --- a/source/gogga/core.d +++ b/source/gogga/core.d @@ -40,7 +40,7 @@ public enum GoggaMode /** * Information obtained during compilation time (if any) */ -private struct GoggaCompInfo +public struct GoggaCompInfo { /** * compile time usage file @@ -292,7 +292,7 @@ public final class GoggaLogger : BasicLogger * info = the context * level = the log level to use */ - private void doLog(TextType...)(TextType segments, GoggaCompInfo info, Level level) + public void doLog(TextType...)(TextType segments, GoggaCompInfo info, Level level) { /* Create a new GoggaMessage */ GoggaMessage message = new GoggaMessage(); diff --git a/source/gogga/extras.d b/source/gogga/extras.d new file mode 100644 index 0000000..0ad17d4 --- /dev/null +++ b/source/gogga/extras.d @@ -0,0 +1,90 @@ +/** + * Extra helper utilities + * + * Authors: Tristan Brice Velloza Kildaire (deavmi) + */ +module gogga.extras; + +/** + * 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; + 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(); +} \ No newline at end of file diff --git a/source/gogga/test.d b/source/gogga/test.d new file mode 100644 index 0000000..73b730d --- /dev/null +++ b/source/gogga/test.d @@ -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(); +} \ No newline at end of file