1 module gogga.core;
2 
3 import dlog;
4 import dlog.utilities : flatten;
5 import std.array : join;
6 
7 import gogga.transform;
8 import gogga.context;
9 
10 /** 
11  * The logging class which provides the logging print
12  * calls, controlling of style and whether to debug or
13  * not
14  */
15 public class GoggaLogger : Logger
16 {
17 	/** 
18 	 * The custom transformer
19 	 */
20     private GoggaTransform gTransform = new GoggaTransform();
21 
22 	/** 
23 	 * Whether debug prints are enabled or not
24 	 */
25     private bool debugEnabled = false;
26 
27 	/** 
28 	 * Constructs a new GoggaLOgger
29 	 */
30     this()
31     {
32         super(gTransform);
33     }
34     
35 	/** 
36 	 * Our underlying logging implementation
37 	 *
38 	 * Params:
39 	 *   text = the text to write
40 	 */
41     public override void logImpl(string text)
42     {
43         import std.stdio : write;
44         write(text);
45     }
46 
47 	/** 
48 	 * Set the style of print outs
49 	 *
50 	 * Params:
51 	 *   mode = the GoggaMode wanted
52 	 */
53 	public void mode(GoggaMode mode)
54 	{
55 		gTransform.setMode(mode);
56 	}
57 
58     /** 
59 	 * Logs using the default context an arbitrary amount of arguments
60 	 * specifically setting the context's level to ERROR
61 	 *
62 	 * Params:
63 	 *   segments = the arbitrary argumnets (alias sequence)
64 	 *   __FILE_FULL_PATH__ = compile time usage file
65 	 *   __FILE__ = compile time usage file (relative)
66 	 *   __LINE__ = compile time usage line number
67 	 *   __MODULE__ = compile time usage module
68 	 *   __FUNCTION__ = compile time usage function
69 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
70 	 */
71 	public final void error(TextType...)(TextType segments,
72 									string c1 = __FILE_FULL_PATH__,
73 									string c2 = __FILE__, ulong c3 = __LINE__,
74 									string c4 = __MODULE__, string c5 = __FUNCTION__,
75 									string c6 = __PRETTY_FUNCTION__)
76 	{
77 		/* Use the context `GoggaContext` */
78 		GoggaContext defaultContext = new GoggaContext();
79 
80 		/* Build up the line information */
81 		CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
82 
83 		/* Set the line information in the context */
84 		defaultContext.setLineInfo(compilationInfo);
85 
86 		/* Set the level to ERROR */
87 		defaultContext.setLevel(Level.ERROR);
88 
89 		/**
90 		 * Grab at compile-time all arguments and generate runtime code to add them to `components`
91 		 */		
92 		string[] components = flatten(segments);
93 
94 		/* Join all `components` into a single string */
95 		string messageOut = join(components, multiArgJoiner);
96 
97 		/* Call the log */
98 		logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
99 	}
100 
101 	/** 
102 	 * Logs using the default context an arbitrary amount of arguments
103 	 * specifically setting the context's level to INFO
104 	 *
105 	 * Params:
106 	 *   segments = the arbitrary argumnets (alias sequence)
107 	 *   __FILE_FULL_PATH__ = compile time usage file
108 	 *   __FILE__ = compile time usage file (relative)
109 	 *   __LINE__ = compile time usage line number
110 	 *   __MODULE__ = compile time usage module
111 	 *   __FUNCTION__ = compile time usage function
112 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
113 	 */
114 	public final void info(TextType...)(TextType segments,
115 									string c1 = __FILE_FULL_PATH__,
116 									string c2 = __FILE__, ulong c3 = __LINE__,
117 									string c4 = __MODULE__, string c5 = __FUNCTION__,
118 									string c6 = __PRETTY_FUNCTION__)
119 	{
120 		/* Use the context `GoggaContext` */
121 		GoggaContext defaultContext = new GoggaContext();
122 
123 		/* Build up the line information */
124 		CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
125 
126 		/* Set the line information in the context */
127 		defaultContext.setLineInfo(compilationInfo);
128 
129 		/* Set the level to INFO */
130 		defaultContext.setLevel(Level.INFO);
131 
132 		/**
133 		 * Grab at compile-time all arguments and generate runtime code to add them to `components`
134 		 */		
135 		string[] components = flatten(segments);
136 
137 		/* Join all `components` into a single string */
138 		string messageOut = join(components, multiArgJoiner);
139 
140 		/* Call the log */
141 		logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
142 	}
143 
144 	/** 
145 	 * Logs using the default context an arbitrary amount of arguments
146 	 * specifically setting the context's level to WARN
147 	 *
148 	 * Params:
149 	 *   segments = the arbitrary argumnets (alias sequence)
150 	 *   __FILE_FULL_PATH__ = compile time usage file
151 	 *   __FILE__ = compile time usage file (relative)
152 	 *   __LINE__ = compile time usage line number
153 	 *   __MODULE__ = compile time usage module
154 	 *   __FUNCTION__ = compile time usage function
155 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
156 	 */
157 	public final void warn(TextType...)(TextType segments,
158 									string c1 = __FILE_FULL_PATH__,
159 									string c2 = __FILE__, ulong c3 = __LINE__,
160 									string c4 = __MODULE__, string c5 = __FUNCTION__,
161 									string c6 = __PRETTY_FUNCTION__)
162 	{
163 		/* Use the context `GoggaContext` */
164 		GoggaContext defaultContext = new GoggaContext();
165 
166 		/* Build up the line information */
167 		CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
168 
169 		/* Set the line information in the context */
170 		defaultContext.setLineInfo(compilationInfo);
171 
172 		/* Set the level to WARN */
173 		defaultContext.setLevel(Level.WARN);
174 
175 		/**
176 		 * Grab at compile-time all arguments and generate runtime code to add them to `components`
177 		 */		
178 		string[] components = flatten(segments);
179 
180 		/* Join all `components` into a single string */
181 		string messageOut = join(components, multiArgJoiner);
182 
183 		/* Call the log */
184 		logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
185 	}
186 
187 	/** 
188 	 * Logs using the default context an arbitrary amount of arguments
189 	 * specifically setting the context's level to DEBUG
190 	 *
191 	 * Params:
192 	 *   segments = the arbitrary argumnets (alias sequence)
193 	 *   __FILE_FULL_PATH__ = compile time usage file
194 	 *   __FILE__ = compile time usage file (relative)
195 	 *   __LINE__ = compile time usage line number
196 	 *   __MODULE__ = compile time usage module
197 	 *   __FUNCTION__ = compile time usage function
198 	 *   __PRETTY_FUNCTION__ = compile time usage function (pretty)
199 	 */
200 	public final void debug_(TextType...)(TextType segments,
201 									string c1 = __FILE_FULL_PATH__,
202 									string c2 = __FILE__, ulong c3 = __LINE__,
203 									string c4 = __MODULE__, string c5 = __FUNCTION__,
204 									string c6 = __PRETTY_FUNCTION__)
205 	{
206 		/* Only debug if debugging is enabled */
207 		if(debugEnabled)
208 		{
209 			/* Use the context `GoggaContext` */
210 			GoggaContext defaultContext = new GoggaContext();
211 
212 			/* Build up the line information */
213 			CompilationInfo compilationInfo = CompilationInfo(c1, c2, c3, c4, c5, c6);
214 
215 			/* Set the line information in the context */
216 			defaultContext.setLineInfo(compilationInfo);
217 
218 			/* Set the level to DEBUG */
219 			defaultContext.setLevel(Level.DEBUG);
220 
221 			/**
222 			* Grab at compile-time all arguments and generate runtime code to add them to `components`
223 			*/		
224 			string[] components = flatten(segments);
225 
226 			/* Join all `components` into a single string */
227 			string messageOut = join(components, multiArgJoiner);
228 
229 			/* Call the log */
230 			logc(defaultContext, messageOut, c1, c2, c3, c4, c5, c6);
231 		}
232 	}
233 
234 	/* You can also call using `dbg` */
235 	public alias dbg = debug_;
236 
237 	/** 
238 	 * Enables debug prints
239 	 */
240     public void enableDebug()
241     {
242         this.debugEnabled = true;
243 	}
244 
245 	/** 
246 	 * Disables debug prints
247 	 */
248     public void disableDebug()
249     {
250         this.debugEnabled = false;
251     }
252 }
253 
254 version(unittest)
255 {
256 	import std.stdio : writeln;
257 }
258 
259 unittest
260 {
261     GoggaLogger gLogger = new GoggaLogger();
262 
263 	// Test the normal modes
264     gLogger.info("This is an info message");
265     gLogger.warn("This is a warning message");
266     gLogger.error("This is an error message");
267 
268 	// We shouldn't see anything as debug is off
269 	gLogger.dbg("This is a debug which is hidden", 1);
270 
271 	// Now enable debugging and you should see it
272 	gLogger.enableDebug();
273 	gLogger.dbg("This is a VISIBLE debug", true);
274 
275     // Make space between unit tests
276 	writeln();
277 }
278 
279 unittest
280 {
281     GoggaLogger gLogger = new GoggaLogger();
282 	gLogger.mode(GoggaMode.TwoKTwenty3);
283 
284     // Test the normal modes
285     gLogger.info("This is an info message");
286     gLogger.warn("This is a warning message");
287     gLogger.error("This is an error message");
288 
289 	// We shouldn't see anything as debug is off
290 	gLogger.dbg("This is a debug which is hidden", 1);
291 
292 	// Now enable debugging and you should see it
293 	gLogger.enableDebug();
294 	gLogger.dbg("This is a VISIBLE debug", true);
295 
296     // Make space between unit tests
297 	writeln();
298 }
299 
300 unittest
301 {
302     GoggaLogger gLogger = new GoggaLogger();
303 	gLogger.mode(GoggaMode.SIMPLE);
304 
305     // Test the normal modes
306     gLogger.info("This is an info message");
307     gLogger.warn("This is a warning message");
308     gLogger.error("This is an error message");
309 
310 	// We shouldn't see anything as debug is off
311 	gLogger.dbg("This is a debug which is hidden", 1);
312 
313 	// Now enable debugging and you should see it
314 	gLogger.enableDebug();
315 	gLogger.dbg("This is a VISIBLE debug", true);
316 
317     // Make space between unit tests
318 	writeln();
319 }
320 
321 unittest
322 {
323     GoggaLogger gLogger = new GoggaLogger();
324 	gLogger.mode(GoggaMode.RUSTACEAN);
325 
326     // Test the normal modes
327     gLogger.info("This is an info message");
328     gLogger.warn("This is a warning message");
329     gLogger.error("This is an error message");
330 
331 	// We shouldn't see anything as debug is off
332 	gLogger.dbg("This is a debug which is hidden", 1);
333 
334 	// Now enable debugging and you should see it
335 	gLogger.enableDebug();
336 	gLogger.dbg("This is a VISIBLE debug", true);
337 
338     // Make space between unit tests
339 	writeln();
340 }