1 /**
2  * Extra helper utilities
3  *
4  * Authors: Tristan Brice Velloza Kildaire (deavmi)
5  */
6 module gogga.extras;
7 
8 import gogga.core : GoggaLogger;
9 
10 /** 
11  * Mixes in a set of easy helper methods
12  * for each log-level in `Level`, with
13  * matching nams
14  *
15  * Params:
16  *   gLogger = the `GoggaLogger` identifier
17  */
18 public mixin template LoggingFuncs(alias gLogger)
19 if(__traits(isSame, typeof(gLogger), GoggaLogger))
20 {
21     import std.meta : AliasSeq, aliasSeqOf;
22     import std.traits : ParameterDefaults;
23     import std.traits : EnumMembers;
24     import dlog.basic : Level;
25 
26     /** 
27      * Generatesd a function named after
28      * the given log level and using
29      * the log level provided with
30      * the correct default (call-site
31      * based) arguments containing
32      * line information
33      *
34      * Params:
35      *   gLogger = the logger identifier
36      *   level = the log level
37      */
38     private mixin template MakeFuncFor(alias GoggaLogger gLogger, Level level)
39     {
40         import std.conv : to;
41         import gogga.core : GoggaCompInfo;
42         mixin(`
43         public void `~to!(string)(level)~`(Text...)
44         (
45             Text segments,
46             string c1 = __FILE_FULL_PATH__,
47             string c2 = __FILE__,
48             ulong c3 = __LINE__,
49             string c4 = __MODULE__,
50             string c5 = __FUNCTION__,
51             string c6 = __PRETTY_FUNCTION__
52         )
53         {
54             gLogger.doLog(segments, GoggaCompInfo(c1, c2, c3, c4, c5, c6), Level.`~to!(string)(level)~`);
55         }
56         `);
57     }
58 
59     // Generate methods per each log level
60     static foreach(level; EnumMembers!(Level))
61     {
62         mixin MakeFuncFor!(gLogger, level);
63     }    
64 }
65 
66 version(unittest)
67 {
68     import gogga.extras;
69     import std.stdio : writeln, stdout;
70     import dlog.basic : Level, FileHandler;
71 }
72 
73 /** 
74  * Tests using the mixin for method
75  * names
76  */
77 unittest
78 {
79     GoggaLogger gLogger = new GoggaLogger();
80     gLogger.addHandler(new FileHandler(stdout));
81     gLogger.setLevel(Level.DEBUG);
82 
83     mixin LoggingFuncs!(gLogger);
84 
85     DEBUG("This is debug", 2.3, true, [2,2]);
86     ERROR("This is error", 2.3, true, [2,2]);
87     INFO("This is info", 2.3, true, [2,2]);
88     WARN("This is warn", 2.3, true, [2,2]);
89 
90     writeln();
91 }